Skip to content

Commit

Permalink
feat(react): add skipPackageJson flag (#12919)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Nov 2, 2022
1 parent 81a5db6 commit 4a4a149
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 21 deletions.
10 changes: 10 additions & 0 deletions docs/generated/packages/react.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
},
"js": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -390,6 +395,11 @@
"enum": ["babel", "swc"],
"default": "babel",
"description": "Which compiler to use."
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": ["name"],
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/web.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
"description": "Skip formatting files",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": [],
Expand Down
6 changes: 4 additions & 2 deletions packages/react/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ export async function reactInitGenerator(host: Tree, schema: InitSchema) {

const initTask = await webInitGenerator(host, schema);
tasks.push(initTask);
const installTask = updateDependencies(host);
tasks.push(installTask);
if (!schema.skipPackageJson) {
const installTask = updateDependencies(host);
tasks.push(installTask);
}

return runTasksInSerial(...tasks);
}
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export interface InitSchema {
unitTestRunner?: 'jest' | 'none';
e2eTestRunner?: 'cypress' | 'none';
skipFormat?: boolean;
skipPackageJson?: boolean;
js?: boolean;
}
5 changes: 5 additions & 0 deletions packages/react/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
},
"js": {
"type": "boolean",
"default": false,
Expand Down
23 changes: 22 additions & 1 deletion packages/react/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
Tree,
updateJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import {
createTreeWithEmptyV1Workspace,
createTreeWithEmptyWorkspace,
} from '@nrwl/devkit/testing';
import { Linter } from '@nrwl/linter';
import applicationGenerator from '../application/application';
import libraryGenerator from './library';
Expand Down Expand Up @@ -714,6 +717,24 @@ describe('lib', () => {
});
});

describe('--skipPackageJson', () => {
it('should not add dependencies to package.json when true', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
const packageJsonBeforeGenerator = tree.read('package.json', 'utf-8');
// ACT
await libraryGenerator(tree, {
...defaultSchema,
skipPackageJson: true,
});

// ASSERT
expect(tree.read('package.json', 'utf-8')).toEqual(
packageJsonBeforeGenerator
);
});
});

it.each`
style
${'styled-components'}
Expand Down
34 changes: 20 additions & 14 deletions packages/react/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ export async function libraryGenerator(host: Tree, schema: Schema) {
updateLibPackageNpmScope(host, options);
}

const installTask = await addDependenciesToPackageJson(
host,
{
react: reactVersion,
'react-dom': reactDomVersion,
},
options.compiler === 'swc' ? { '@swc/core': swcCoreVersion } : {}
);
tasks.push(installTask);
if (!options.skipPackageJson) {
const installTask = await addDependenciesToPackageJson(
host,
{
react: reactVersion,
'react-dom': reactDomVersion,
},
options.compiler === 'swc' ? { '@swc/core': swcCoreVersion } : {}
);
tasks.push(installTask);
}

const routeTask = updateAppRoutes(host, options);
tasks.push(routeTask);
Expand All @@ -150,6 +152,7 @@ async function addLinting(host: Tree, options: NormalizedSchema) {
unitTestRunner: options.unitTestRunner,
eslintFilePatterns: [`${options.projectRoot}/**/*.{ts,tsx,js,jsx}`],
skipFormat: true,
skipPackageJson: options.skipPackageJson,
});

const reactEslintJson = createReactEslintJson(
Expand All @@ -163,11 +166,14 @@ async function addLinting(host: Tree, options: NormalizedSchema) {
() => reactEslintJson
);

const installTask = await addDependenciesToPackageJson(
host,
extraEslintDependencies.dependencies,
extraEslintDependencies.devDependencies
);
let installTask = () => {};
if (!options.skipPackageJson) {
installTask = await addDependenciesToPackageJson(
host,
extraEslintDependencies.dependencies,
extraEslintDependencies.devDependencies
);
}

return runTasksInSerial(lintTask, installTask);
}
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export interface Schema {
setParserOptionsProject?: boolean;
standaloneConfig?: boolean;
compiler?: 'babel' | 'swc';
skipPackageJson?: boolean;
}
5 changes: 5 additions & 0 deletions packages/react/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@
"enum": ["babel", "swc"],
"default": "babel",
"description": "Which compiler to use."
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": ["name"]
Expand Down
14 changes: 10 additions & 4 deletions packages/web/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,21 @@ export async function webInitGenerator(tree: Tree, schema: Schema) {
let tasks: GeneratorCallback[] = [];

if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') {
const jestTask = jestInitGenerator(tree, {});
const jestTask = jestInitGenerator(tree, {
skipPackageJson: schema.skipPackageJson,
});
tasks.push(jestTask);
}
if (!schema.e2eTestRunner || schema.e2eTestRunner === 'cypress') {
const cypressTask = cypressInitGenerator(tree, {});
const cypressTask = cypressInitGenerator(tree, {
skipPackageJson: schema.skipPackageJson,
});
tasks.push(cypressTask);
}
const installTask = updateDependencies(tree, schema);
tasks.push(installTask);
if (!schema.skipPackageJson) {
const installTask = updateDependencies(tree, schema);
tasks.push(installTask);
}
initRootBabelConfig(tree);
if (!schema.skipFormat) {
await formatFiles(tree);
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface Schema {
unitTestRunner?: 'jest' | 'none';
e2eTestRunner?: 'cypress' | 'none';
skipFormat?: boolean;
skipPackageJson?: boolean;
}
5 changes: 5 additions & 0 deletions packages/web/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"description": "Skip formatting files",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": []
Expand Down

1 comment on commit 4a4a149

@vercel
Copy link

@vercel vercel bot commented on 4a4a149 Nov 2, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.