Skip to content

Commit

Permalink
fix(react-native): skipPackageJson was being ignored when creating a …
Browse files Browse the repository at this point in the history
…library or an app
  • Loading branch information
Nicholas Cunningham committed Nov 30, 2022
1 parent b9b4b2b commit b0efb99
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 37 deletions.
15 changes: 15 additions & 0 deletions docs/generated/packages/react-native.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript"
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": [],
Expand Down Expand Up @@ -135,6 +140,11 @@
"type": "boolean",
"description": "Runs `pod install` for native modules before building iOS app.",
"default": true
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": [],
Expand Down Expand Up @@ -240,6 +250,11 @@
"type": "boolean",
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": ["name"],
Expand Down
8 changes: 7 additions & 1 deletion packages/detox/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
addDependenciesToPackageJson,
convertNxGenerator,
formatFiles,
GeneratorCallback,
removeDependenciesFromPackageJson,
Tree,
} from '@nrwl/devkit';
Expand All @@ -15,7 +16,12 @@ import {
} from '../../utils/versions';

export async function detoxInitGenerator(host: Tree, schema: Schema) {
const tasks = [moveDependency(host), updateDependencies(host)];
const tasks: GeneratorCallback[] = [];

if (!schema.skipPackageJson) {
tasks.push(moveDependency(host));
tasks.push(updateDependencies(host));
}

if (!schema.skipFormat) {
await formatFiles(host);
Expand Down
1 change: 1 addition & 0 deletions packages/detox/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Schema {
skipFormat?: boolean;
skipPackageJson?: boolean; //default is false
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,21 @@ describe('app', () => {
});
});
});

describe('--skipPackageJson', () => {
it('should not add or update dependencies when true', async () => {
const packageJsonBefore = appTree.read('package.json', 'utf-8');

await reactNativeApplicationGenerator(appTree, {
name: 'myApp',
displayName: 'myApp',
linter: Linter.EsLint,
e2eTestRunner: 'none',
install: false,
skipPackageJson: true,
});

expect(appTree.read('package.json', 'utf-8')).toEqual(packageJsonBefore);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export async function reactNativeApplicationGenerator(
options.unitTestRunner,
options.projectName,
options.appProjectRoot,
options.js
options.js,
options.skipPackageJson
);
const detoxTask = await addDetox(host, options);
const symlinkTask = runSymlink(host.root, options.appProjectRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface Schema {
setParserOptionsProject?: boolean;
e2eTestRunner?: 'detox' | 'none';
install: boolean; // default is true
skipPackageJson?: boolean; //default is false
}
5 changes: 5 additions & 0 deletions packages/react-native/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
"type": "boolean",
"description": "Runs `pod install` for native modules before building iOS app.",
"default": true
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": []
Expand Down
10 changes: 9 additions & 1 deletion packages/react-native/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
convertNxGenerator,
detectPackageManager,
formatFiles,
GeneratorCallback,
removeDependenciesFromPackageJson,
Tree,
} from '@nrwl/devkit';
Expand Down Expand Up @@ -42,7 +43,14 @@ export async function reactNativeInitGenerator(host: Tree, schema: Schema) {
addGitIgnoreEntry(host);
initRootBabelConfig(host);

const tasks = [moveDependency(host), updateDependencies(host)];
const tasks: GeneratorCallback[] = [];

if (!schema.skipPackageJson) {
const installTask = updateDependencies(host);

tasks.push(moveDependency(host));
tasks.push(installTask);
}

if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') {
const jestTask = jestInitGenerator(host, schema);
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export interface Schema {
unitTestRunner?: 'jest' | 'none';
skipFormat?: boolean;
e2eTestRunner?: 'detox' | 'none';
skipPackageJson?: boolean; //default is false
js?: boolean;
}
5 changes: 5 additions & 0 deletions packages/react-native/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript"
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": []
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,17 @@ describe('lib', () => {
).not.toBeDefined();
});
});

describe('--skipPackageJson', () => {
it('should not add or update dependencies when true', async () => {
const packageJsonBefore = appTree.read('package.json', 'utf-8');

await libraryGenerator(appTree, {
...defaultSchema,
skipPackageJson: true,
});

expect(appTree.read('package.json', 'utf-8')).toEqual(packageJsonBefore);
});
});
});
3 changes: 2 additions & 1 deletion packages/react-native/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export async function reactNativeLibraryGenerator(
options.unitTestRunner,
options.name,
options.projectRoot,
options.js
options.js,
options.skipPackageJson
);

