Skip to content

Commit 003fb39

Browse files
committed
feat(#43): adds yargs package for parsing CLI parameters
1 parent fe8ff7d commit 003fb39

File tree

9 files changed

+129
-0
lines changed

9 files changed

+129
-0
lines changed

.github/workflows/publishing.yml

+6
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ jobs:
154154
token: ${{ secrets.NPM_TOKEN }}
155155
access: public
156156
package: ./app-config-webpack/package.json
157+
- name: app-config-yargs
158+
uses: JS-DevTools/npm-publish@v1
159+
with:
160+
token: ${{ secrets.NPM_TOKEN }}
161+
access: public
162+
package: ./app-config-yargs/package.json
157163
- name: lcdev-app-config-inject
158164
uses: JS-DevTools/npm-publish@v1
159165
with:

app-config-yargs/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@lcdev/eslint-config/cwd')(__dirname);

app-config-yargs/README.md

Whitespace-only changes.

app-config-yargs/package.json

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@app-config/yargs",
3+
"description": "Command line parsing for @app-config from yargs",
4+
"version": "2.4.4",
5+
"license": "MPL-2.0",
6+
"author": {
7+
"name": "Launchcode",
8+
"email": "[email protected]",
9+
"url": "https://lc.dev"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/launchcodedev/app-config.git"
14+
},
15+
"main": "dist/index.js",
16+
"module": "dist/es/index.js",
17+
"types": "dist/index.d.ts",
18+
"files": [
19+
"/dist",
20+
"!**/*.tsbuildinfo",
21+
"!**/*.test.*"
22+
],
23+
"scripts": {
24+
"build": "tsc -b",
25+
"build:es": "tsc -b tsconfig.es.json",
26+
"clean": "rm -rf dist *.tsbuildinfo",
27+
"lint": "eslint src",
28+
"fix": "eslint --fix src",
29+
"test": "jest",
30+
"prepublishOnly": "yarn clean && yarn build && yarn build:es"
31+
},
32+
"dependencies": {
33+
"@app-config/core": "^2.4.4",
34+
"@app-config/extension-utils": "^2.4.4",
35+
"@app-config/utils": "^2.4.4",
36+
"yargs": "16"
37+
},
38+
"devDependencies": {
39+
"@app-config/test-utils": "^2.4.4"
40+
},
41+
"prettier": "@lcdev/prettier",
42+
"jest": {
43+
"preset": "@lcdev/jest"
44+
}
45+
}

app-config-yargs/src/index.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import yargs from 'yargs';
2+
import { defaultEnvExtensions, defaultExtensions, loadUnvalidatedConfig } from '@app-config/main';
3+
import yargsParsingExtension from './index';
4+
5+
const makeOptions = (argv: string[]) => ({
6+
environmentExtensions: defaultEnvExtensions().concat(yargsParsingExtension({ argv })),
7+
parsingExtensions: defaultExtensions().concat(yargsParsingExtension({ argv })),
8+
});
9+
10+
beforeEach(() => {
11+
yargs.reset();
12+
});
13+
14+
describe('yargsParsingExtension', () => {
15+
it('loads an argument', async () => {
16+
process.env.APP_CONFIG = `
17+
foo:
18+
$yargs: foo
19+
`;
20+
21+
const { fullConfig } = await loadUnvalidatedConfig(makeOptions(['--foo=bar']));
22+
23+
expect(fullConfig).toEqual({ foo: 'bar' });
24+
});
25+
});

app-config-yargs/src/index.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { forKey, validateOptions } from '@app-config/extension-utils';
2+
import { ParsingExtension, AppConfigError } from '@app-config/core';
3+
import { Json } from '@app-config/utils';
4+
import yargs from 'yargs';
5+
6+
interface Options {
7+
argv?: string[];
8+
}
9+
10+
function yargsParsingExtension({
11+
argv = process.argv,
12+
}: Options = {}): ParsingExtension {
13+
const args = yargs.parse(argv);
14+
15+
return forKey(
16+
'$yargs',
17+
validateOptions(
18+
(SchemaBuilder) => SchemaBuilder.stringSchema(),
19+
(value) => async (parse) => {
20+
if (value in args) {
21+
return parse(args[value] as Json, { shouldFlatten: true });
22+
}
23+
24+
throw new AppConfigError(`Tried to use CLI arg named '${value}' but it was not found`);
25+
},
26+
),
27+
);
28+
}
29+
30+
export default yargsParsingExtension;

app-config-yargs/tsconfig.es.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"target": "es2020",
5+
"module": "es2020",
6+
"outDir": "./dist/es"
7+
}
8+
}

app-config-yargs/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "@lcdev/tsconfig",
3+
"compilerOptions": {
4+
"rootDir": "./src",
5+
"outDir": "./dist"
6+
},
7+
"include": ["src"],
8+
"exclude": ["node_modules"],
9+
"references": [
10+
{ "path": "../app-config-test-utils" },
11+
{ "path": "../app-config-extension-utils" }
12+
]
13+
}

new-version.sh

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ new_version app-config-utils
6262
new_version app-config-v1-compat
6363
new_version app-config-vault
6464
new_version app-config-webpack
65+
new_version app-config-yargs
6566
new_version lcdev-app-config
6667
new_version lcdev-app-config-inject
6768
new_version lcdev-app-config-webpack-plugin

0 commit comments

Comments
 (0)