Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): implement npm scripts updates for migration generator #18403

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,21 +398,33 @@ describe('migrate-converged-pkg generator', () => {
});
});

describe.skip('package.json updates', () => {
it(`should update npm scripts`, async () => {
describe('package.json updates', () => {
it(`should update package npm scripts`, async () => {
const projectConfig = readProjectConfiguration(tree, options.name);
let pkgJson = readJson(tree, `${projectConfig.root}/package.json`);

expect(pkgJson).toMatchInlineSnapshot();
expect(pkgJson.scripts).toMatchInlineSnapshot(`
Object {
"build": "just-scripts build",
"clean": "just-scripts clean",
"code-style": "just-scripts code-style",
"just": "just-scripts",
"lint": "just-scripts lint",
"start": "just-scripts dev:storybook",
"start-test": "just-scripts jest-watch",
"test": "just-scripts test",
"update-snapshots": "just-scripts jest -u",
}
`);

await generator(tree, options);

pkgJson = readJson(tree, `${projectConfig.root}/package.json`);

expect(pkgJson).toEqual({
expect(pkgJson.scripts).toEqual({
docs: 'api-extractor run --config=config/api-extractor.local.json --local',
// eslint-disable-next-line @fluentui/max-len
'build:local': `tsc -p . --module esnext --emitDeclarationOnly && node config/normalize-import --output dist/${projectConfig.root}/src && yarn docs`,
'build:local': `tsc -p . --module esnext --emitDeclarationOnly && node ../../scripts/typescript/normalize-import --output dist/${projectConfig.root}/src && yarn docs`,
build: 'just-scripts build',
clean: 'just-scripts clean',
'code-style': 'just-scripts code-style',
Expand All @@ -423,6 +435,16 @@ describe('migrate-converged-pkg generator', () => {
test: 'jest',
});
});

it(`should create api-extractor.local.json for scripts:docs task consumption`, async () => {
const projectConfig = readProjectConfiguration(tree, options.name);

expect(tree.exists(`${projectConfig.root}/config/api-extractor.local.json`)).toBeFalsy();

await generator(tree, options);

expect(tree.exists(`${projectConfig.root}/config/api-extractor.local.json`)).toBeTruthy();
});
});
});

Expand Down
39 changes: 38 additions & 1 deletion tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
stripIndents,
visitNotIgnoredFiles,
logger,
writeJson,
} from '@nrwl/devkit';
import { serializeJson } from '@nrwl/workspace';
import { updateJestConfig } from '@nrwl/jest/src/generators/jest-project/lib/update-jestconfig';
Expand All @@ -25,7 +26,7 @@ import { MigrateConvergedPkgGeneratorSchema } from './schema';
* 2. migrate to use standard jest powered by TS path aliases - #18368 ✅
* 3. bootstrap new storybook config - #18394 ✅
* 4. collocate all package stories from `react-examples` - #18394 ✅
* 5. update npm scripts (setup docs task to run api-extractor for local changes verification)
* 5. update npm scripts (setup docs task to run api-extractor for local changes verification) - #18403 ✅
*/

interface NormalizedSchema extends ReturnType<typeof normalizeOptions> {}
Expand All @@ -48,6 +49,10 @@ export default async function (tree: Tree, schema: MigrateConvergedPkgGeneratorS
moveStorybookFromReactExamples(tree, options);
deleteProjectFolderInReactExamples(tree, options);

// 5. update package npm scripts
updateNpmScripts(tree, options);
updateApiExtractorForLocalBuilds(tree, options);

formatFiles(tree);

return () => {
Expand All @@ -60,6 +65,11 @@ const userLog: Array<{ type: keyof typeof logger; message: string }> = [];
// ==== helpers ====

const templates = {
apiExtractor: {
$schema: 'https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json',
extends: './api-extractor.json',
mainEntryPointFilePath: '<projectFolder>/dist/<unscopedPackageName>/src/index.d.ts',
},
tsconfig: {
extends: '../../tsconfig.base.json',
compilerOptions: {
Expand Down Expand Up @@ -148,6 +158,7 @@ function normalizeOptions(host: Tree, options: MigrateConvergedPkgGeneratorSchem
*/
normalizedPkgName: options.name.replace(`@${workspaceConfig.npmScope}/`, ''),
paths: {
configRoot: joinPathFragments(projectConfig.root, 'config'),
packageJson: joinPathFragments(projectConfig.root, 'package.json'),
tsconfig: joinPathFragments(projectConfig.root, 'tsconfig.json'),
jestConfig: joinPathFragments(projectConfig.root, 'jest.config.js'),
Expand All @@ -163,6 +174,32 @@ function normalizeOptions(host: Tree, options: MigrateConvergedPkgGeneratorSchem
};
}

function updateNpmScripts(tree: Tree, options: NormalizedSchema) {
updateJson(tree, options.paths.packageJson, json => {
delete json.scripts['update-snapshots'];
delete json.scripts['start-test'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still want the deprecation message with these commands ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm not really - user that will run the migration on his component is in charge/aware of those changes. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah if the user does that it will make sense 👍


json.scripts.docs = 'api-extractor run --config=config/api-extractor.local.json --local';
json.scripts[
'build:local'
// eslint-disable-next-line @fluentui/max-len
] = `tsc -p . --module esnext --emitDeclarationOnly && node ../../scripts/typescript/normalize-import --output dist/${options.projectConfig.root}/src && yarn docs`;
json.scripts.storybook = 'start-storybook';
json.scripts.start = 'storybook';
json.scripts.test = 'jest';

return json;
});

return tree;
}

function updateApiExtractorForLocalBuilds(tree: Tree, options: NormalizedSchema) {
writeJson(tree, joinPathFragments(options.paths.configRoot, 'api-extractor.local.json'), templates.apiExtractor);

return tree;
}

function setupStorybook(tree: Tree, options: NormalizedSchema) {
tree.write(options.paths.storybook.tsconfig, serializeJson(templates.storybook.tsconfig));
tree.write(options.paths.storybook.main, templates.storybook.main);
Expand Down