Skip to content

Commit

Permalink
fix(devkit): always read package version from the actual package in e…
Browse files Browse the repository at this point in the history
…nsurePackage function
  • Loading branch information
jaysoo committed Jan 25, 2023
1 parent bf0aaeb commit 4db58f5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
14 changes: 0 additions & 14 deletions packages/devkit/src/utils/package-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,20 +318,6 @@ describe('ensurePackage', () => {
tree = createTree();
});

it('should return without error when dependency is satisfied', async () => {
writeJson(tree, 'package.json', {
devDependencies: {
'@nrwl/vite': '15.0.0',
},
});

await expect(
ensurePackage(tree, '@nrwl/vite', '>=15.0.0', {
throwOnMissing: true,
})
).resolves.toBeUndefined();
});

it('should throw when dependencies are missing', async () => {
writeJson(tree, 'package.json', {});

Expand Down
19 changes: 6 additions & 13 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,27 +314,20 @@ export async function ensurePackage(
throwOnMissing?: boolean;
} = {}
): Promise<void> {
let version: string;
let version: string = null;

// Read package and version from root package.json file.
const packageJson = readJson(tree, 'package.json');
const dev = options.dev ?? true;
const throwOnMissing = options.throwOnMissing ?? !!process.env.NX_DRY_RUN; // NX_DRY_RUN is set in `packages/nx/src/command-line/generate.ts`
const pmc = getPackageManagerCommand();
const field = dev ? 'devDependencies' : 'dependencies';

version = packageJson[field]?.[pkg];

// If package not found, try to resolve it using Node and get its version.
if (!version) {
try {
version = require(`${pkg}/package.json`).version;
} catch {
// ignore
}
try {
version = require(`${pkg}/package.json`).version;
} catch {
// ignore
}

if (!satisfies(version, requiredVersion)) {
if (!version || !satisfies(version, requiredVersion)) {
const installCmd = `${
dev ? pmc.addDev : pmc.add
} ${pkg}@${requiredVersion}`;
Expand Down

0 comments on commit 4db58f5

Please sign in to comment.