Skip to content

Commit

Permalink
fix(repo): lookup for installed plugins in package.json
Browse files Browse the repository at this point in the history
In addition, we refactor the plugin capabilities and installed plugin
methods to avoid references to `node_modules`.
  • Loading branch information
bekos authored and vsavkin committed Sep 25, 2020
1 parent c3334d0 commit 341cc6b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 46 deletions.
4 changes: 2 additions & 2 deletions packages/workspace/src/command-line/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { output } from '../utils/output';
import {
fetchCommunityPlugins,
fetchCorePlugins,
getInstalledPluginsFromNodeModules,
getInstalledPluginsFromPackageJson,
listCommunityPlugins,
listCorePlugins,
listInstalledPlugins,
Expand Down Expand Up @@ -43,7 +43,7 @@ async function listHandler(args: YargsListArgs) {
} else {
const corePlugins = await fetchCorePlugins();
const communityPlugins = await fetchCommunityPlugins();
const installedPlugins = getInstalledPluginsFromNodeModules(
const installedPlugins = getInstalledPluginsFromPackageJson(
appRootPath,
corePlugins,
communityPlugins
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/utils/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {
} from './community-plugins';
export { fetchCorePlugins, listCorePlugins } from './core-plugins';
export {
getInstalledPluginsFromNodeModules,
getInstalledPluginsFromPackageJson,
listInstalledPlugins,
} from './installed-plugins';
export {
Expand Down
58 changes: 22 additions & 36 deletions packages/workspace/src/utils/plugins/installed-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,36 @@
import { terminal } from '@angular-devkit/core';
import { readdirSync } from 'fs';
import * as path from 'path';
import { readJsonFile } from '../fileutils';
import { output } from '../output';
import { CommunityPlugin, CorePlugin, PluginCapabilities } from './models';
import { getPluginCapabilities } from './plugin-capabilities';
import { hasElements } from './shared';

function getPackagesFromNodeModules(
workspaceRoot: string,
requery: boolean = false
): string[] {
let packageList: string[] = [];

if (!requery && packageList.length > 0) {
return packageList;
}

const nodeModulesDir = path.join(workspaceRoot, 'node_modules');
readdirSync(nodeModulesDir).forEach((npmPackageOrScope) => {
if (npmPackageOrScope.startsWith('@')) {
readdirSync(path.join(nodeModulesDir, npmPackageOrScope)).forEach((p) => {
packageList.push(`${npmPackageOrScope}/${p}`);
});
} else {
packageList.push(npmPackageOrScope);
}
});

return packageList;
}

export function getInstalledPluginsFromNodeModules(
export function getInstalledPluginsFromPackageJson(
workspaceRoot: string,
corePlugins: CorePlugin[],
communityPlugins: CommunityPlugin[]
): Array<PluginCapabilities> {
const corePluginNames = corePlugins.map((p) => p.name);
const communityPluginNames = communityPlugins.map((p) => p.name);
const packages = getPackagesFromNodeModules(workspaceRoot);
const packageJson = readJsonFile(`${workspaceRoot}/package.json`);

return packages
.filter(
(name) =>
corePluginNames.indexOf(name) > -1 ||
communityPluginNames.indexOf(name) > -1
)
const plugins = new Set([
...corePlugins.map((p) => p.name),
...communityPlugins.map((p) => p.name),
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.devDependencies || {}),
]);

return [...plugins]
.filter((name) => {
try {
// Check for `package.json` existence instead of requiring the module itself
// because malformed entries like `main`, may throw false exceptions.
require.resolve(`${name}/package.json`, { paths: [workspaceRoot] });
return true;
} catch {
return false;
}
})
.sort()
.map((name) => getPluginCapabilities(workspaceRoot, name))
.filter((x) => x && !!(x.schematics || x.builders));
}
Expand Down
25 changes: 18 additions & 7 deletions packages/workspace/src/utils/plugins/plugin-capabilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { terminal } from '@angular-devkit/core';
import * as path from 'path';
import { appRootPath } from '../app-root';
import { detectPackageManager } from '../detect-package-manager';
import { readJsonFile } from '../fileutils';
Expand All @@ -23,7 +22,8 @@ function getPackageManagerInstallCommand(): string {
}

function tryGetCollection<T>(
pluginPath: string,
workspaceRoot: string,
pluginName: string,
jsonFile: string,
propName: string
): T {
Expand All @@ -32,7 +32,10 @@ function tryGetCollection<T>(
}

try {
return readJsonFile<T>(path.join(pluginPath, jsonFile))[propName];
const jsonFilePath = require.resolve(`${pluginName}/${jsonFile}`, {
paths: [workspaceRoot],
});
return readJsonFile<T>(jsonFilePath)[propName];
} catch {
return null;
}
Expand All @@ -43,16 +46,24 @@ export function getPluginCapabilities(
pluginName: string
): PluginCapabilities {
try {
const pluginPath = path.join(workspaceRoot, 'node_modules', pluginName);
const packageJson = readJsonFile(path.join(pluginPath, 'package.json'));
const packageJsonPath = require.resolve(`${pluginName}/package.json`, {
paths: [workspaceRoot],
});
const packageJson = readJsonFile(packageJsonPath);
return {
name: pluginName,
schematics: tryGetCollection(
pluginPath,
workspaceRoot,
pluginName,
packageJson.schematics,
'schematics'
),
builders: tryGetCollection(pluginPath, packageJson.builders, 'builders'),
builders: tryGetCollection(
workspaceRoot,
pluginName,
packageJson.builders,
'builders'
),
};
} catch {
return null;
Expand Down

0 comments on commit 341cc6b

Please sign in to comment.