From 29cb1b6e019e54646c1db5dc06243cde394c042f Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Tue, 30 May 2023 16:13:28 +0200 Subject: [PATCH] feat(tools): update v8,v9 migration generators to update npmignore --- .../migrate-converged-pkg/index.spec.ts | 1 + .../generators/migrate-converged-pkg/index.ts | 5 +- tools/generators/migrate-v8-pkg/index.spec.ts | 88 +++++++++++- tools/generators/migrate-v8-pkg/index.ts | 127 +++++++++++++++++- tools/generators/migrate-v8-pkg/schema.json | 4 + tools/generators/migrate-v8-pkg/schema.ts | 4 + 6 files changed, 217 insertions(+), 12 deletions(-) diff --git a/tools/generators/migrate-converged-pkg/index.spec.ts b/tools/generators/migrate-converged-pkg/index.spec.ts index 061e26ecc9bf1b..d02f32d9c5d649 100644 --- a/tools/generators/migrate-converged-pkg/index.spec.ts +++ b/tools/generators/migrate-converged-pkg/index.spec.ts @@ -1058,6 +1058,7 @@ describe('migrate-converged-pkg generator', () => { .git* .prettierignore .swcrc + project.json # exclude gitignore patterns explicitly !lib diff --git a/tools/generators/migrate-converged-pkg/index.ts b/tools/generators/migrate-converged-pkg/index.ts index 779cce54762dcb..6e32061c688521 100644 --- a/tools/generators/migrate-converged-pkg/index.ts +++ b/tools/generators/migrate-converged-pkg/index.ts @@ -142,7 +142,7 @@ function runMigrationOnProject(tree: Tree, schema: AssertedSchema, _userLog: Use setupNpmIgnoreConfig(tree, options); setupBabel(tree, options); - updateNxWorkspace(tree, options); + updateNxProject(tree, options); setupUnstableApi(tree, optionsWithTsConfigs); @@ -397,6 +397,7 @@ const templates = { .git* .prettierignore .swcrc + project.json # exclude gitignore patterns explicitly !lib @@ -580,7 +581,7 @@ function uniqueArray(value: T[]) { return Array.from(new Set(value)); } -function updateNxWorkspace(tree: Tree, options: NormalizedSchema) { +function updateNxProject(tree: Tree, options: NormalizedSchema) { const packageType = getPackageType(tree, options); const tags = { web: 'platform:web', diff --git a/tools/generators/migrate-v8-pkg/index.spec.ts b/tools/generators/migrate-v8-pkg/index.spec.ts index dfdae4f2c84be6..717e722151696e 100644 --- a/tools/generators/migrate-v8-pkg/index.spec.ts +++ b/tools/generators/migrate-v8-pkg/index.spec.ts @@ -2,13 +2,13 @@ import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { Tree, readProjectConfiguration, - readWorkspaceConfiguration, - WorkspaceConfiguration, serializeJson, stripIndents, addProjectConfiguration, ProjectConfiguration, logger, + readNxJson, + NxJsonConfiguration, } from '@nrwl/devkit'; import type { Linter } from 'eslint'; @@ -63,9 +63,89 @@ describe('migrate-v8-pkg generator', () => { expect(loggerInfoSpy).toHaveBeenCalled(); }); }); + + describe(`--name`, () => { + it(`should setup .npmignore`, async () => { + await generator(tree, options); + + expect(tree.read(`packages/eight/.npmignore`, 'utf-8')).toMatchInlineSnapshot(` + "*.api.json + *.config.js + *.log + *.nuspec + *.test.* + *.yml + .editorconfig + .eslintrc* + .eslintcache + .gitattributes + .gitignore + .vscode + coverage + dist/storybook + dist/*.stats.html + dist/*.stats.json + dist/demo + fabric-test* + gulpfile.js + images + index.html + jsconfig.json + node_modules + results + src/**/* + !src/**/*.types.ts + temp + tsconfig.json + tsd.json + tslint.json + typings + visualtests + project.json + + # exclude gitignore patterns explicitly + !lib + !lib-commonjs + !lib-amd + !dist" + `); + }); + }); + + describe(`--all`, () => { + const projects = [ + options.name, + '@proj/react-foo', + '@proj/react-bar', + '@proj/react-moo', + '@proj/react-zoo', + ] as const; + + beforeEach(() => { + setupDummyPackage(tree, { name: projects[1], version: '9.0.22' }); + setupDummyPackage(tree, { name: projects[2], version: '8.0.31' }); + setupDummyPackage(tree, { name: projects[3], version: '8.0.12' }); + setupDummyPackage(tree, { name: projects[4], version: '8.0.1' }); + }); + it(`should run migration on all vNext packages in batch`, async () => { + await generator(tree, { all: true }); + + const configs = projects.reduce((acc, projectName) => { + acc[projectName] = readProjectConfiguration(tree, projectName); + + return acc; + }, {} as Record<(typeof projects)[number], ProjectConfiguration>); + + expect(configs[projects[1]].sourceRoot).not.toBeDefined(); + expect(configs[options.name].sourceRoot).toBeDefined(); + expect(configs[projects[2]].sourceRoot).toBeDefined(); + expect(configs[projects[3]].sourceRoot).toBeDefined(); + expect(configs[projects[4]].sourceRoot).toBeDefined(); + }); + }); }); -function getNormalizedPkgName(options: { pkgName: string; workspaceConfig: WorkspaceConfiguration }) { +function getNormalizedPkgName(options: { pkgName: string; workspaceConfig: NxJsonConfiguration }) { return options.pkgName.replace(`@${options.workspaceConfig.npmScope}/`, ''); } function setupDummyPackage( @@ -79,7 +159,7 @@ function setupDummyPackage( projectConfiguration: Partial; }>, ) { - const workspaceConfig = readWorkspaceConfiguration(tree); + const workspaceConfig = readNxJson(tree) ?? {}; const defaults = { version: '8.0.0', dependencies: { diff --git a/tools/generators/migrate-v8-pkg/index.ts b/tools/generators/migrate-v8-pkg/index.ts index 18c472104ab409..5882a56b39b6b0 100644 --- a/tools/generators/migrate-v8-pkg/index.ts +++ b/tools/generators/migrate-v8-pkg/index.ts @@ -1,4 +1,3 @@ -import * as path from 'path'; import type { Linter } from 'eslint'; import { logger, @@ -9,11 +8,13 @@ import { readJson, joinPathFragments, ProjectConfiguration, + stripIndents, + updateProjectConfiguration, } from '@nrwl/devkit'; import { printStats } from '../print-stats'; -import { getProjectConfig, getProjects, isV8Package } from '../../utils'; +import { getProjectConfig, getProjects, isV8Package, printUserLogs, UserLog } from '../../utils'; import { MigrateV8PkgGeneratorSchema } from './schema'; import { PackageJson, TsConfig } from '../../types'; @@ -28,6 +29,7 @@ interface AssertedSchema extends MigrateV8PkgGeneratorSchema { const noop = () => {}; export default async function (tree: Tree, schema: MigrateV8PkgGeneratorSchema) { + const userLog: UserLog = []; const validatedSchema = await validateSchema(tree, schema); if (hasSchemaFlag(validatedSchema, 'stats')) { @@ -52,14 +54,19 @@ export default async function (tree: Tree, schema: MigrateV8PkgGeneratorSchema) return noop; } + if (hasSchemaFlag(validatedSchema, 'all')) { + runBatchMigration(tree, userLog); + } + if (hasSchemaFlag(validatedSchema, 'name')) { - console.log('THIS ISNT DOING ANYTHING YET, use --stats 🤝'); - const normalizedOptions = normalizeOptions(tree, validatedSchema); + runMigrationOnProject(tree, validatedSchema); } await formatFiles(tree); - return noop; + return () => { + printUserLogs(userLog); + }; } function normalizeOptions(tree: Tree, options: AssertedSchema) { @@ -90,8 +97,16 @@ async function validateSchema(tree: Tree, schema: MigrateV8PkgGeneratorSchema) { throw new Error('--name and --stats are mutually exclusive'); } + if (newSchema.name && newSchema.all) { + throw new Error('--name and --all are mutually exclusive'); + } + + if (newSchema.stats && newSchema.all) { + throw new Error('--stats and --all are mutually exclusive'); + } + const shouldValidateNameInput = () => { - return !newSchema.name && !newSchema.stats; + return !newSchema.name && !(newSchema.all || newSchema.stats); }; if (shouldValidateNameInput()) { @@ -153,3 +168,103 @@ function getProjectMetadata(tree: Tree, project: ProjectConfiguration) { } } } + +function runBatchMigration(tree: Tree, userLog: UserLog) { + const projects = getProjects(tree); + projects.forEach((projectConfig, projectName) => { + if (!isV8Package(tree, projectConfig)) { + userLog.push({ type: 'error', message: `${projectName} is not v8 package. Skipping migration...` }); + return; + } + + runMigrationOnProject(tree, { name: projectName }); + }); + + return tree; +} + +function runMigrationOnProject(tree: Tree, schema: AssertedSchema) { + const options = normalizeOptions(tree, schema); + + if (options.projectConfig.projectType === 'application') { + logger.warn( + stripIndents` + NOTE: you're trying to migrate an Application - ${options.name}. + We apply limited migration steps at the moment. + `, + ); + return; + } + + // updates start + + setupNpmIgnoreConfig(tree, options); + updateNxProject(tree, options); + + return tree; +} + +function setupNpmIgnoreConfig(tree: Tree, options: NormalizedSchema) { + tree.write(options.paths.npmConfig, templates.npmIgnoreConfig); + + return tree; +} + +function updateNxProject(tree: Tree, options: NormalizedSchema) { + updateProjectConfiguration(tree, options.name, { + ...options.projectConfig, + sourceRoot: joinPathFragments(options.projectConfig.root, 'src'), + tags: uniqueArray([...(options.projectConfig.tags ?? []), 'v8']), + implicitDependencies: uniqueArray([...(options.projectConfig.implicitDependencies ?? [])]), + }); + + return tree; +} + +const templates = { + npmIgnoreConfig: stripIndents` +*.api.json +*.config.js +*.log +*.nuspec +*.test.* +*.yml +.editorconfig +.eslintrc* +.eslintcache +.gitattributes +.gitignore +.vscode +coverage +dist/storybook +dist/*.stats.html +dist/*.stats.json +dist/demo +fabric-test* +gulpfile.js +images +index.html +jsconfig.json +node_modules +results +src/**/* +!src/**/*.types.ts +temp +tsconfig.json +tsd.json +tslint.json +typings +visualtests +project.json + +# exclude gitignore patterns explicitly +!lib +!lib-commonjs +!lib-amd +!dist +`, +}; + +function uniqueArray(value: T[]) { + return Array.from(new Set(value)); +} diff --git a/tools/generators/migrate-v8-pkg/schema.json b/tools/generators/migrate-v8-pkg/schema.json index 8e58beb33ab4a9..eb31715f70f472 100644 --- a/tools/generators/migrate-v8-pkg/schema.json +++ b/tools/generators/migrate-v8-pkg/schema.json @@ -12,6 +12,10 @@ "index": 0 } }, + "all": { + "type": "boolean", + "description": "Run generator on all v8 packages" + }, "stats": { "type": "boolean", "description": "Get statistics for how many projects have been migrated" diff --git a/tools/generators/migrate-v8-pkg/schema.ts b/tools/generators/migrate-v8-pkg/schema.ts index 192590d8dc6baa..d1a7bdb60cc794 100644 --- a/tools/generators/migrate-v8-pkg/schema.ts +++ b/tools/generators/migrate-v8-pkg/schema.ts @@ -3,6 +3,10 @@ export interface MigrateV8PkgGeneratorSchema { * Library name or comma delimited library names to execute migration on multiple libraries. */ name?: string; + /** + * Run generator on all v8 packages + */ + all?: boolean; /** * Get statistics for how many projects have been migrated */