Skip to content

Commit

Permalink
fix(angular): check for devDeps & peerDeps when writing package versi…
Browse files Browse the repository at this point in the history
…on for dep libs

when determining the package version of dependent libraries, make sure that there's no devDeps or
peerDep already set. In such case don't touch the package.json
  • Loading branch information
juristr authored and vsavkin committed Feb 4, 2020
1 parent da1f851 commit c27b33a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
1 change: 0 additions & 1 deletion e2e/angular-package.test.ts
Expand Up @@ -67,7 +67,6 @@ forEachCli('angular', cli => {
`
);
};
debugger;

createDep(parentLib, [childLib, childLib2]);
});
Expand Down
45 changes: 42 additions & 3 deletions packages/angular/src/builders/package/package.impl.spec.ts
Expand Up @@ -149,9 +149,18 @@ describe('AngularLibraryWebBuildBuilder', () => {
});

it('should update the package.json', async () => {
spyOn(workspaceUtils, 'readJsonFile').and.returnValue({
name: '@proj/buildable-child',
version: '1.2.3'
spyOn(workspaceUtils, 'readJsonFile').and.callFake((path: string) => {
if (path.endsWith('buildable-parent/package.json')) {
return {
name: '@proj/buildable-parent',
version: '3.3.3'
};
} else {
return {
name: '@proj/buildable-child',
version: '1.2.3'
};
}
});

// act
Expand All @@ -168,5 +177,35 @@ describe('AngularLibraryWebBuildBuilder', () => {
})
);
});

['dependencies', 'devDependencies', 'peerDependencies'].forEach(
(depConfigName: string) => {
it(`should not update the package.json if the ${depConfigName} already contain a matching entry`, async () => {
spyOn(workspaceUtils, 'readJsonFile').and.callFake((path: string) => {
if (path.endsWith('buildable-parent/package.json')) {
return {
name: '@proj/buildable-parent',
version: '1.2.3',
[depConfigName]: {
'@proj/buildable-child': '1.1.1'
}
};
} else {
return {
name: '@proj/buildable-child',
version: '1.2.3'
};
}
});

// act
const result = await run(testOptions, context).toPromise();

// assert
expect(result.success).toBeTruthy();
expect(fileUtils.writeJsonFile).not.toHaveBeenCalled();
});
}
);
});
});
23 changes: 20 additions & 3 deletions packages/angular/src/builders/package/package.impl.ts
Expand Up @@ -6,7 +6,7 @@ import {
import { JsonObject } from '@angular-devkit/core';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import * as ng from '@angular/compiler-cli';
import { readJsonFile } from '@nrwl/workspace';
import { readJsonFile, output } from '@nrwl/workspace';
import {
createProjectGraph,
ProjectGraphNode,
Expand Down Expand Up @@ -149,6 +149,15 @@ export function calculateLibraryDependencies(
.filter(x => !!x);
}

// verify whether the package.json already specifies the dep
function hasDependency(outputJson, depConfigName: string, packageName: string) {
if (outputJson[depConfigName]) {
return outputJson[depConfigName][packageName];
} else {
return false;
}
}

/**
* Updates the peerDependencies section in the `dist/lib/xyz/package.json` with
* the proper dependency and version
Expand All @@ -168,11 +177,16 @@ function updatePackageJsonDependencies(
const jsonOutputFile = `${distLibOutputPath}/package.json`;
if (libDependencies && libDependencies.length > 0) {
const outputJson = readJsonFile(jsonOutputFile);
let writeJson = false;

outputJson.dependencies = outputJson.dependencies || {};

libDependencies.forEach(entry => {
if (!outputJson.dependencies[entry.scope]) {
if (
!hasDependency(outputJson, 'dependencies', entry.scope) &&
!hasDependency(outputJson, 'devDependencies', entry.scope) &&
!hasDependency(outputJson, 'peerDependencies', entry.scope)
) {
// read the lib version (should we read the one from the dist?)
const packageJsonPath = join(
context.workspaceRoot,
Expand All @@ -182,10 +196,13 @@ function updatePackageJsonDependencies(
const depNodePackageJson = readJsonFile(packageJsonPath);

outputJson.dependencies[entry.scope] = depNodePackageJson.version;
writeJson = true;
}
});

writeJsonFile(jsonOutputFile, outputJson);
if (writeJson) {
writeJsonFile(jsonOutputFile, outputJson);
}
}
}

Expand Down

0 comments on commit c27b33a

Please sign in to comment.