Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { bundle } from '.';
const options = yargs
.usage('Usage: $0 [options]')
.example('$0', 'Bundle from `build` to `lambda`')
.example('$0 -f', 'Bundle from `build` to `lambda`, first removing `lambda` if it exists.')
.example('$0 -o code -vv', 'Bundle from `build` to `code`, very verbosely')
.example('$0 -i dist -o package', 'Bundle from `dist` to `package`')
.example('$0 -e aws-sdk other-pkg', 'Bundle, excluding `aws-sdk` and `other-pkg`')
Expand Down Expand Up @@ -37,6 +38,8 @@ const options = yargs
.array('e')
.alias('e', 'exclude-packages')
.default('e', ['aws-sdk'])
.describe('f', 'Force mode, which deletes the output directory before running')
.default('f', false)
.describe('v', 'Enable verbose output (multiple v for more)')
.count('v')
.alias('v', 'verbose')
Expand All @@ -48,12 +51,14 @@ const {
i: inputDir,
o: outputDir,
e: excludePackages,
f: force,
v: verbosity,
} = options;

bundle({
inputDir,
outputDir,
excludePackages,
force,
verbosity,
});
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ interface BundleParams {
outputDir: string;
/** Packages to exclude from bundling */
excludePackages: string[];
/** Whether to force deletion of the output directory */
force: boolean;
/** 0 means quiet, higher numbers are more verbose */
verbosity: number;
}
Expand All @@ -95,6 +97,7 @@ export const bundle = ({
inputDir,
outputDir,
excludePackages,
force,
verbosity,
}: BundleParams): void => {
if (!fs.existsSync(inputDir)) {
Expand All @@ -105,7 +108,20 @@ export const bundle = ({
}

if (fs.existsSync(outputDir)) {
throw new Error(`Output directory ${outputDir} already exists`);
if (force) {
if (verbosity > 0) {
console.log(`Removing directory ${outputDir}`);
}
fs.rmdirSync(outputDir, { recursive: true });
if (verbosity > 0) {
console.log(`Directory ${outputDir} removed`);
}

} else {
throw new Error(`Output directory ${outputDir} already exists`);
}
} else {
console.log(`Output directory ${outputDir} does not already exist`);
}

console.log(`Bundling ${inputDir} and dependencies to ${outputDir}`);
Expand Down