@magidoc/plugin-starter-variables 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +9 -0
- package/.vscode/launch.json +18 -0
- package/LICENSE +21 -0
- package/build/common/index.d.ts +5 -0
- package/build/common/index.js +9 -0
- package/build/common/index.js.map +1 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/build/index.js.map +1 -0
- package/build/variable.d.ts +18 -0
- package/build/variable.js +17 -0
- package/build/variable.js.map +1 -0
- package/jest.config.cjs +11 -0
- package/package.json +32 -0
- package/readme.md +2 -0
- package/rollup.config.cjs +14 -0
- package/src/common/index.ts +6 -0
- package/src/index.ts +3 -0
- package/src/variable.ts +53 -0
- package/tests/index.spec.ts +31 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +22 -0
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"type": "node",
|
|
6
|
+
"name": "vscode-jest-tests",
|
|
7
|
+
"request": "launch",
|
|
8
|
+
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
|
|
9
|
+
"args": [
|
|
10
|
+
"--runInBand"
|
|
11
|
+
],
|
|
12
|
+
"cwd": "${workspaceFolder}",
|
|
13
|
+
"console": "integratedTerminal",
|
|
14
|
+
"internalConsoleOptions": "neverOpen",
|
|
15
|
+
"disableOptimisticBPs": true,
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Sunny Pelletier
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createVariable, stringConversion } from '../variable.js';
|
|
2
|
+
|
|
3
|
+
var index = {
|
|
4
|
+
APP_LOGO: createVariable('APP_LOGO', stringConversion()),
|
|
5
|
+
APP_TITLE: createVariable('APP_TITLE', stringConversion()),
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export { index as default };
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/common/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA,YAAe;AACb,IAAA,QAAQ,EAAE,cAAc,CAAS,UAAU,EAAE,gBAAgB,EAAE,CAAC;AAChE,IAAA,SAAS,EAAE,cAAc,CAAS,WAAW,EAAE,gBAAgB,EAAE,CAAC;CACnE;;;;"}
|
package/build/index.d.ts
ADDED
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy of the ViteJS import meta interface
|
|
3
|
+
*/
|
|
4
|
+
interface MetaEnv {
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
export declare type Variable<T> = {
|
|
8
|
+
vite: ViteVariable<T>;
|
|
9
|
+
};
|
|
10
|
+
export declare type ViteVariable<T> = {
|
|
11
|
+
key: string;
|
|
12
|
+
get: (env: MetaEnv) => T | null;
|
|
13
|
+
getOrDefault: (env: MetaEnv, def: T) => T;
|
|
14
|
+
};
|
|
15
|
+
export declare function createVariable<T = string | boolean>(key: string, conversion: (target: unknown) => T): Variable<T>;
|
|
16
|
+
export declare function stringConversion(): (target: unknown) => string;
|
|
17
|
+
export declare function booleanConversion(): (target: unknown) => boolean;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function createVariable(key, conversion) {
|
|
2
|
+
const viteKey = `VITE_${key.toUpperCase()}`;
|
|
3
|
+
const viteGet = (env) => env[viteKey] !== undefined ? conversion(env[viteKey]) : null;
|
|
4
|
+
return {
|
|
5
|
+
vite: {
|
|
6
|
+
key: viteKey,
|
|
7
|
+
get: viteGet,
|
|
8
|
+
getOrDefault: (env, def) => viteGet(env) ?? def,
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function stringConversion() {
|
|
13
|
+
return (target) => String(target);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { createVariable, stringConversion };
|
|
17
|
+
//# sourceMappingURL=variable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variable.js","sources":["../../src/variable.ts"],"sourcesContent":[null],"names":[],"mappings":"AAiBgB,SAAA,cAAc,CAC5B,GAAW,EACX,UAAkC,EAAA;IAElC,MAAM,OAAO,GAAG,CAAQ,KAAA,EAAA,GAAG,CAAC,WAAW,EAAE,EAAE,CAAA;IAC3C,MAAM,OAAO,GAAG,CAAC,GAAY,KAC3B,GAAG,CAAC,OAAO,CAAC,KAAK,SAAS,GAAG,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAA;IAE9D,OAAO;AACL,QAAA,IAAI,EAAE;AACJ,YAAA,GAAG,EAAE,OAAO;AACZ,YAAA,GAAG,EAAE,OAAO;AACZ,YAAA,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,IAAI,GAAG;AAChD,SAAA;KACF,CAAA;AACH,CAAC;SAEe,gBAAgB,GAAA;IAC9B,OAAO,CAAC,MAAe,KAAa,MAAM,CAAC,MAAM,CAAC,CAAA;AACpD;;;;"}
|
package/jest.config.cjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@magidoc/plugin-starter-variables",
|
|
3
|
+
"description": "A shared library that contains common Magidoc starter environment variables keys.",
|
|
4
|
+
"private": false,
|
|
5
|
+
"version": "0.6.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "./build/index.js",
|
|
9
|
+
"types": "./build/index.d.ts",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"node",
|
|
12
|
+
"magidoc"
|
|
13
|
+
],
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@rollup/plugin-typescript": "^8.3.2",
|
|
16
|
+
"@types/jest": "^27.4.1",
|
|
17
|
+
"jest": "^27.5.1",
|
|
18
|
+
"rollup": "^2.70.2",
|
|
19
|
+
"ts-jest": "^27.1.4",
|
|
20
|
+
"tslib": "^2.3.1",
|
|
21
|
+
"typescript": "^4.6.3"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"start": "pnpm build:rollup && node build/index.js",
|
|
25
|
+
"build": "run-p build:*",
|
|
26
|
+
"build:rollup": "rollup -c",
|
|
27
|
+
"build:tsc": "tsc --build tsconfig.build.json",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"release": "pnpm publish --no-git-checks --access public"
|
|
30
|
+
},
|
|
31
|
+
"readme": "# Magidoc Starters Variables\nThis is an internal Magidoc shared library used for starters environment variables. You most likely never will need to use this library directly. \n"
|
|
32
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { defineConfig } = require('rollup')
|
|
2
|
+
const typescript = require('@rollup/plugin-typescript')
|
|
3
|
+
const shebang = require('rollup-plugin-preserve-shebang')
|
|
4
|
+
|
|
5
|
+
module.exports = defineConfig({
|
|
6
|
+
input: ['src/index.ts'],
|
|
7
|
+
plugins: [typescript()],
|
|
8
|
+
output: {
|
|
9
|
+
dir: 'build',
|
|
10
|
+
format: 'esm',
|
|
11
|
+
sourcemap: true,
|
|
12
|
+
preserveModules: true,
|
|
13
|
+
},
|
|
14
|
+
})
|
package/src/index.ts
ADDED
package/src/variable.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy of the ViteJS import meta interface
|
|
3
|
+
*/
|
|
4
|
+
interface MetaEnv {
|
|
5
|
+
[key: string]: unknown
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type Variable<T> = {
|
|
9
|
+
vite: ViteVariable<T>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type ViteVariable<T> = {
|
|
13
|
+
key: string
|
|
14
|
+
get: (env: MetaEnv) => T | null
|
|
15
|
+
getOrDefault: (env: MetaEnv, def: T) => T
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createVariable<T = string | boolean>(
|
|
19
|
+
key: string,
|
|
20
|
+
conversion: (target: unknown) => T,
|
|
21
|
+
): Variable<T> {
|
|
22
|
+
const viteKey = `VITE_${key.toUpperCase()}`
|
|
23
|
+
const viteGet = (env: MetaEnv) =>
|
|
24
|
+
env[viteKey] !== undefined ? conversion(env[viteKey]) : null
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
vite: {
|
|
28
|
+
key: viteKey,
|
|
29
|
+
get: viteGet,
|
|
30
|
+
getOrDefault: (env, def) => viteGet(env) ?? def,
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function stringConversion() {
|
|
36
|
+
return (target: unknown): string => String(target)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function booleanConversion() {
|
|
40
|
+
return (target: unknown): boolean => {
|
|
41
|
+
switch (typeof target) {
|
|
42
|
+
case 'boolean':
|
|
43
|
+
return target
|
|
44
|
+
case 'string':
|
|
45
|
+
const lowerCase = target.toLowerCase().trim()
|
|
46
|
+
return lowerCase === 'true' || lowerCase === 't' || lowerCase === '1'
|
|
47
|
+
case 'number':
|
|
48
|
+
return target !== 0
|
|
49
|
+
default:
|
|
50
|
+
return !!target
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as variables from '../src/index'
|
|
2
|
+
import type { Variable } from '../src/variable'
|
|
3
|
+
|
|
4
|
+
describe('variables', () => {
|
|
5
|
+
it('contains the right number of export keys', () => {
|
|
6
|
+
expect(Object.keys(variables)).toEqual(['common'])
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('contains the right common variables', () => {
|
|
10
|
+
expect(Object.keys(variables.common)).toEqual(['APP_LOGO', 'APP_TITLE'])
|
|
11
|
+
|
|
12
|
+
testStringVariable(variables.common.APP_TITLE, 'VITE_APP_TITLE')
|
|
13
|
+
testStringVariable(variables.common.APP_LOGO, 'VITE_APP_LOGO')
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
function testStringVariable(target: Variable<string>, viteKey: string) {
|
|
18
|
+
expect(target.vite.key).toEqual(viteKey)
|
|
19
|
+
|
|
20
|
+
expect(target.vite.get({ [viteKey]: 'Potato' })).toBe('Potato')
|
|
21
|
+
expect(target.vite.get({ [viteKey]: false })).toBe('false')
|
|
22
|
+
expect(target.vite.get({})).toBeNull()
|
|
23
|
+
|
|
24
|
+
expect(target.vite.getOrDefault({ [viteKey]: 'Potato' }, 'Default')).toBe(
|
|
25
|
+
'Potato',
|
|
26
|
+
)
|
|
27
|
+
expect(target.vite.getOrDefault({ [viteKey]: false }, 'Default')).toBe(
|
|
28
|
+
'false',
|
|
29
|
+
)
|
|
30
|
+
expect(target.vite.getOrDefault({}, 'Default')).toBe('Default')
|
|
31
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "esnext",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"importsNotUsedAsValues": "error",
|
|
6
|
+
"isolatedModules": true,
|
|
7
|
+
"module": "esnext",
|
|
8
|
+
"moduleResolution": "node",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"noImplicitAny": true,
|
|
11
|
+
"noImplicitThis": true,
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"sourceMap": true,
|
|
14
|
+
"outDir": "build",
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"esModuleInterop": true,
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"lib": ["esnext", "dom"]
|
|
19
|
+
},
|
|
20
|
+
"include": ["src/**/*.ts", "src/**/*.d.ts", "tests/**/*.ts"],
|
|
21
|
+
"exclude": ["build", "node_modules"]
|
|
22
|
+
}
|