Skip to content

Commit

Permalink
Merge pull request #78 from LironEr/feat/override-flags
Browse files Browse the repository at this point in the history
feat: override subProject and defaultCompression with CLI
  • Loading branch information
LironEr authored Dec 18, 2021
2 parents 4e9e223 + 284b881 commit 2dc9537
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
8 changes: 5 additions & 3 deletions docs/cli-flags.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CLI flags

| Flag | Description | Type |
| ------------ | ------------------------------------------------------------------------------------------- | ------------------ |
| -c, --config | Config file path | `string` optional |
| Flag | Description | Type |
| -------------------- | ------------------------------------------ | ------------------------------------------- |
| -c, --config | Config file path | `string` optional |
| --subProject | Override `subProject` config value | `string` optional |
| --defaultCompression | Override `defaultCompression` config value | `"none"` \| `"gzip"` \| `"brotli"` optional |
1 change: 1 addition & 0 deletions packages/bundlemon-markdown-output/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"module": "./lib/esm/index.js",
"types": "./lib/esm/index.d.ts",
"scripts": {
"test": "echo no tests",
"build": "rimraf lib/ && tsc -p tsconfig.release.json && tsc -p tsconfig-cjs.json",
"prepublishOnly": "yarn test && yarn lint && yarn build",
"lint": "yarn eslint --config ../../.eslintrc.json --max-warnings=0 \"src/**/*.ts\"",
Expand Down
32 changes: 27 additions & 5 deletions packages/bundlemon/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { program } from 'commander';
import { Status } from 'bundlemon-utils';
import { program, Option } from 'commander';
import { Compression, Status } from 'bundlemon-utils';
import bundlemon from '../main';
import logger from '../common/logger';
import { version } from '../common/consts';
import type { CliOptions } from './types';
import { loadConfigFile } from './configFile';

program.version(version).option('-c, --config [path]', 'Config file path');
import type { CliOptions } from './types';
import type { Config } from '../main/types';

program
.version(version)
.addOption(new Option('-c, --config <path>', 'config file path'))
.addOption(new Option('--subProject <name>', 'sub project name'))
.addOption(
new Option('--defaultCompression <compression>', 'default compression').choices(Object.values(Compression))
);

export default async (): Promise<void> => {
try {
Expand All @@ -21,11 +29,25 @@ export default async (): Promise<void> => {
process.exit(1);
}

const report = await bundlemon(config);
const report = await bundlemon(mergeCliOptions(config, options));

process.exit(report.status === Status.Pass ? 0 : 1);
} catch (err) {
logger.error('Unhandled error', err);
process.exit(1);
}
};

function mergeCliOptions(config: Config, options: CliOptions): Config {
const newConfig = { ...config };

if (options.subProject) {
newConfig.subProject = options.subProject;
}

if (options.defaultCompression) {
newConfig.defaultCompression = options.defaultCompression;
}

return newConfig;
}
4 changes: 4 additions & 0 deletions packages/bundlemon/src/cli/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { Compression } from 'bundlemon-utils';

export interface CliOptions {
config?: string;
subProject?: string;
defaultCompression?: Compression;
}

0 comments on commit 2dc9537

Please sign in to comment.