@magidoc/plugin-starter-variables 0.6.1 → 0.7.1

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/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- import common from './common';
2
- export { common };
1
+ import templates from './variables/templates';
2
+ import magidoc from './variables/magidoc';
3
+ import type { Variable, ViteVariable } from './variables/variable';
4
+ export { templates };
5
+ export { magidoc };
6
+ export type { Variable, ViteVariable };
package/build/index.js CHANGED
@@ -1,2 +1,3 @@
1
- export { default as common } from './common/index.js';
1
+ export { default as templates } from './variables/templates.js';
2
+ export { default as magidoc } from './variables/magidoc.js';
2
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ MAGIDOC_GENERATE: import("./variable").Variable<boolean>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,8 @@
1
+ import { createVariable, booleanConversion } from './variable.js';
2
+
3
+ var magidoc = {
4
+ MAGIDOC_GENERATE: createVariable('MAGIDOC_GENERATE', booleanConversion()),
5
+ };
6
+
7
+ export { magidoc as default };
8
+ //# sourceMappingURL=magidoc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"magidoc.js","sources":["../../../src/variables/magidoc.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA,cAAe;AACb,IAAA,gBAAgB,EAAE,cAAc,CAC9B,kBAAkB,EAClB,iBAAiB,EAAE,CACpB;CACF;;;;"}
@@ -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 templates = {
4
+ APP_LOGO: createVariable('APP_LOGO', stringConversion()),
5
+ APP_TITLE: createVariable('APP_TITLE', stringConversion()),
6
+ };
7
+
8
+ export { templates as default };
9
+ //# sourceMappingURL=templates.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templates.js","sources":["../../../src/variables/templates.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA,gBAAe;AACb,IAAA,QAAQ,EAAE,cAAc,CAAS,UAAU,EAAE,gBAAgB,EAAE,CAAC;AAChE,IAAA,SAAS,EAAE,cAAc,CAAS,WAAW,EAAE,gBAAgB,EAAE,CAAC;CACnE;;;;"}
@@ -5,6 +5,7 @@ interface MetaEnv {
5
5
  [key: string]: unknown;
6
6
  }
7
7
  export declare type Variable<T> = {
8
+ names: string[];
8
9
  vite: ViteVariable<T>;
9
10
  };
