@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 ADDED
@@ -0,0 +1,9 @@
1
+ const base = require('../../../.eslintrc.cjs')
2
+
3
+ module.exports = {
4
+ ...base,
5
+ parserOptions: {
6
+ ...base.parserOptions,
7
+ project: [__dirname + '/tsconfig.json'],
8
+ },
9
+ }
@@ -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,5 @@
1
+ declare const _default: {
2
+ APP_LOGO: import("../variable").Variable<string>;
3
+ APP_TITLE: import("../variable").Variable<string>;
4
+ };
5
+ export default _default;
@@ -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;;;;"}
@@ -0,0 +1,2 @@
1
+ import common from './common';
2
+ export { common };
package/build/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { default as common } from './common/index.js';
2
+ //# sourceMappingURL=index.js.map
@@ -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;;;;"}
@@ -0,0 +1,11 @@
1
+ const base = require('../../../jest.config.cjs')
2
+
3
+ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
4
+ module.exports = {
5
+ ...base,
6
+ globals: {
7
+ 'ts-jest': {
8
+ tsconfig: 'tsconfig.json',
9
+ },
10
+ }
11
+ }
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,2 @@
1
+ # Magidoc Starters Variables
2
+ This is an internal Magidoc shared library used for starters environment variables. You most likely never will need to use this library directly.
@@ -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
+ })
@@ -0,0 +1,6 @@
1
+ import { createVariable, stringConversion } from '../variable'
2
+
3
+ export default {
4
+ APP_LOGO: createVariable<string>('APP_LOGO', stringConversion()),
5
+ APP_TITLE: createVariable<string>('APP_TITLE', stringConversion()),
6
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import common from './common'
2
+
3
+ export { common }
@@ -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
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "emitDeclarationOnly": true,
6
+ "outDir": "build"
7
+ },
8
+ "include": ["src/**/*.ts", "src/**/*.d.ts"]
9
+ }
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
+ }