@magidoc/plugin-starter-variables 1.1.1 → 2.0.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/build/env/envFileContent.d.ts +6 -0
- package/build/env/envFileContent.js +45 -0
- package/build/env/envFileContent.js.map +1 -0
- package/build/index.d.ts +4 -2
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/variables/variable.d.ts +3 -9
- package/build/variables/variable.js +5 -8
- package/build/variables/variable.js.map +1 -1
- package/package.json +8 -5
- package/rollup.config.cjs +1 -1
- package/src/env/envFileContent.ts +60 -0
- package/src/index.ts +7 -2
- package/src/variables/variable.ts +8 -18
- package/tests/env/envFileContent.spec.ts +54 -0
- package/tests/index.spec.ts +70 -74
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Variable } from '../variables/variable';
|
|
2
|
+
export declare function toVariablesFile(options: Record<string, unknown>, supportedVariables: ReadonlyArray<Variable<unknown>>): string;
|
|
3
|
+
export declare class UnsupportedVariablesError extends Error {
|
|
4
|
+
unsupportedVariables: string[];
|
|
5
|
+
constructor(message: string, unsupportedVariables: string[]);
|
|
6
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import magidoc from '../variables/magidoc.js';
|
|
3
|
+
|
|
4
|
+
function toVariablesFile(options, supportedVariables) {
|
|
5
|
+
return asVariablesString(buildEnv(options, supportedVariables));
|
|
6
|
+
}
|
|
7
|
+
function asVariablesString(env) {
|
|
8
|
+
return JSON.stringify(env);
|
|
9
|
+
}
|
|
10
|
+
function buildEnv(options, supportedVariables) {
|
|
11
|
+
let newRecord = {};
|
|
12
|
+
const unsupportedVariables = [];
|
|
13
|
+
_.forEach(options, (value, key) => {
|
|
14
|
+
const variable = supportedVariables.find((option) => option.name === key);
|
|
15
|
+
if (!variable) {
|
|
16
|
+
unsupportedVariables.push(key);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
newRecord = {
|
|
20
|
+
...newRecord,
|
|
21
|
+
...variable.asEnv(value),
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
if (unsupportedVariables.length > 0) {
|
|
25
|
+
throw new UnsupportedVariablesError(`Options [${unsupportedVariables.toString()}] are not supported... Supported option names are [${supportedVariables
|
|
26
|
+
.map((value) => value.name)
|
|
27
|
+
.join(', ')}]`, unsupportedVariables);
|
|
28
|
+
}
|
|
29
|
+
insertDefaultVariables(newRecord);
|
|
30
|
+
return newRecord;
|
|
31
|
+
}
|
|
32
|
+
function insertDefaultVariables(newRecord) {
|
|
33
|
+
newRecord[magidoc.MAGIDOC_GENERATE.key] = 'true';
|
|
34
|
+
}
|
|
35
|
+
class UnsupportedVariablesError extends Error {
|
|
36
|
+
unsupportedVariables;
|
|
37
|
+
constructor(message, unsupportedVariables) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.unsupportedVariables = unsupportedVariables;
|
|
40
|
+
this.name = 'UnsupportedVariableError';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { UnsupportedVariablesError, toVariablesFile };
|
|
45
|
+
//# sourceMappingURL=envFileContent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envFileContent.js","sources":["../../../src/env/envFileContent.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIgB,SAAA,eAAe,CAC7B,OAAgC,EAChC,kBAAoD,EAAA;IAEpD,OAAO,iBAAiB,CAAC,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAA;AACjE,CAAC;AAED,SAAS,iBAAiB,CAAC,GAA2B,EAAA;AACpD,IAAA,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;AAC5B,CAAC;AAED,SAAS,QAAQ,CACf,OAAgC,EAChC,kBAAoD,EAAA;IAEpD,IAAI,SAAS,GAA2B,EAAE,CAAA;IAC1C,MAAM,oBAAoB,GAAa,EAAE,CAAA;IACzC,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,KAAI;AAChC,QAAA,MAAM,QAAQ,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,KAAK,GAAG,CAAC,CAAA;QACzE,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,oBAAoB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YAC9B,OAAM;AACP,SAAA;AAED,QAAA,SAAS,GAAG;AACV,YAAA,GAAG,SAAS;AACZ,YAAA,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC;SACzB,CAAA;AACH,KAAC,CAAC,CAAA;AAEF,IAAA,IAAI,oBAAoB,CAAC,MAAM,GAAG,CAAC,EAAE;QACnC,MAAM,IAAI,yBAAyB,CACjC,CAAA,SAAA,EAAY,oBAAoB,CAAC,QAAQ,EAAE,CAAA,mDAAA,EAAsD,kBAAkB;aAChH,GAAG,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,IAAI,CAAC;AAC1B,aAAA,IAAI,CAAC,IAAI,CAAC,GAAG,EAChB,oBAAoB,CACrB,CAAA;AACF,KAAA;IAED,sBAAsB,CAAC,SAAS,CAAC,CAAA;AACjC,IAAA,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,SAAS,sBAAsB,CAAC,SAAiC,EAAA;IAC/D,SAAS,CAAC,OAAO,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,MAAM,CAAA;AAClD,CAAC;AAEK,MAAO,yBAA0B,SAAQ,KAAK,CAAA;AAC3C,IAAA,oBAAoB,CAAU;IAErC,WAAY,CAAA,OAAe,EAAE,oBAA8B,EAAA;QACzD,KAAK,CAAC,OAAO,CAAC,CAAA;AACd,QAAA,IAAI,CAAC,oBAAoB,GAAG,oBAAoB,CAAA;AAChD,QAAA,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAA;KACvC;AACF;;;;"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import templates from './variables/templates';
|
|
2
2
|
import magidoc from './variables/magidoc';
|
|
3
|
-
import
|
|
3
|
+
import { toVariablesFile, UnsupportedVariablesError } from './env/envFileContent';
|
|
4
|
+
import type { Variable } from './variables/variable';
|
|
4
5
|
import type { Page } from './variables/templates';
|
|
5
6
|
export { templates };
|
|
6
7
|
export { magidoc };
|
|
7
|
-
export type { Variable,
|
|
8
|
+
export type { Variable, Page };
|
|
9
|
+
export { toVariablesFile, UnsupportedVariablesError };
|
package/build/index.js
CHANGED
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import type { ZodType, ZodTypeDef } from 'zod';
|
|
2
|
-
/**
|
|
3
|
-
* Copy of the ViteJS import meta interface
|
|
4
|
-
*/
|
|
5
2
|
interface MetaEnv {
|
|
6
3
|
[key: string]: unknown;
|
|
7
4
|
}
|
|
8
5
|
export declare type Variable<T> = {
|
|
9
6
|
name: string;
|
|
7
|
+
key: string;
|
|
10
8
|
asEnv: (value: T) => Record<string, string>;
|
|
9
|
+
get: (env: MetaEnv) => T | null;
|
|
10
|
+
getOrDefault: (env: MetaEnv, def: T) => T;
|
|
11
11
|
zod: ZodVariable<T>;
|
|
12
|
-
vite: ViteVariable<T>;
|
|
13
12
|
};
|
|
14
13
|
export declare type ZodTypeProvider<T> = (zod: typeof import('zod')) => ZodType<T | undefined, ZodTypeDef, T | undefined>;
|
|
15
14
|
export declare type ZodVariable<T> = {
|
|
@@ -20,10 +19,5 @@ export declare type Converter<T> = {
|
|
|
20
19
|
asString: (value: T) => string;
|
|
21
20
|
type: ZodTypeProvider<T>;
|
|
22
21
|
};
|
|
23
|
-
export declare type ViteVariable<T> = {
|
|
24
|
-
key: string;
|
|
25
|
-
get: (env: MetaEnv) => T | null;
|
|
26
|
-
getOrDefault: (env: MetaEnv, def: T) => T;
|
|
27
|
-
};
|
|
28
22
|
export declare function createVariable<T>(key: string, converter: Converter<T>): Variable<T>;
|
|
29
23
|
export {};
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
function createVariable(key, converter) {
|
|
2
|
-
const
|
|
3
|
-
const viteGet = (env) => converter.convert(env[viteKey]);
|
|
2
|
+
const get = (env) => converter.convert(env[key]);
|
|
4
3
|
return {
|
|
5
4
|
name: toCamelCase(key),
|
|
6
|
-
|
|
5
|
+
key: key,
|
|
6
|
+
asEnv: (value) => ({ [key]: converter.asString(value) }),
|
|
7
7
|
zod: {
|
|
8
8
|
type: converter.type,
|
|
9
9
|
},
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
get: viteGet,
|
|
13
|
-
getOrDefault: (env, def) => viteGet(env) ?? def,
|
|
14
|
-
},
|
|
10
|
+
get: get,
|
|
11
|
+
getOrDefault: (env, def) => get(env) ?? def,
|
|
15
12
|
};
|
|
16
13
|
}
|
|
17
14
|
function toCamelCase(key) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"variable.js","sources":["../../../src/variables/variable.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"variable.js","sources":["../../../src/variables/variable.ts"],"sourcesContent":[null],"names":[],"mappings":"AA6BgB,SAAA,cAAc,CAC5B,GAAW,EACX,SAAuB,EAAA;AAEvB,IAAA,MAAM,GAAG,GAAG,CAAC,GAAY,KAAK,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;IAEzD,OAAO;AACL,QAAA,IAAI,EAAE,WAAW,CAAC,GAAG,CAAC;AACtB,QAAA,GAAG,EAAE,GAAG;AACR,QAAA,KAAK,EAAE,CAAC,KAAQ,MAAM,EAAE,CAAC,GAAG,GAAG,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;AAC3D,QAAA,GAAG,EAAE;YACH,IAAI,EAAE,SAAS,CAAC,IAAI;AACrB,SAAA;AACD,QAAA,GAAG,EAAE,GAAG;AACR,QAAA,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,IAAI,GAAG;KAC5C,CAAA;AACH,CAAC;AAED,SAAS,WAAW,CAAC,GAAW,EAAA;AAC9B,IAAA,OAAO,GAAG,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC,EAAE,KAAI;AACvD,QAAA,OAAO,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;AAC3D,KAAC,CAAC,CAAA;AACJ;;;;"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@magidoc/plugin-starter-variables",
|
|
3
3
|
"description": "A shared library that contains common Magidoc starter environment variables keys.",
|
|
4
4
|
"private": false,
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "2.0.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"main": "./build/index.js",
|
|
@@ -19,12 +19,15 @@
|
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@rollup/plugin-typescript": "^8.3.2",
|
|
22
|
-
"@types/jest": "^
|
|
22
|
+
"@types/jest": "^28.1.1",
|
|
23
|
+
"@types/lodash": "^4.14.182",
|
|
24
|
+
"@types/node": "^17.0.40",
|
|
23
25
|
"jest": "^28.1.0",
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
+
"lodash": "^4.17.21",
|
|
27
|
+
"rollup": "^2.75.5",
|
|
28
|
+
"ts-jest": "^28.0.4",
|
|
26
29
|
"tslib": "^2.4.0",
|
|
27
|
-
"typescript": "^4.7.
|
|
30
|
+
"typescript": "^4.7.3"
|
|
28
31
|
},
|
|
29
32
|
"optionalDependencies": {
|
|
30
33
|
"zod": "^3.17.3"
|
package/rollup.config.cjs
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import _ from 'lodash'
|
|
2
|
+
import magidoc from '../variables/magidoc'
|
|
3
|
+
import type { Variable } from '../variables/variable'
|
|
4
|
+
|
|
5
|
+
export function toVariablesFile(
|
|
6
|
+
options: Record<string, unknown>,
|
|
7
|
+
supportedVariables: ReadonlyArray<Variable<unknown>>,
|
|
8
|
+
): string {
|
|
9
|
+
return asVariablesString(buildEnv(options, supportedVariables))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function asVariablesString(env: Record<string, string>) {
|
|
13
|
+
return JSON.stringify(env)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function buildEnv(
|
|
17
|
+
options: Record<string, unknown>,
|
|
18
|
+
supportedVariables: ReadonlyArray<Variable<unknown>>,
|
|
19
|
+
): Record<string, string> {
|
|
20
|
+
let newRecord: Record<string, string> = {}
|
|
21
|
+
const unsupportedVariables: string[] = []
|
|
22
|
+
_.forEach(options, (value, key) => {
|
|
23
|
+
const variable = supportedVariables.find((option) => option.name === key)
|
|
24
|
+
if (!variable) {
|
|
25
|
+
unsupportedVariables.push(key)
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
newRecord = {
|
|
30
|
+
...newRecord,
|
|
31
|
+
...variable.asEnv(value),
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
if (unsupportedVariables.length > 0) {
|
|
36
|
+
throw new UnsupportedVariablesError(
|
|
37
|
+
`Options [${unsupportedVariables.toString()}] are not supported... Supported option names are [${supportedVariables
|
|
38
|
+
.map((value) => value.name)
|
|
39
|
+
.join(', ')}]`,
|
|
40
|
+
unsupportedVariables,
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
insertDefaultVariables(newRecord)
|
|
45
|
+
return newRecord
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function insertDefaultVariables(newRecord: Record<string, string>) {
|
|
49
|
+
newRecord[magidoc.MAGIDOC_GENERATE.key] = 'true'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class UnsupportedVariablesError extends Error {
|
|
53
|
+
public unsupportedVariables: string[]
|
|
54
|
+
|
|
55
|
+
constructor(message: string, unsupportedVariables: string[]) {
|
|
56
|
+
super(message)
|
|
57
|
+
this.unsupportedVariables = unsupportedVariables
|
|
58
|
+
this.name = 'UnsupportedVariableError'
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import templates from './variables/templates'
|
|
2
2
|
import magidoc from './variables/magidoc'
|
|
3
|
-
import
|
|
3
|
+
import {
|
|
4
|
+
toVariablesFile,
|
|
5
|
+
UnsupportedVariablesError,
|
|
6
|
+
} from './env/envFileContent'
|
|
7
|
+
import type { Variable } from './variables/variable'
|
|
4
8
|
import type { Page } from './variables/templates'
|
|
5
9
|
|
|
6
10
|
export { templates }
|
|
7
11
|
export { magidoc }
|
|
8
|
-
export type { Variable,
|
|
12
|
+
export type { Variable, Page }
|
|
13
|
+
export { toVariablesFile, UnsupportedVariablesError }
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import type { ZodType, ZodTypeDef } from 'zod'
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Copy of the ViteJS import meta interface
|
|
5
|
-
*/
|
|
6
3
|
interface MetaEnv {
|
|
7
4
|
[key: string]: unknown
|
|
8
5
|
}
|
|
9
6
|
|
|
10
7
|
export type Variable<T> = {
|
|
11
8
|
name: string
|
|
9
|
+
key: string
|
|
12
10
|
asEnv: (value: T) => Record<string, string>
|
|
11
|
+
get: (env: MetaEnv) => T | null
|
|
12
|
+
getOrDefault: (env: MetaEnv, def: T) => T
|
|
13
13
|
zod: ZodVariable<T>
|
|
14
|
-
vite: ViteVariable<T>
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
export type ZodTypeProvider<T> = (
|
|
@@ -28,30 +27,21 @@ export type Converter<T> = {
|
|
|
28
27
|
type: ZodTypeProvider<T>
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
export type ViteVariable<T> = {
|
|
32
|
-
key: string
|
|
33
|
-
get: (env: MetaEnv) => T | null
|
|
34
|
-
getOrDefault: (env: MetaEnv, def: T) => T
|
|
35
|
-
}
|
|
36
|
-
|
|
37
30
|
export function createVariable<T>(
|
|
38
31
|
key: string,
|
|
39
32
|
converter: Converter<T>,
|
|
40
33
|
): Variable<T> {
|
|
41
|
-
const
|
|
42
|
-
const viteGet = (env: MetaEnv) => converter.convert(env[viteKey])
|
|
34
|
+
const get = (env: MetaEnv) => converter.convert(env[key])
|
|
43
35
|
|
|
44
36
|
return {
|
|
45
37
|
name: toCamelCase(key),
|
|
46
|
-
|
|
38
|
+
key: key,
|
|
39
|
+
asEnv: (value: T) => ({ [key]: converter.asString(value) }),
|
|
47
40
|
zod: {
|
|
48
41
|
type: converter.type,
|
|
49
42
|
},
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
get: viteGet,
|
|
53
|
-
getOrDefault: (env, def) => viteGet(env) ?? def,
|
|
54
|
-
},
|
|
43
|
+
get: get,
|
|
44
|
+
getOrDefault: (env, def) => get(env) ?? def,
|
|
55
45
|
}
|
|
56
46
|
}
|
|
57
47
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { templates, Variable, magidoc, toVariablesFile } from '../../src'
|
|
2
|
+
import { UnsupportedVariablesError } from '../../src/env/envFileContent'
|
|
3
|
+
|
|
4
|
+
describe('creating a variables file ', () => {
|
|
5
|
+
const initialObject = {
|
|
6
|
+
appTitle: 'My App',
|
|
7
|
+
pages: [
|
|
8
|
+
{
|
|
9
|
+
title: 'My-page-title',
|
|
10
|
+
content:
|
|
11
|
+
'A raw markdown string containing stuff like "double quotes", `code`, and **bold**.',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
title: 'My-page-title-2',
|
|
15
|
+
content: [
|
|
16
|
+
{
|
|
17
|
+
title:
|
|
18
|
+
"This is another page, but this one contains 'single quotes', ```much more code```,\n new lines \n\n # And a new header apparently",
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('all provided options are supported', () => {
|
|
26
|
+
const result = toVariablesFile(initialObject, [
|
|
27
|
+
templates.APP_TITLE as Variable<unknown>,
|
|
28
|
+
templates.PAGES as Variable<unknown>,
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
it('is possible to convert it back to the original object using variables', () => {
|
|
32
|
+
const parsed = JSON.parse(result) as Record<string, string>
|
|
33
|
+
|
|
34
|
+
expect(magidoc.MAGIDOC_GENERATE.get(parsed)).toBe(true)
|
|
35
|
+
expect(templates.APP_TITLE.get(parsed)).toBe(initialObject.appTitle)
|
|
36
|
+
expect(templates.PAGES.get(parsed)).toEqual(initialObject.pages)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('some of the options are unsupported', () => {
|
|
41
|
+
it('raises an error', () => {
|
|
42
|
+
expect.assertions(2)
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
toVariablesFile(initialObject, [])
|
|
46
|
+
} catch (error) {
|
|
47
|
+
expect(error).toBeInstanceOf(UnsupportedVariablesError)
|
|
48
|
+
expect(
|
|
49
|
+
(error as UnsupportedVariablesError).unsupportedVariables,
|
|
50
|
+
).toEqual(['appTitle', 'pages'])
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
})
|
package/tests/index.spec.ts
CHANGED
|
@@ -6,7 +6,12 @@ import type { Page } from '../src/index'
|
|
|
6
6
|
|
|
7
7
|
describe('variables', () => {
|
|
8
8
|
it('contains the right number of export keys', () => {
|
|
9
|
-
expect(Object.keys(variables)).toEqual([
|
|
9
|
+
expect(Object.keys(variables)).toEqual([
|
|
10
|
+
'templates',
|
|
11
|
+
'magidoc',
|
|
12
|
+
'toVariablesFile',
|
|
13
|
+
'UnsupportedVariablesError',
|
|
14
|
+
])
|
|
10
15
|
})
|
|
11
16
|
|
|
12
17
|
describe('template variables', () => {
|
|
@@ -25,7 +30,7 @@ describe('variables', () => {
|
|
|
25
30
|
test('app title', () => {
|
|
26
31
|
testStringVariable(
|
|
27
32
|
variables.templates.APP_TITLE,
|
|
28
|
-
'
|
|
33
|
+
'APP_TITLE',
|
|
29
34
|
z.string().optional(),
|
|
30
35
|
)
|
|
31
36
|
})
|
|
@@ -33,7 +38,7 @@ describe('variables', () => {
|
|
|
33
38
|
test('app logo', () => {
|
|
34
39
|
testStringVariable(
|
|
35
40
|
variables.templates.APP_LOGO,
|
|
36
|
-
'
|
|
41
|
+
'APP_LOGO',
|
|
37
42
|
z.string().optional(),
|
|
38
43
|
)
|
|
39
44
|
})
|
|
@@ -41,7 +46,7 @@ describe('variables', () => {
|
|
|
41
46
|
test('app favicon', () => {
|
|
42
47
|
testStringVariable(
|
|
43
48
|
variables.templates.APP_FAVICON,
|
|
44
|
-
'
|
|
49
|
+
'APP_FAVICON',
|
|
45
50
|
z.string().optional(),
|
|
46
51
|
)
|
|
47
52
|
})
|
|
@@ -49,7 +54,7 @@ describe('variables', () => {
|
|
|
49
54
|
test('site root', () => {
|
|
50
55
|
testStringVariable(
|
|
51
56
|
variables.templates.SITE_ROOT,
|
|
52
|
-
'
|
|
57
|
+
'SITE_ROOT',
|
|
53
58
|
z.string().optional(),
|
|
54
59
|
)
|
|
55
60
|
})
|
|
@@ -57,7 +62,7 @@ describe('variables', () => {
|
|
|
57
62
|
test('site meta', () => {
|
|
58
63
|
testRecordVariable(
|
|
59
64
|
variables.templates.SITE_META,
|
|
60
|
-
'
|
|
65
|
+
'SITE_META',
|
|
61
66
|
z.record(z.string().optional()).optional(),
|
|
62
67
|
)
|
|
63
68
|
})
|
|
@@ -65,7 +70,7 @@ describe('variables', () => {
|
|
|
65
70
|
test('query generation factories', () => {
|
|
66
71
|
testRecordVariable(
|
|
67
72
|
variables.templates.QUERY_GENERATION_FACTORIES,
|
|
68
|
-
'
|
|
73
|
+
'QUERY_GENERATION_FACTORIES',
|
|
69
74
|
z
|
|
70
75
|
.record(
|
|
71
76
|
z
|
|
@@ -92,7 +97,7 @@ describe('variables', () => {
|
|
|
92
97
|
|
|
93
98
|
testArrayVariable(
|
|
94
99
|
variables.templates.PAGES,
|
|
95
|
-
'
|
|
100
|
+
'PAGES',
|
|
96
101
|
z.array(pagesType).optional(),
|
|
97
102
|
)
|
|
98
103
|
})
|
|
@@ -106,7 +111,7 @@ describe('variables', () => {
|
|
|
106
111
|
test('magidoc generate', () => {
|
|
107
112
|
testBooleanVariable(
|
|
108
113
|
variables.magidoc.MAGIDOC_GENERATE,
|
|
109
|
-
'
|
|
114
|
+
'MAGIDOC_GENERATE',
|
|
110
115
|
z.boolean().optional(),
|
|
111
116
|
)
|
|
112
117
|
})
|
|
@@ -115,24 +120,20 @@ describe('variables', () => {
|
|
|
115
120
|
|
|
116
121
|
function testStringVariable(
|
|
117
122
|
target: Variable<string>,
|
|
118
|
-
|
|
123
|
+
key: string,
|
|
119
124
|
expectedZod: ZodType<any>,
|
|
120
125
|
) {
|
|
121
|
-
expect(target.
|
|
126
|
+
expect(target.key).toEqual(key)
|
|
122
127
|
|
|
123
|
-
expect(target.
|
|
124
|
-
expect(target.
|
|
125
|
-
expect(target.
|
|
128
|
+
expect(target.get({ [key]: 'Potato' })).toBe('Potato')
|
|
129
|
+
expect(target.get({ [key]: false })).toBe('false')
|
|
130
|
+
expect(target.get({})).toBeNull()
|
|
126
131
|
|
|
127
|
-
expect(target.
|
|
128
|
-
|
|
129
|
-
)
|
|
130
|
-
expect(target.vite.getOrDefault({ [viteKey]: false }, 'Default')).toBe(
|
|
131
|
-
'false',
|
|
132
|
-
)
|
|
133
|
-
expect(target.vite.getOrDefault({}, 'Default')).toBe('Default')
|
|
132
|
+
expect(target.getOrDefault({ [key]: 'Potato' }, 'Default')).toBe('Potato')
|
|
133
|
+
expect(target.getOrDefault({ [key]: false }, 'Default')).toBe('false')
|
|
134
|
+
expect(target.getOrDefault({}, 'Default')).toBe('Default')
|
|
134
135
|
|
|
135
|
-
expect(target.asEnv('Potato')).toEqual({ [
|
|
136
|
+
expect(target.asEnv('Potato')).toEqual({ [key]: 'Potato' })
|
|
136
137
|
|
|
137
138
|
ensureZodTypeEqual(target, expectedZod)
|
|
138
139
|
}
|
|
@@ -142,27 +143,27 @@ function testBooleanVariable(
|
|
|
142
143
|
viteKey: string,
|
|
143
144
|
expectedZod: ZodType<any>,
|
|
144
145
|
) {
|
|
145
|
-
expect(target.
|
|
146
|
+
expect(target.key).toEqual(viteKey)
|
|
146
147
|
|
|
147
|
-
expect(target.
|
|
148
|
-
expect(target.
|
|
149
|
-
expect(target.
|
|
150
|
-
expect(target.
|
|
151
|
-
expect(target.
|
|
148
|
+
expect(target.get({ [viteKey]: true })).toBe(true)
|
|
149
|
+
expect(target.get({ [viteKey]: 'true' })).toBe(true)
|
|
150
|
+
expect(target.get({ [viteKey]: 't' })).toBe(true)
|
|
151
|
+
expect(target.get({ [viteKey]: '1' })).toBe(true)
|
|
152
|
+
expect(target.get({ [viteKey]: 12 })).toBe(true)
|
|
152
153
|
|
|
153
|
-
expect(target.
|
|
154
|
-
expect(target.
|
|
155
|
-
expect(target.
|
|
156
|
-
expect(target.
|
|
154
|
+
expect(target.get({ [viteKey]: false })).toBe(false)
|
|
155
|
+
expect(target.get({ [viteKey]: 'false' })).toBe(false)
|
|
156
|
+
expect(target.get({ [viteKey]: 'wow' })).toBe(false)
|
|
157
|
+
expect(target.get({ [viteKey]: 0 })).toBe(false)
|
|
157
158
|
|
|
158
|
-
expect(target.
|
|
159
|
-
expect(target.
|
|
160
|
-
expect(target.
|
|
161
|
-
expect(target.
|
|
159
|
+
expect(target.get({ [viteKey]: {} })).toBeNull()
|
|
160
|
+
expect(target.get({ [viteKey]: null })).toBeNull()
|
|
161
|
+
expect(target.get({ [viteKey]: undefined })).toBeNull()
|
|
162
|
+
expect(target.get({})).toBeNull()
|
|
162
163
|
|
|
163
|
-
expect(target.
|
|
164
|
-
expect(target.
|
|
165
|
-
expect(target.
|
|
164
|
+
expect(target.getOrDefault({ [viteKey]: false }, true)).toBe(false)
|
|
165
|
+
expect(target.getOrDefault({ [viteKey]: {} }, true)).toBe(true)
|
|
166
|
+
expect(target.getOrDefault({}, true)).toBe(true)
|
|
166
167
|
|
|
167
168
|
expect(target.asEnv(true)).toEqual({ [viteKey]: 'true' })
|
|
168
169
|
expect(target.asEnv(false)).toEqual({ [viteKey]: 'false' })
|
|
@@ -172,76 +173,71 @@ function testBooleanVariable(
|
|
|
172
173
|
|
|
173
174
|
function testRecordVariable(
|
|
174
175
|
target: Variable<Record<string, any>>,
|
|
175
|
-
|
|
176
|
+
key: string,
|
|
176
177
|
expectedZod: ZodType<any>,
|
|
177
178
|
) {
|
|
178
|
-
expect(target.
|
|
179
|
+
expect(target.key).toEqual(key)
|
|
179
180
|
|
|
180
|
-
expect(target.
|
|
181
|
-
expect(target.
|
|
182
|
-
expect(target.
|
|
183
|
-
expect(target.
|
|
181
|
+
expect(target.get({ [key]: true })).toBeNull()
|
|
182
|
+
expect(target.get({ [key]: 'true' })).toBeNull()
|
|
183
|
+
expect(target.get({ [key]: '4234' })).toBeNull()
|
|
184
|
+
expect(target.get({ [key]: 4234 })).toBeNull()
|
|
184
185
|
|
|
185
|
-
expect(target.
|
|
186
|
-
expect(
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
expect(target.get({ [key]: { abc: '123' } })).toEqual({ abc: '123' })
|
|
187
|
+
expect(target.get({ [key]: JSON.stringify({ abc: '123' }) })).toEqual({
|
|
188
|
+
abc: '123',
|
|
189
|
+
})
|
|
189
190
|
|
|
190
|
-
expect(target.
|
|
191
|
+
expect(target.getOrDefault({ [key]: false }, { abc: 123 })).toEqual({
|
|
191
192
|
abc: 123,
|
|
192
193
|
})
|
|
193
|
-
expect(target.
|
|
194
|
-
expect(target.
|
|
194
|
+
expect(target.getOrDefault({ [key]: {} }, { abc: 123 })).toEqual({})
|
|
195
|
+
expect(target.getOrDefault({}, { abc: 123 })).toEqual({ abc: 123 })
|
|
195
196
|
|
|
196
197
|
expect(target.asEnv({ abc: 123 })).toEqual({
|
|
197
|
-
[
|
|
198
|
+
[key]: JSON.stringify({ abc: 123 }),
|
|
198
199
|
})
|
|
199
|
-
expect(target.asEnv({})).toEqual({ [
|
|
200
|
+
expect(target.asEnv({})).toEqual({ [key]: '{}' })
|
|
200
201
|
|
|
201
202
|
ensureZodTypeEqual(target, expectedZod)
|
|
202
203
|
}
|
|
203
204
|
|
|
204
205
|
function testArrayVariable(
|
|
205
206
|
target: Variable<Array<any>>,
|
|
206
|
-
|
|
207
|
+
key: string,
|
|
207
208
|
expectedZod: ZodType<any>,
|
|
208
209
|
) {
|
|
209
|
-
expect(target.
|
|
210
|
+
expect(target.key).toEqual(key)
|
|
210
211
|
|
|
211
|
-
expect(target.
|
|
212
|
-
expect(target.
|
|
213
|
-
expect(target.
|
|
214
|
-
expect(target.
|
|
215
|
-
expect(target.
|
|
212
|
+
expect(target.get({ [key]: true })).toBeNull()
|
|
213
|
+
expect(target.get({ [key]: 'true' })).toBeNull()
|
|
214
|
+
expect(target.get({ [key]: '4234' })).toBeNull()
|
|
215
|
+
expect(target.get({ [key]: 4234 })).toBeNull()
|
|
216
|
+
expect(target.get({ [key]: {} })).toBeNull()
|
|
216
217
|
|
|
217
|
-
expect(target.
|
|
218
|
+
expect(target.get({ [key]: [{ abc: '123' }] })).toEqual([{ abc: '123' }])
|
|
219
|
+
expect(target.get({ [key]: JSON.stringify([{ abc: '123' }]) })).toEqual([
|
|
218
220
|
{ abc: '123' },
|
|
219
221
|
])
|
|
220
|
-
expect(
|
|
221
|
-
target.vite.get({ [viteKey]: JSON.stringify([{ abc: '123' }]) }),
|
|
222
|
-
).toEqual([{ abc: '123' }])
|
|
223
222
|
|
|
224
|
-
expect(
|
|
225
|
-
target.vite.getOrDefault({ [viteKey]: false }, [{ abc: 123 }]),
|
|
226
|
-
).toEqual([
|
|
223
|
+
expect(target.getOrDefault({ [key]: false }, [{ abc: 123 }])).toEqual([
|
|
227
224
|
{
|
|
228
225
|
abc: 123,
|
|
229
226
|
},
|
|
230
227
|
])
|
|
231
|
-
expect(target.
|
|
232
|
-
|
|
233
|
-
)
|
|
234
|
-
expect(target.vite.getOrDefault({}, [{ abc: 123 }])).toEqual([{ abc: 123 }])
|
|
228
|
+
expect(target.getOrDefault({ [key]: [] }, [{ abc: 123 }])).toEqual([])
|
|
229
|
+
expect(target.getOrDefault({}, [{ abc: 123 }])).toEqual([{ abc: 123 }])
|
|
235
230
|
|
|
236
231
|
expect(target.asEnv([{ abc: 123 }])).toEqual({
|
|
237
|
-
[
|
|
232
|
+
[key]: JSON.stringify([{ abc: 123 }]),
|
|
238
233
|
})
|
|
239
|
-
expect(target.asEnv([])).toEqual({ [
|
|
234
|
+
expect(target.asEnv([])).toEqual({ [key]: '[]' })
|
|
240
235
|
|
|
241
236
|
ensureZodTypeEqual(target, expectedZod)
|
|
242
237
|
}
|
|
243
238
|
|
|
244
239
|
function ensureZodTypeEqual(target: Variable<any>, expected: ZodType<any>) {
|
|
240
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
245
241
|
// @ts-ignore
|
|
246
242
|
// this does a good enough compare to make sure they have the same schema
|
|
247
243
|
expect(JSON.parse(JSON.stringify(target.zod.type(z)))).toEqual(
|