Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular/cli): resolve packages package.json from workspace directory #19071

Merged
merged 2 commits into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 22 additions & 32 deletions packages/angular/cli/utilities/package-tree.ts
Expand Up @@ -29,13 +29,7 @@ interface PackageJson {
};
}

async function readJSON(file: string) {
const buffer = await readFile(file);

return JSON.parse(buffer.toString());
}

function getAllDependencies(pkg: PackageJson) {
function getAllDependencies(pkg: PackageJson): Set<[string, string]> {
return new Set([
...Object.entries(pkg.dependencies || []),
...Object.entries(pkg.devDependencies || []),
Expand All @@ -53,47 +47,43 @@ export interface PackageTreeNode {

export async function readPackageJson(packageJsonPath: string): Promise<PackageJson | undefined> {
try {
return await readJSON(packageJsonPath);
} catch (err) {
return JSON.parse((await readFile(packageJsonPath)).toString());
} catch {
return undefined;
}
}

export function findPackageJson(workspaceDir: string, packageName: string) {
export function findPackageJson(workspaceDir: string, packageName: string): string | undefined {
try {
// avoid require.resolve here, see: https://github.com/angular/angular-cli/pull/18610#issuecomment-681980185
const packageJsonPath = resolve.sync(`${packageName}/package.json`, { paths: [workspaceDir] });
const packageJsonPath = resolve.sync(`${packageName}/package.json`, { basedir: workspaceDir });

return packageJsonPath;
} catch (err) {
} catch {
return undefined;
}
}

export async function getProjectDependencies(dir: string) {
const pkgJsonPath = resolve.sync(join(dir, `package.json`));
if (!pkgJsonPath) {
export async function getProjectDependencies(dir: string): Promise<Map<string, PackageTreeNode>> {
const pkg = await readPackageJson(join(dir, 'package.json'));
if (!pkg) {
throw new Error('Could not find package.json');
}

const pkg: PackageJson = await readJSON(pkgJsonPath);
const results = new Map<string, PackageTreeNode> ();
for (const [name, version] of getAllDependencies(pkg)) {
const packageJsonPath = findPackageJson(dir, name);
if (!packageJsonPath) {
continue;
}

const results = new Map<string, PackageTreeNode>();
await Promise.all(
Array.from(getAllDependencies(pkg)).map(async ([name, version]) => {
const packageJsonPath = findPackageJson(dir, name);
if (packageJsonPath) {
const currentDependency = {
name,
version,
path: dirname(packageJsonPath),
package: await readPackageJson(packageJsonPath),
};

results.set(currentDependency.name, currentDependency);
}
}),
);
results.set(name, {
name,
version,
path: dirname(packageJsonPath),
package: await readPackageJson(packageJsonPath),
});
}

return results;
}
2 changes: 1 addition & 1 deletion packages/schematics/update/update/index.ts
Expand Up @@ -531,7 +531,7 @@ function _usageMessage(
});

logger.info(
`There might be additional packages which don't provide 'ng update' capabilities that are outdated.\n`
`\nThere might be additional packages which don't provide 'ng update' capabilities that are outdated.\n`
+ `You can update the addition packages by running the update command of your package manager.`,
);

Expand Down