10
11
  export declare type ViteVariable<T> = {
@@ -12,7 +13,7 @@ export declare type ViteVariable<T> = {
12
13
  get: (env: MetaEnv) => T | null;
13
14
  getOrDefault: (env: MetaEnv, def: T) => T;
14
15
  };
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;
16
+ export declare function createVariable<T = string | boolean>(key: string, conversion: (target: unknown) => T | null): Variable<T>;
17
+ export declare function stringConversion(): (target: unknown) => string | null;
18
+ export declare function booleanConversion(): (target: unknown) => boolean | null;
18
19
  export {};
@@ -0,0 +1,47 @@
1
+ function createVariable(key, conversion) {
2
+ const viteKey = `VITE_${key.toUpperCase()}`;
3
+ const viteGet = (env) => conversion(env[viteKey]);
4
+ return {
5
+ names: [key, toCamelCase(key)],
6
+ vite: {
7
+ key: viteKey,
8
+ get: viteGet,
9
+ getOrDefault: (env, def) => viteGet(env) ?? def,
10
+ },
11
+ };
12
+ }
13
+ function toCamelCase(key) {
14
+ return key.toLowerCase().replace(/([-_][a-z])/gi, ($1) => {
15
+ return $1.toUpperCase().replace('-', '').replace('_', '');
16
+ });
17
+ }
18
+ function stringConversion() {
19
+ return (target) => {
20
+ if (target === null || target === undefined || target === '') {
21
+ return null;
22
+ }
23
+ return String(target);
24
+ };
25
+ }
26
+ function booleanConversion() {
27
+ return (target) => {
28
+ if (target === null)
29
+ return null;
30
+ if (target === undefined)
31
+ return null;
32
+ switch (typeof target) {
33
+ case 'boolean':
34
+ return target;
35
+ case 'string':
36
+ const lowerCase = target.toLowerCase().trim();
37
+ return lowerCase === 'true' || lowerCase === 't' || lowerCase === '1';
38
+ case 'number':
39
+ return target !== 0;
40
+ default:
41
+ return null;
42
+ }
43
+ };
44
+ }
45
+
46
+ export { booleanConversion, createVariable, stringConversion };
47
+ //# sourceMappingURL=variable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"variable.js","sources":["../../../src/variables/variable.ts"],"sourcesContent":[null],"names":[],"mappings":"AAkBgB,SAAA,cAAc,CAC5B,GAAW,EACX,UAAyC,EAAA;IAEzC,MAAM,OAAO,GAAG,CAAQ,KAAA,EAAA,GAAG,CAAC,WAAW,EAAE,EAAE,CAAA;AAC3C,IAAA,MAAM,OAAO,GAAG,CAAC,GAAY,KAAK,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAA;IAE1D,OAAO;QACL,KAAK,EAAE,CAAC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;AAC9B,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;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,CAAC;SAEe,gBAAgB,GAAA;IAC9B,OAAO,CAAC,MAAe,KAAmB;QACxC,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,EAAE,EAAE;AAC5D,YAAA,OAAO,IAAI,CAAA;AACZ,SAAA;AAED,QAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAA;AACvB,KAAC,CAAA;AACH,CAAC;SAEe,iBAAiB,GAAA;IAC/B,OAAO,CAAC,MAAe,KAAoB;QACzC,IAAI,MAAM,KAAK,IAAI;AAAE,YAAA,OAAO,IAAI,CAAA;QAChC,IAAI,MAAM,KAAK,SAAS;AAAE,YAAA,OAAO,IAAI,CAAA;QAErC,QAAQ,OAAO,MAAM;AACnB,YAAA,KAAK,SAAS;AACZ,gBAAA,OAAO,MAAM,CAAA;AACf,YAAA,KAAK,QAAQ;gBACX,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAA;gBAC7C,OAAO,SAAS,KAAK,MAAM,IAAI,SAAS,KAAK,GAAG,IAAI,SAAS,KAAK,GAAG,CAAA;AACvE,YAAA,KAAK,QAAQ;gBACX,OAAO,MAAM,KAAK,CAAC,CAAA;AACrB,YAAA;AACE,gBAAA,OAAO,IAAI,CAAA;AACd,SAAA;AACH,KAAC,CAAA;AACH;;;;"}
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": "0.6.1",
5
+ "version": "0.7.1",
6
6
  "type": "module",
7
7
  "license": "MIT",
8
8
  "main": "./build/index.js",
package/src/index.ts CHANGED
@@ -1,3 +1,7 @@
1
- import common from './common'
1
+ import templates from './variables/templates'
2
+ import magidoc from './variables/magidoc'
3
+ import type { Variable, ViteVariable } from './variables/variable'
2
4
 
3
- export { common }
5
+ export { templates }
6
+ export { magidoc }
7
+ export type { Variable, ViteVariable }
@@ -0,0 +1,8 @@
1
+ import { booleanConversion, createVariable } from './variable'
2
+
3
+ export default {
4
+ MAGIDOC_GENERATE: createVariable<boolean>(
5
+ 'MAGIDOC_GENERATE',
6
+ booleanConversion(),
7
+ ),
8
+ }
@@ -1,4 +1,4 @@
1
- import { createVariable, stringConversion } from '../variable'
1
+ import { createVariable, stringConversion } from './variable'
2
2
 
3
3
  export default {
4
4
  APP_LOGO: createVariable<string>('APP_LOGO', stringConversion()),
@@ -6,6 +6,7 @@ interface MetaEnv {
6
6
  }
7
7
 
8
8
  export type Variable<T> = {
9
+ names: string[]
9
10
  vite: ViteVariable<T>
10
11
  }
11
12
 
@@ -17,13 +18,13 @@ export type ViteVariable<T> = {
17
18
 
18
19
  export function createVariable<T = string | boolean>(
19
20
  key: string,
20
- conversion: (target: unknown) => T,
21
+ conversion: (target: unknown) => T | null,
21
22
  ): Variable<T> {
22
23
  const viteKey = `VITE_${key.toUpperCase()}`
23
- const viteGet = (env: MetaEnv) =>
24
- env[viteKey] !== undefined ? conversion(env[viteKey]) : null
24
+ const viteGet = (env: MetaEnv) => conversion(env[viteKey])
25
25
 
26
26
  return {
27
+ names: [key, toCamelCase(key)],
27
28
  vite: {
28
29
  key: viteKey,
29
30
  get: viteGet,
@@ -32,12 +33,27 @@ export function createVariable<T = string | boolean>(
32
33
  }
33
34
  }
34
35
 
36
+ function toCamelCase(key: string): string {
37
+ return key.toLowerCase().replace(/([-_][a-z])/gi, ($1) => {
38
+ return $1.toUpperCase().replace('-', '').replace('_', '')
39
+ })
40
+ }
41
+
35
42
  export function stringConversion() {
36
- return (target: unknown): string => String(target)
43
+ return (target: unknown): string | null => {
44
+ if (target === null || target === undefined || target === '') {
45
+ return null
46
+ }
47
+
48
+ return String(target)
49
+ }
37
50
  }
38
51
 
39
52
  export function booleanConversion() {
40
- return (target: unknown): boolean => {
53
+ return (target: unknown): boolean | null => {
54
+ if (target === null) return null
55
+ if (target === undefined) return null
56
+
41
57
  switch (typeof target) {
42
58
  case 'boolean':
43
59
  return target
@@ -47,7 +63,7 @@ export function booleanConversion() {
47
63
  case 'number':
48
64
  return target !== 0
49
65
  default:
50
- return !!target
66
+ return null
51
67
  }
52
68
  }
53
69
  }
@@ -1,16 +1,25 @@
1
1
  import * as variables from '../src/index'
2
- import type { Variable } from '../src/variable'
2
+ import type { Variable } from '../src/variables/variable'
3
3
 
4
4
  describe('variables', () => {
5
5
  it('contains the right number of export keys', () => {
6
- expect(Object.keys(variables)).toEqual(['common'])
6
+ expect(Object.keys(variables)).toEqual(['templates', 'magidoc'])
7
7
  })
8
8
 
9
- it('contains the right common variables', () => {
10
- expect(Object.keys(variables.common)).toEqual(['APP_LOGO', 'APP_TITLE'])
9
+ it('contains the right templates variables', () => {
10
+ expect(Object.keys(variables.templates)).toEqual(['APP_LOGO', 'APP_TITLE'])
11
11
 
12
- testStringVariable(variables.common.APP_TITLE, 'VITE_APP_TITLE')
13
- testStringVariable(variables.common.APP_LOGO, 'VITE_APP_LOGO')
12
+ testStringVariable(variables.templates.APP_TITLE, 'VITE_APP_TITLE')
13
+ testStringVariable(variables.templates.APP_LOGO, 'VITE_APP_LOGO')
14
+ })
15
+
16
+ it('contains the right magidoc variables', () => {
17
+ expect(Object.keys(variables.magidoc)).toEqual(['MAGIDOC_GENERATE'])
18
+
19
+ testBooleanVariable(
20
+ variables.magidoc.MAGIDOC_GENERATE,
21
+ 'VITE_MAGIDOC_GENERATE',
22
+ )
14
23
  })
15
24
  })
16
25
 
@@ -29,3 +38,27 @@ function testStringVariable(target: Variable<string>, viteKey: string) {
29
38
  )
30
39
  expect(target.vite.getOrDefault({}, 'Default')).toBe('Default')
31
40
  }
41
+
42
+ function testBooleanVariable(target: Variable<boolean>, viteKey: string) {
43
+ expect(target.vite.key).toEqual(viteKey)
44
+
45
+ expect(target.vite.get({ [viteKey]: true })).toBe(true)
46
+ expect(target.vite.get({ [viteKey]: 'true' })).toBe(true)
47
+ expect(target.vite.get({ [viteKey]: 't' })).toBe(true)
48
+ expect(target.vite.get({ [viteKey]: '1' })).toBe(true)
49
+ expect(target.vite.get({ [viteKey]: 12 })).toBe(true)
50
+
51
+ expect(target.vite.get({ [viteKey]: false })).toBe(false)
52
+ expect(target.vite.get({ [viteKey]: 'false' })).toBe(false)
53
+ expect(target.vite.get({ [viteKey]: 'wow' })).toBe(false)
54
+ expect(target.vite.get({ [viteKey]: 0 })).toBe(false)
55
+
56
+ expect(target.vite.get({ [viteKey]: {} })).toBeNull()
57
+ expect(target.vite.get({ [viteKey]: null })).toBeNull()
58
+ expect(target.vite.get({ [viteKey]: undefined })).toBeNull()
59
+ expect(target.vite.get({})).toBeNull()
60
+
61
+ expect(target.vite.getOrDefault({ [viteKey]: false }, true)).toBe(false)
62
+ expect(target.vite.getOrDefault({ [viteKey]: {} }, true)).toBe(true)
63
+ expect(target.vite.getOrDefault({}, true)).toBe(true)
64
+ }
@@ -1,5 +0,0 @@
1
- declare const _default: {
2
- APP_LOGO: import("../variable").Variable<string>;
3
- APP_TITLE: import("../variable").Variable<string>;
4
- };
5
- export default _default;
@@ -1,9 +0,0 @@
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
@@ -1 +0,0 @@
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/variable.js DELETED
@@ -1,17 +0,0 @@
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
@@ -1 +0,0 @@
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;;;;"}