Skip to content

Commit bb55d45

Browse files
committed
feat(#103): adds $jsModule parsing extension package
1 parent 7167a5c commit bb55d45

File tree

9 files changed

+190
-0
lines changed

9 files changed

+190
-0
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
./app-config-generate/coverage/coverage-final.json,
4242
./app-config-git/coverage/coverage-final.json,
4343
./app-config-inject/coverage/coverage-final.json,
44+
./app-config-js/coverage/coverage-final.json,
4445
./app-config-logging/coverage/coverage-final.json,
4546
./app-config-main/coverage/coverage-final.json,
4647
./app-config-meta/coverage/coverage-final.json,

.github/workflows/publishing.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ jobs:
7676
token: ${{ secrets.NPM_TOKEN }}
7777
access: public
7878
package: ./app-config-inject/package.json
79+
- uses: JS-DevTools/npm-publish@v1
80+
with:
81+
token: ${{ secrets.NPM_TOKEN }}
82+
access: public
83+
package: ./app-config-js/package.json
7984
- uses: JS-DevTools/npm-publish@v1
8085
with:
8186
token: ${{ secrets.NPM_TOKEN }}

app-config-js/.eslintrc.js

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

app-config-js/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## App Config JS Module Loader
2+
3+
This is a parsing extension to load arbitrary JavaScript code.
4+
5+
Add to meta file:
6+
7+
```yaml
8+
parsingExtensions:
9+
- @app-config/js
10+
```
11+
12+
The API of this module is documented via TypeScript definitions.
13+
14+
Read the [Introduction](https://app-config.dev/guide/intro/) or
15+
[Quick Start](https://app-config.dev/guide/intro/quick-start/) guides on our website.

app-config-js/package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@app-config/js",
3+
"description": "Loads a JavaScript module to inject configuration",
4+
"version": "2.2.0",
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.2.0",
34+
"@app-config/extension-utils": "^2.2.0"
35+
},
36+
"devDependencies": {
37+
"@app-config/test-utils": "^2.2.0"
38+
},
39+
"prettier": "@lcdev/prettier",
40+
"jest": {
41+
"preset": "@lcdev/jest"
42+
}
43+
}

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { LiteralSource } from '@app-config/core';
2+
import { withTempFiles } from '@app-config/test-utils';
3+
import jsModuleDirective from './index';
4+
5+
describe('$jsModule directive', () => {
6+
it('loads function node export', () =>
7+
withTempFiles(
8+
{
9+
'foo.js': `
10+
module.exports = () => 'bar';
11+
`,
12+
},
13+
async (inDir) => {
14+
const source = new LiteralSource({
15+
$jsModule: inDir('foo.js'),
16+
});
17+
18+
expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar');
19+
},
20+
));
21+
22+
it('loads default function node export', () =>
23+
withTempFiles(
24+
{
25+
'foo.js': `
26+
module.exports.__esModule = true;
27+
module.exports.default = () => 'bar';
28+
`,
29+
},
30+
async (inDir) => {
31+
const source = new LiteralSource({
32+
$jsModule: inDir('foo.js'),
33+
});
34+
35+
expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar');
36+
},
37+
));
38+
39+
it('loads object node export', () =>
40+
withTempFiles(
41+
{
42+
'foo.js': `
43+
module.exports = 'bar';
44+
`,
45+
},
46+
async (inDir) => {
47+
const source = new LiteralSource({
48+
$jsModule: inDir('foo.js'),
49+
});
50+
51+
expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar');
52+
},
53+
));
54+
55+
it('loads default object node export', () =>
56+
withTempFiles(
57+
{
58+
'foo.js': `
59+
module.exports.__esModule = true;
60+
module.exports.default = 'bar';
61+
`,
62+
},
63+
async (inDir) => {
64+
const source = new LiteralSource({
65+
$jsModule: inDir('foo.js'),
66+
});
67+
68+
expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar');
69+
},
70+
));
71+
});

app-config-js/src/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ParsingExtension } from '@app-config/core';
2+
import { forKey, validateOptions } from '@app-config/extension-utils';
3+
4+
/* eslint-disable @typescript-eslint/no-unsafe-call */
5+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
6+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
7+
8+
export default function jsModuleDirective(): ParsingExtension {
9+
return forKey(
10+
'$jsModule',
11+
validateOptions(
12+
(SchemaBuilder) => SchemaBuilder.stringSchema(),
13+
(value) => async (parse) => {
14+
let loaded: any = await import(value);
15+
16+
if (!loaded) {
17+
return parse(loaded, { shouldFlatten: true });
18+
}
19+
20+
if ('default' in loaded) {
21+
loaded = loaded.default;
22+
}
23+
24+
if (typeof loaded === 'function') {
25+
loaded = loaded();
26+
}
27+
28+
return parse(loaded, { shouldFlatten: true });
29+
},
30+
),
31+
);
32+
}

app-config-js/tsconfig.es.json

Lines changed: 8 additions & 0 deletions
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-js/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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-core" },
12+
{ "path": "../app-config-extension-utils" }
13+
]
14+
}

0 commit comments

Comments
 (0)