Skip to content

Commit

Permalink
npm auto install for non-root package.json (#1478)
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu authored Dec 3, 2024
1 parent a720d3b commit ee1c691
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
14 changes: 9 additions & 5 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,11 @@ export async function createNodejsBom(path, options) {
}
}
const pkgJsonLockFile = getAllFiles(path, "package-lock.json", options);
const pkgJsonFile = getAllFiles(path, "package.json", options);
const pkgJsonFile = getAllFiles(
path,
`${options.multiProject ? "**/" : ""}package.json`,
options,
);
const yarnLockFile = getAllFiles(path, "yarn.lock", options);
const pnpmLockFile = getAllFiles(path, "pnpm-lock.yaml", options);
if (
Expand All @@ -2283,7 +2287,7 @@ export async function createNodejsBom(path, options) {
) {
let pkgMgr = "npm";
const supPkgMgrs = ["npm", "yarn", "yarnpkg", "pnpm", "pnpx"];
const pkgData = JSON.parse(readFileSync(`${path}/package.json`, "utf8"));
const pkgData = JSON.parse(readFileSync(pkgJsonFile[0], "utf8"));
const mgrData = pkgData.packageManager;
let mgr = "";
let installArgs = ["install"];
Expand All @@ -2299,9 +2303,10 @@ export async function createNodejsBom(path, options) {
process.env[`${pkgMgr.toUpperCase()}_INSTALL_ARGS`].split(" ");
installArgs = installArgs.concat(addArgs);
}
console.log(`Executing '${pkgMgr} ${installArgs.join(" ")}' in`, path);
const basePath = dirname(pkgJsonFile[0]);
console.log(`Executing '${pkgMgr} ${installArgs.join(" ")}' in`, basePath);
const result = spawnSync(pkgMgr, installArgs, {
cwd: path,
cwd: basePath,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER,
Expand Down Expand Up @@ -6070,7 +6075,6 @@ export async function createXBom(path, options) {
} catch (err) {
return undefined;
}
// node.js - package.json
if (
existsSync(join(path, "package.json")) ||
existsSync(join(path, "rush.json")) ||
Expand Down
10 changes: 9 additions & 1 deletion lib/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,17 @@ export function getAllFiles(dirPath, pattern, options = {}) {
"**/flow-typed/**",
"**/coverage/**",
];
// Only ignore node_modules and docs if the caller is not looking for package.json
// Only ignore node_modules if the caller is not looking for package.json
if (!pattern.includes("package.json")) {
ignoreList.push("**/node_modules/**");
}
// ignore docs only for non-lock file lookups
if (
!pattern.includes("package.json") &&
!pattern.includes("package-lock.json") &&
!pattern.includes("yarn.lock") &&
!pattern.includes("pnpm-lock.yaml")
) {
ignoreList.push("**/docs/**");
}
if (options?.exclude && Array.isArray(options.exclude)) {
Expand Down
2 changes: 1 addition & 1 deletion types/lib/cli/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion types/lib/helpers/utils.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee1c691

Please sign in to comment.