Skip to content

Commit

Permalink
fix(misc): update package.json only if needed
Browse files Browse the repository at this point in the history
Adjusts the schematic to only touch the package.json if really required

ISSUES CLOSED: nrwl#2317
  • Loading branch information
juristr committed Jan 21, 2020
1 parent 1132d9e commit c7f845b
Showing 1 changed file with 89 additions and 18 deletions.
107 changes: 89 additions & 18 deletions packages/jest/src/schematics/init/init.ts
@@ -1,5 +1,9 @@
import { mergeWith, chain, url, Tree } from '@angular-devkit/schematics';
import { addDepsToPackageJson, updateJsonInTree } from '@nrwl/workspace';
import {
addDepsToPackageJson,
updateJsonInTree,
readJsonInTree
} from '@nrwl/workspace';
import {
jestVersion,
jestTypesVersion,
Expand All @@ -9,22 +13,89 @@ import {
import { Rule } from '@angular-devkit/schematics';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';

const updatePackageJson = chain([
addDepsToPackageJson(
{},
{
'@nrwl/jest': nxVersion,
jest: jestVersion,
'@types/jest': jestTypesVersion,
'ts-jest': tsJestVersion
}
),
updateJsonInTree('package.json', json => {
json.dependencies = json.dependencies || {};
delete json.dependencies['@nrwl/jest'];
return json;
})
]);
/**
* verifies whether the given packageJson dependencies require an update
* given the deps & devDeps passed in
*/
const requiresAddingOfPackages = (packageJsonFile, deps, devDeps): boolean => {
let needsDepsUpdate = false;
let needsDevDepsUpdate = false;

if (Object.keys(deps).length > 0 && packageJsonFile.dependencies) {
needsDepsUpdate = !Object.keys(deps).find(
entry => packageJsonFile.dependencies[entry]
);
}

if (Object.keys(devDeps).length > 0 && packageJsonFile.dependencies) {
needsDevDepsUpdate = !Object.keys(devDeps).find(
entry => packageJsonFile.devDependencies[entry]
);
}

return needsDepsUpdate || needsDevDepsUpdate;
};

const updatePackageJson = (host: Tree): Rule => {
const rules = [];

// check whether to update the packge.json is necessary
const currentPackageJson = readJsonInTree(host, 'package.json');
const devDepstoUpdate = {
'@nrwl/jest': nxVersion,
jest: jestVersion,
'@types/jest': jestTypesVersion,
'ts-jest': tsJestVersion
};

if (requiresAddingOfPackages(currentPackageJson, {}, devDepstoUpdate)) {
rules.push(
addDepsToPackageJson(
{},
{
'@nrwl/jest': nxVersion,
jest: jestVersion,
'@types/jest': jestTypesVersion,
'ts-jest': tsJestVersion
}
)
);
}

if (
currentPackageJson.dependencies &&
currentPackageJson.dependencies['@nrwl/jest']
) {
rules.push(
updateJsonInTree('package.json', json => {
json.dependencies = json.dependencies || {};
delete json.dependencies['@nrwl/jest'];
return json;
})
);
}

return chain(rules);
};

const updatePackageJson2 = () => {
return chain([
addDepsToPackageJson(
{},
{
'@nrwl/jest': nxVersion,
jest: jestVersion,
'@types/jest': jestTypesVersion,
'ts-jest': tsJestVersion
}
),
updateJsonInTree('package.json', json => {
json.dependencies = json.dependencies || {};
delete json.dependencies['@nrwl/jest'];
return json;
})
]);
};

const createJestConfig = (host: Tree) => {
if (!host.exists('jest.config.js')) {
Expand All @@ -46,5 +117,5 @@ const createJestConfig = (host: Tree) => {
};

export default function(): Rule {
return chain([createJestConfig, updatePackageJson]);
return chain([createJestConfig, updatePackageJson2]);
}

0 comments on commit c7f845b

Please sign in to comment.