if (options.publishable || options.buildable) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface Schema {
globalCss?: boolean;
strict?: boolean;
setParserOptionsProject?: boolean;
skipPackageJson?: boolean; //default is false
}
5 changes: 5 additions & 0 deletions packages/react-native/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
"type": "boolean",
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": ["name"]
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native/src/utils/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export async function addJest(
unitTestRunner: 'jest' | 'none',
projectName: string,
appProjectRoot: string,
js: boolean
js: boolean,
skipPackageJson: boolean
) {
if (unitTestRunner !== 'jest') {
return () => {};
Expand All @@ -19,6 +20,7 @@ export async function addJest(
skipSerializers: true,
setupFile: 'none',
compiler: 'babel',
skipPackageJson,
});

// overwrite the jest.config.ts file because react native needs to have special transform property
Expand Down
21 changes: 15 additions & 6 deletions packages/react-native/src/utils/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-ser
import { Linter, lintProjectGenerator } from '@nrwl/linter';
import {
addDependenciesToPackageJson,
GeneratorCallback,
joinPathFragments,
Tree,
updateJson,
Expand All @@ -15,21 +16,26 @@ interface NormalizedSchema {
projectRoot: string;
setParserOptionsProject?: boolean;
tsConfigPaths: string[];
skipPackageJson?: boolean;
}

export async function addLinting(host: Tree, options: NormalizedSchema) {
if (options.linter === Linter.None) {
return () => {};
}
const tasks: GeneratorCallback[] = [];

const lintTask = await lintProjectGenerator(host, {
linter: options.linter,
project: options.projectName,
tsConfigPaths: options.tsConfigPaths,
eslintFilePatterns: [`${options.projectRoot}/**/*.{ts,tsx,js,jsx}`],
skipFormat: true,
skipPackageJson: options.skipPackageJson,
});

tasks.push(lintTask);

updateJson(
host,
joinPathFragments(options.projectRoot, '.eslintrc.json'),
Expand All @@ -51,11 +57,14 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
}
);

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

return runTasksInSerial(lintTask, installTask);
return runTasksInSerial(...tasks);
}
52 changes: 26 additions & 26 deletions packages/react/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { jestInitGenerator } from '@nrwl/jest';
import { webInitGenerator } from '@nrwl/web';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import {
Expand Down Expand Up @@ -42,48 +43,47 @@ function setDefault(host: Tree) {
updateWorkspaceConfiguration(host, { ...workspace, generators });
}

function updateDependencies(host: Tree, schema: InitSchema) {
function updateDependencies(host: Tree) {
removeDependenciesFromPackageJson(host, ['@nrwl/react'], []);

const dependencies = {
react: reactVersion,
'react-dom': reactDomVersion,
};

if (!schema.skipHelperLibs) {
dependencies['core-js'] = '^3.6.5';
dependencies['regenerator-runtime'] = '0.13.7';
dependencies['tslib'] = tsLibVersion;
}

return addDependenciesToPackageJson(host, dependencies, {
'@nrwl/react': nxVersion,
'@types/node': typesNodeVersion,
'@types/react': typesReactVersion,
'@types/react-dom': typesReactDomVersion,
'@testing-library/react': testingLibraryReactVersion,
'react-test-renderer': reactTestRendererVersion,
});
return addDependenciesToPackageJson(
host,
{
'core-js': '^3.6.5',
react: reactVersion,
'react-dom': reactDomVersion,
'regenerator-runtime': '0.13.7',
tslib: tsLibVersion,
},
{
'@nrwl/react': nxVersion,
'@types/node': typesNodeVersion,
'@types/react': typesReactVersion,
'@types/react-dom': typesReactDomVersion,
'@testing-library/react': testingLibraryReactVersion,
'react-test-renderer': reactTestRendererVersion,
}
);
}

export async function reactInitGenerator(host: Tree, schema: InitSchema) {
const tasks: GeneratorCallback[] = [];

setDefault(host);

if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') {
const jestTask = jestInitGenerator(host, schema);
tasks.push(jestTask);
}
if (!schema.e2eTestRunner || schema.e2eTestRunner === 'cypress') {
const cypressTask = cypressInitGenerator(host, {});
tasks.push(cypressTask);
}

// TODO(jack): We should be able to remove this generator and have react init everything.
const initTask = await webInitGenerator(host, {
...schema,
skipPackageJson: true,
});
const initTask = await webInitGenerator(host, schema);
tasks.push(initTask);
if (!schema.skipPackageJson) {
const installTask = updateDependencies(host, schema);
const installTask = updateDependencies(host);
tasks.push(installTask);
}

Expand Down

0 comments on commit b0efb99

Please sign in to comment.