Skip to content

Commit

Permalink
feat(package): better depth defaults on get prod deps
Browse files Browse the repository at this point in the history
  • Loading branch information
olup committed Feb 24, 2021
1 parent 2237ef7 commit 045167f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/pack-individually.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export async function packIndividually() {
// get a list of external dependencies already listed in package.json
const externals = Object.keys(require(path.join(buildDir, 'package.json')).dependencies);

// get a list of all production dependencies
const { dependencies } = await packager.getProdDependencies(buildDir, 10);
// get a tree of all production dependencies
const { dependencies } = await packager.getProdDependencies(buildDir);

// package each function
await Promise.all(
Expand Down
43 changes: 26 additions & 17 deletions src/packagers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ export class NPM implements Packager {
return true;
}

async getProdDependencies(cwd: string, depth: number) {
async getProdDependencies(cwd: string, depth?: number) {
// Get first level dependency graph
const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const args = [
'ls',
'-prod', // Only prod dependencies
'-json',
`-depth=${depth || 1}`
];
depth ? `-depth=${depth}` : null,
].filter(Boolean);

const ignoredNpmErrors = [
{ npmError: 'extraneous', log: false },
{ npmError: 'missing', log: false },
{ npmError: 'peer dep missing', log: true }
{ npmError: 'peer dep missing', log: true },
];

try {
Expand All @@ -45,15 +45,22 @@ export class NPM implements Packager {
if (err instanceof SpawnError) {
// Only exit with an error if we have critical npm errors for 2nd level inside
const errors = split('\n', err.stderr);
const failed = reduce((f, error) => {
if (f) {
return true;
}
return (
!isEmpty(error) &&
!any(ignoredError => startsWith(`npm ERR! ${ignoredError.npmError}`, error), ignoredNpmErrors)
);
}, false, errors);
const failed = reduce(
(f, error) => {
if (f) {
return true;
}
return (
!isEmpty(error) &&
!any(
ignoredError => startsWith(`npm ERR! ${ignoredError.npmError}`, error),
ignoredNpmErrors
)
);
},
false,
errors
);

if (!failed && !isEmpty(err.stdout)) {
return { stdout: err.stdout };
Expand Down Expand Up @@ -111,10 +118,12 @@ export class NPM implements Packager {
async runScripts(cwd, scriptNames) {
const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';

await Promise.all(scriptNames.map(scriptName => {
const args = ['run', scriptName];
await Promise.all(
scriptNames.map(scriptName => {
const args = ['run', scriptName];

return spawnProcess(command, args, { cwd });
}));
return spawnProcess(command, args, { cwd });
})
);
}
}
2 changes: 1 addition & 1 deletion src/packagers/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Packager {
lockfileName: string;
copyPackageSectionNames: Array<string>;
mustCopyModules: boolean;
getProdDependencies(cwd: string, depth: number): Promise<JSONObject>;
getProdDependencies(cwd: string, depth?: number): Promise<JSONObject>;
rebaseLockfile(pathToPackageRoot: string, lockfile: JSONObject): JSONObject;
install(cwd: string): Promise<void>;
prune(cwd: string): Promise<void>;
Expand Down
47 changes: 31 additions & 16 deletions src/packagers/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export class Yarn implements Packager {
return false;
}

async getProdDependencies(cwd, depth) {
async getProdDependencies(cwd: string, depth?: number) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['list', `--depth=${depth || 1}`, '--json', '--production'];
const args = ['list', depth ? `--depth=${depth}` : null, '--json', '--production'].filter(
Boolean
);

// If we need to ignore some errors add them here
const ignoredYarnErrors = [];
Expand All @@ -38,15 +40,22 @@ export class Yarn implements Packager {
if (err instanceof SpawnError) {
// Only exit with an error if we have critical npm errors for 2nd level inside
const errors = split('\n', err.stderr);
const failed = reduce((f, error) => {
if (f) {
return true;
}
return (
!isEmpty(error) &&
!any(ignoredError => startsWith(`npm ERR! ${ignoredError.npmError}`, error), ignoredYarnErrors)
);
}, false, errors);
const failed = reduce(
(f, error) => {
if (f) {
return true;
}
return (
!isEmpty(error) &&
!any(
ignoredError => startsWith(`npm ERR! ${ignoredError.npmError}`, error),
ignoredYarnErrors
)
);
},
false,
errors
);

if (!failed && !isEmpty(err.stdout)) {
return { stdout: err.stdout };
Expand All @@ -67,15 +76,15 @@ export class Yarn implements Packager {
}
__[head(splitModule)] = {
version: join('@', tail(splitModule)),
dependencies: convertTrees(tree.children)
dependencies: convertTrees(tree.children),
};
return __;
}, {});

const trees = pathOr([], ['data', 'trees'], parsedTree);
const result = {
problems: [],
dependencies: convertTrees(trees)
dependencies: convertTrees(trees),
};
return result;
}
Expand All @@ -89,12 +98,16 @@ export class Yarn implements Packager {
while ((match = fileVersionMatcher.exec(lockfile)) !== null) {
replacements.push({
oldRef: match[1],
newRef: replace(/\\/g, '/', `${pathToPackageRoot}/${match[1]}`)
newRef: replace(/\\/g, '/', `${pathToPackageRoot}/${match[1]}`),
});
}

// Replace all lines in lockfile
return reduce((__, replacement) => replace(__, replacement.oldRef, replacement.newRef), lockfile, replacements);
return reduce(
(__, replacement) => replace(__, replacement.oldRef, replacement.newRef),
lockfile,
replacements
);
}

async install(cwd) {
Expand All @@ -111,6 +124,8 @@ export class Yarn implements Packager {

async runScripts(cwd, scriptNames: string[]) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
await Promise.all(scriptNames.map(scriptName => spawnProcess(command, ['run', scriptName], { cwd })));
await Promise.all(
scriptNames.map(scriptName => spawnProcess(command, ['run', scriptName], { cwd }))
);
}
}

0 comments on commit 045167f

Please sign in to comment.