Skip to content

Commit

Permalink
Release v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Jul 10, 2023
1 parent b6f3e98 commit 5512270
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cli-argv-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const cliArgvUtil = {
},

run(packageJson: { [key: string]: unknown }, posix: string) {
// Example usage:
// const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
// it('executing from the CLI copies the correct files', () => {
// cliArgvUtil.run(pkg, 'copy-folder source build');
const name = Object.keys(<string[]>packageJson.bin).sort()[0]!;
const command = process.platform === 'win32' ? posix.replaceAll('\\ ', '" "') : posix;
return execSync(command.replace(name, 'node bin/cli.js'), { stdio: 'inherit' });
Expand Down
5 changes: 4 additions & 1 deletion dist/cli-argv-util.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! cli-argv-util v1.1.0 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
//! cli-argv-util v1.2.0 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License

export type StringFlagMap = {
[flag: string]: string | undefined;
Expand All @@ -16,5 +16,8 @@ export type Result = {
};
declare const cliArgvUtil: {
parse(validFlags: string[]): Result;
run(packageJson: {
[key: string]: unknown;
}, posix: string): Buffer;
};
export { cliArgvUtil };
11 changes: 8 additions & 3 deletions dist/cli-argv-util.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! cli-argv-util v1.1.0 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
//! cli-argv-util v1.2.0 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License

import { execSync } from 'node:child_process';
const cliArgvUtil = {
parse(validFlags) {
var _a, _b;
const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
const toPair = (flag) => flag.replace(/^--/, '').split('=');
const args = process.argv.slice(2);
const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
const flagMap = Object.fromEntries(pairs.map(toEntry));
const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
const invalidFlag = (_b = (_a = pairs.find(pair => !validFlags.includes(pair[0]))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
const invalidFlag = pairs.find(pair => !validFlags.includes(pair[0]))?.[0] ?? null;
const helpMsg = '\nValid flags are --' + validFlags.join(' --');
const params = args.filter(arg => !/^--/.test(arg));
return {
Expand All @@ -22,5 +22,10 @@ const cliArgvUtil = {
paramCount: params.length,
};
},
run(packageJson, posix) {
const name = Object.keys(packageJson.bin).sort()[0];
const command = process.platform === 'win32' ? posix.replaceAll('\\ ', '" "') : posix;
return execSync(command.replace(name, 'node bin/cli.js'), { stdio: 'inherit' });
},
};
export { cliArgvUtil };
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cli-argv-util",
"version": "1.1.0",
"version": "1.2.0",
"description": "Simple utility to parse command line parameters and flags (arguments vector)",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -73,8 +73,8 @@
},
"devDependencies": {
"@types/node": "~20.4",
"@typescript-eslint/eslint-plugin": "~5.61",
"@typescript-eslint/parser": "~5.61",
"@typescript-eslint/eslint-plugin": "~6.0",
"@typescript-eslint/parser": "~6.0",
"add-dist-header": "~1.1",
"assert-deep-strict-equal": "~1.1",
"copy-file-util": "~1.1",
Expand Down

0 comments on commit 5512270

Please sign in to comment.