Skip to content

Commit

Permalink
--only-packages positive filter to include only specific packages & i…
Browse files Browse the repository at this point in the history
…ts deps
  • Loading branch information
zbigg committed Nov 22, 2021
1 parent db12bc9 commit da8261f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Find and copy production deps of `package` living in `yarn workspace` to `dist` folder.

**NOTE: This is very experimental package.**, nevertheless it's used in one internal project
**NOTE: This is very experimental package.**, nevertheless it's used in one internal project
to build minimal `node_modules` for services running in docker.

## Usage
Expand All @@ -17,6 +17,10 @@ $ npx copy-production-deps

# copy deps specific package to specific folder
$ npx copy-production-deps packages/foo-backend dist/foo-backend

# Only dist selected packages that are not bundled with esbuild
$ esbuild server.js --outfile dist/server.js --external:mongoose --external:hiredis
$ npx copy-production-deps . dist --only-packages mongoose,hiredis
```

## Doc
Expand All @@ -41,9 +45,8 @@ Options:
--exclude-from Read ecluded file patterns from file one pattern a line.
[array] [default: []]
--exclude Exclude file pattern (minimatch glob) [array] [default: []]
--only-packages Include only those packages [array] [default: []
--help Show help [boolean]
```

## Why
Expand Down
13 changes: 12 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import * as path from "path";
import * as fs from "fs";
import yargs from "yargs";

function parseList(args: string[]): string[] {
return ([] as string[]).concat(...args.map((arg) => (arg.includes(",") ? arg.split(",") : [arg])));
}
async function asyncCommand(code: () => void) {
try {
await code();
Expand Down Expand Up @@ -55,6 +58,11 @@ function main() {
description: "Exclude file pattern (minimatch glob)",
default: [] as string[]
})
.option("only-packages", {
type: "array",
description: "Include only those packages",
default: [] as string[]
})
.command(
"$0 [packageDir] [distDir]",
"copy production deps from Npm/Yarn workspace to dist folder",
Expand All @@ -80,11 +88,14 @@ function main() {
const lines = parseBasicPatternList(fs.readFileSync(excludeFrom, "utf-8"));
allExcludedGlobs.push(...lines);
}
const onlyPackages =
argv["only-packages"].length > 0 ? parseList(argv["only-packages"]) : undefined;
const excludePaths = allExcludedGlobs.length > 0 ? globListFilter(allExcludedGlobs) : undefined;
const options: CopyProductionDepsOptions = {
dryRun: argv.dryRun,
verbose: argv.verbose,
excludePaths: excludePaths
excludePaths: excludePaths,
onlyPackages
};
await copyProductionDeps(packageDir, distDir, options);
})
Expand Down
5 changes: 5 additions & 0 deletions src/copy-production-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ export function assignTargetDirs(allPackages: Context, rootPkg: ResolvedPackage)
export interface CopyProductionDepsOptions {
dryRun?: boolean;
verbose?: boolean;
onlyPackages?: string[];
excludePaths?: (srcFileName: string) => boolean;
}

Expand Down Expand Up @@ -451,6 +452,10 @@ export async function copyProductionDeps(
if (options.verbose) {
console.error(`copy-production-deps: found ${context.length} packages`);
}
if (options.onlyPackages) {
rootDep.deps = rootDep.deps.filter((d) => options.onlyPackages?.includes(d.name));
console.error(`copy-production-deps: only bundling deps of ${options.onlyPackages}`);
}
const targetPackages = assignTargetDirs(context, rootDep);

const targetNodeModules = path.join(targetDir, "node_modules");
Expand Down

0 comments on commit da8261f

Please sign in to comment.