@teambit/config-store 0.0.16 → 0.0.17
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/package.json +32 -20
- package/types/asset.d.ts +15 -3
- package/config-cmd.ts +0 -143
- package/config-getter.ts +0 -149
- package/config-store.aspect.ts +0 -6
- package/config-store.main.runtime.ts +0 -89
- package/global-config.ts +0 -69
- package/index.ts +0 -6
- /package/dist/{preview-1741058300965.js → preview-1741111850470.js} +0 -0
package/package.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@teambit/config-store",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.17",
|
4
4
|
"homepage": "https://bit.cloud/teambit/harmony/config-store",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"componentId": {
|
7
7
|
"scope": "teambit.harmony",
|
8
8
|
"name": "config-store",
|
9
|
-
"version": "0.0.
|
9
|
+
"version": "0.0.17"
|
10
10
|
},
|
11
11
|
"dependencies": {
|
12
12
|
"chalk": "4.1.2",
|
@@ -14,33 +14,45 @@
|
|
14
14
|
"@teambit/gitconfig": "2.0.10",
|
15
15
|
"lodash": "4.17.21",
|
16
16
|
"fs-extra": "10.0.0",
|
17
|
-
"core-js": "^3.0.0",
|
18
|
-
"@babel/runtime": "7.20.0",
|
19
17
|
"@teambit/harmony": "0.4.7",
|
20
|
-
"@teambit/cli": "0.0.
|
18
|
+
"@teambit/cli": "0.0.1137",
|
21
19
|
"@teambit/legacy.constants": "0.0.11"
|
22
20
|
},
|
23
21
|
"devDependencies": {
|
24
22
|
"@types/lodash": "4.14.165",
|
25
23
|
"@types/fs-extra": "9.0.7",
|
26
|
-
"@
|
27
|
-
"@types/react": "^17.0.8",
|
28
|
-
"@types/react-dom": "^17.0.5",
|
29
|
-
"@types/jest": "^26.0.0",
|
30
|
-
"@types/testing-library__jest-dom": "5.9.5"
|
24
|
+
"@teambit/harmony.envs.core-aspect-env": "0.0.69"
|
31
25
|
},
|
32
|
-
"peerDependencies": {
|
33
|
-
|
34
|
-
"react-dom": "^16.8.0 || ^17.0.0"
|
35
|
-
},
|
36
|
-
"license": "SEE LICENSE IN UNLICENSED",
|
26
|
+
"peerDependencies": {},
|
27
|
+
"license": "Apache-2.0",
|
37
28
|
"optionalDependencies": {},
|
38
29
|
"peerDependenciesMeta": {},
|
39
30
|
"exports": {
|
40
|
-
"
|
41
|
-
"
|
42
|
-
|
31
|
+
".": {
|
32
|
+
"node": {
|
33
|
+
"require": "./dist/index.js",
|
34
|
+
"import": "./dist/esm.mjs"
|
35
|
+
},
|
36
|
+
"default": "./dist/index.js"
|
43
37
|
},
|
44
|
-
"
|
45
|
-
|
38
|
+
"./dist/*": "./dist/*",
|
39
|
+
"./artifacts/*": "./artifacts/*",
|
40
|
+
"./*": "./*.ts"
|
41
|
+
},
|
42
|
+
"private": false,
|
43
|
+
"engines": {
|
44
|
+
"node": ">=16.0.0"
|
45
|
+
},
|
46
|
+
"repository": {
|
47
|
+
"type": "git",
|
48
|
+
"url": "https://github.com/teambit/bit"
|
49
|
+
},
|
50
|
+
"keywords": [
|
51
|
+
"bit",
|
52
|
+
"bit-aspect",
|
53
|
+
"bit-core-aspect",
|
54
|
+
"components",
|
55
|
+
"collaboration",
|
56
|
+
"web"
|
57
|
+
]
|
46
58
|
}
|
package/types/asset.d.ts
CHANGED
@@ -5,12 +5,12 @@ declare module '*.png' {
|
|
5
5
|
declare module '*.svg' {
|
6
6
|
import type { FunctionComponent, SVGProps } from 'react';
|
7
7
|
|
8
|
-
export const ReactComponent: FunctionComponent<
|
8
|
+
export const ReactComponent: FunctionComponent<
|
9
|
+
SVGProps<SVGSVGElement> & { title?: string }
|
10
|
+
>;
|
9
11
|
const src: string;
|
10
12
|
export default src;
|
11
13
|
}
|
12
|
-
|
13
|
-
// @TODO Gilad
|
14
14
|
declare module '*.jpg' {
|
15
15
|
const value: any;
|
16
16
|
export = value;
|
@@ -27,3 +27,15 @@ declare module '*.bmp' {
|
|
27
27
|
const value: any;
|
28
28
|
export = value;
|
29
29
|
}
|
30
|
+
declare module '*.otf' {
|
31
|
+
const value: any;
|
32
|
+
export = value;
|
33
|
+
}
|
34
|
+
declare module '*.woff' {
|
35
|
+
const value: any;
|
36
|
+
export = value;
|
37
|
+
}
|
38
|
+
declare module '*.woff2' {
|
39
|
+
const value: any;
|
40
|
+
export = value;
|
41
|
+
}
|
package/config-cmd.ts
DELETED
@@ -1,143 +0,0 @@
|
|
1
|
-
/* eslint max-classes-per-file: 0 */
|
2
|
-
import chalk from 'chalk';
|
3
|
-
import rightpad from 'pad-right';
|
4
|
-
import { BASE_DOCS_DOMAIN } from '@teambit/legacy.constants';
|
5
|
-
import { Command, CommandOptions } from '@teambit/cli';
|
6
|
-
import { ConfigStoreMain, StoreOrigin } from './config-store.main.runtime';
|
7
|
-
|
8
|
-
class ConfigSet implements Command {
|
9
|
-
name = 'set <key> <val>';
|
10
|
-
description = 'set a configuration. default to save it globally';
|
11
|
-
extendedDescription = `to set temporary configuration by env variable, prefix with "BIT_CONFIG", replace "." with "_" and change to upper case.
|
12
|
-
for example, "user.token" becomes "BIT_CONFIG_USER_TOKEN"`;
|
13
|
-
baseUrl = 'reference/config/bit-config/';
|
14
|
-
alias = '';
|
15
|
-
skipWorkspace = true;
|
16
|
-
options = [
|
17
|
-
['l', 'local', 'set the configuration in the current scope (saved in .bit/scope.json)'],
|
18
|
-
['t', 'local-track', 'set the configuration in the current workspace (saved in workspace.jsonc)'],
|
19
|
-
] as CommandOptions;
|
20
|
-
|
21
|
-
constructor(private configStore: ConfigStoreMain) {}
|
22
|
-
|
23
|
-
async report([key, value]: [string, string], { local, localTrack }: { local?: boolean, localTrack?: boolean }) {
|
24
|
-
const getOrigin = () => {
|
25
|
-
if (local) return 'scope';
|
26
|
-
if (localTrack) return 'workspace';
|
27
|
-
return 'global';
|
28
|
-
}
|
29
|
-
await this.configStore.setConfig(key, value, getOrigin());
|
30
|
-
return chalk.green('added configuration successfully');
|
31
|
-
}
|
32
|
-
}
|
33
|
-
|
34
|
-
class ConfigGet implements Command {
|
35
|
-
name = 'get <key>';
|
36
|
-
description = 'get a value from global configuration';
|
37
|
-
alias = '';
|
38
|
-
options = [] as CommandOptions;
|
39
|
-
|
40
|
-
constructor(private configStore: ConfigStoreMain) {}
|
41
|
-
|
42
|
-
async report([key]: [string]) {
|
43
|
-
const value = this.configStore.getConfig(key);
|
44
|
-
return value || '';
|
45
|
-
}
|
46
|
-
}
|
47
|
-
|
48
|
-
class ConfigList implements Command {
|
49
|
-
name = 'list';
|
50
|
-
description = 'list all configuration(s)';
|
51
|
-
alias = '';
|
52
|
-
options = [
|
53
|
-
['o', 'origin <origin>', 'list configuration specifically from the following: [scope, workspace, global]'],
|
54
|
-
['d', 'detailed', 'list all configuration(s) with the origin'],
|
55
|
-
['j', 'json', 'output as JSON'],
|
56
|
-
] as CommandOptions;
|
57
|
-
|
58
|
-
constructor(private configStore: ConfigStoreMain) {}
|
59
|
-
|
60
|
-
async report(_, { origin, detailed }: { origin?: StoreOrigin, detailed?: boolean }) {
|
61
|
-
|
62
|
-
const objToFormattedString = (conf: Record<string, string>) => {
|
63
|
-
return Object.entries(conf)
|
64
|
-
.map((tuple) => {
|
65
|
-
tuple[0] = rightpad(tuple[0], 45, ' ');
|
66
|
-
return tuple.join('');
|
67
|
-
})
|
68
|
-
.join('\n');
|
69
|
-
}
|
70
|
-
|
71
|
-
if (origin) {
|
72
|
-
const conf = this.configStore.stores[origin].list();
|
73
|
-
return objToFormattedString(conf);
|
74
|
-
}
|
75
|
-
|
76
|
-
if (detailed) {
|
77
|
-
const formatTitle = (str: string) => chalk.bold(str.toUpperCase());
|
78
|
-
const origins = Object.keys(this.configStore.stores).map((originName) => {
|
79
|
-
const conf = this.configStore.stores[originName].list();
|
80
|
-
return formatTitle(originName) + '\n' + objToFormattedString(conf);
|
81
|
-
}).join('\n\n');
|
82
|
-
const combined = this.configStore.listConfig();
|
83
|
-
|
84
|
-
const combinedFormatted = objToFormattedString(combined);
|
85
|
-
return `${origins}\n\n${formatTitle('All Combined')}\n${combinedFormatted}`;
|
86
|
-
}
|
87
|
-
|
88
|
-
const conf = this.configStore.listConfig();
|
89
|
-
return objToFormattedString(conf);
|
90
|
-
}
|
91
|
-
|
92
|
-
async json(_, { origin, detailed }: { origin?: StoreOrigin, detailed?: boolean }) {
|
93
|
-
if (origin) {
|
94
|
-
return this.configStore.stores[origin].list();
|
95
|
-
}
|
96
|
-
if (detailed) {
|
97
|
-
const allStores = Object.keys(this.configStore.stores).reduce((acc, current) => {
|
98
|
-
acc[current] = this.configStore.stores[current].list();
|
99
|
-
return acc;
|
100
|
-
}, {} as Record<string, Record<string, string>>);
|
101
|
-
allStores.combined = this.configStore.listConfig();
|
102
|
-
return allStores;
|
103
|
-
}
|
104
|
-
return this.configStore.listConfig();
|
105
|
-
}
|
106
|
-
}
|
107
|
-
|
108
|
-
class ConfigDel implements Command {
|
109
|
-
name = 'del <key>';
|
110
|
-
description = 'delete given key from global configuration';
|
111
|
-
alias = '';
|
112
|
-
options = [
|
113
|
-
['o', 'origin <origin>', 'default to delete whenever it found first. specify to delete specifically from the following: [scope, workspace, global]'],
|
114
|
-
] as CommandOptions;
|
115
|
-
|
116
|
-
constructor(private configStore: ConfigStoreMain) {}
|
117
|
-
|
118
|
-
async report([key]: [string], { origin }: { origin?: StoreOrigin }) {
|
119
|
-
await this.configStore.delConfig(key, origin);
|
120
|
-
return chalk.green('deleted successfully');
|
121
|
-
}
|
122
|
-
}
|
123
|
-
|
124
|
-
export class ConfigCmd implements Command {
|
125
|
-
name = 'config';
|
126
|
-
description = 'config management';
|
127
|
-
extendedDescription = `${BASE_DOCS_DOMAIN}reference/config/bit-config`;
|
128
|
-
group = 'general';
|
129
|
-
alias = '';
|
130
|
-
loadAspects = false;
|
131
|
-
commands: Command[] = [];
|
132
|
-
options = [] as CommandOptions;
|
133
|
-
|
134
|
-
constructor(private configStore: ConfigStoreMain) {
|
135
|
-
this.commands = [
|
136
|
-
new ConfigSet(configStore), new ConfigDel(configStore), new ConfigGet(configStore), new ConfigList(configStore)
|
137
|
-
];
|
138
|
-
}
|
139
|
-
|
140
|
-
async report() {
|
141
|
-
return new ConfigList(this.configStore).report(undefined, { });
|
142
|
-
}
|
143
|
-
}
|
package/config-getter.ts
DELETED
@@ -1,149 +0,0 @@
|
|
1
|
-
import gitconfig from '@teambit/gitconfig';
|
2
|
-
import { isNil } from 'lodash';
|
3
|
-
import { getGlobalConfigPath, GlobalConfig } from './global-config';
|
4
|
-
|
5
|
-
export const ENV_VARIABLE_CONFIG_PREFIX = 'BIT_CONFIG_';
|
6
|
-
|
7
|
-
export interface Store {
|
8
|
-
list(): Record<string, string>;
|
9
|
-
set(key: string, value: string): void;
|
10
|
-
del(key: string): void;
|
11
|
-
write(): Promise<void>;
|
12
|
-
getPath(): string;
|
13
|
-
invalidateCache(): Promise<void>;
|
14
|
-
}
|
15
|
-
|
16
|
-
/**
|
17
|
-
* Singleton cache for the config object. so it can be used everywhere even by non-aspects components.
|
18
|
-
*/
|
19
|
-
export class ConfigGetter {
|
20
|
-
private _store: Record<string, string> | undefined;
|
21
|
-
private _globalConfig: GlobalConfig | undefined;
|
22
|
-
private gitStore: Record<string, string | undefined> = {};
|
23
|
-
|
24
|
-
get globalConfig() {
|
25
|
-
if (!this._globalConfig) {
|
26
|
-
this._globalConfig = GlobalConfig.loadSync();
|
27
|
-
}
|
28
|
-
return this._globalConfig;
|
29
|
-
}
|
30
|
-
|
31
|
-
get store() {
|
32
|
-
if (!this._store) {
|
33
|
-
this._store = this.globalConfig.toPlainObject();
|
34
|
-
}
|
35
|
-
return this._store;
|
36
|
-
}
|
37
|
-
|
38
|
-
/**
|
39
|
-
* in case a config-key exists in both, the new one (the given store) wins.
|
40
|
-
*/
|
41
|
-
addStore(store: Store) {
|
42
|
-
const currentStore = this.store;
|
43
|
-
this._store = { ...currentStore, ...store.list() };
|
44
|
-
}
|
45
|
-
|
46
|
-
getConfig(key: string): string | undefined {
|
47
|
-
if (!key) {
|
48
|
-
return undefined;
|
49
|
-
}
|
50
|
-
|
51
|
-
const envVarName = toEnvVariableName(key);
|
52
|
-
if (process.env[envVarName]) {
|
53
|
-
return process.env[envVarName];
|
54
|
-
}
|
55
|
-
|
56
|
-
const store = this.store;
|
57
|
-
const val = store[key];
|
58
|
-
if (!isNil(val)) {
|
59
|
-
return val;
|
60
|
-
}
|
61
|
-
const gitKeys = ['user.name', 'user.email'];
|
62
|
-
if (gitKeys.includes(key)) {
|
63
|
-
return this.getFromGit(key);
|
64
|
-
}
|
65
|
-
}
|
66
|
-
private getFromGit(key: string): string | undefined {
|
67
|
-
if (key in this.gitStore) {
|
68
|
-
return this.gitStore[key];
|
69
|
-
}
|
70
|
-
try {
|
71
|
-
const gitVal = gitconfig.get.sync(key);
|
72
|
-
this.gitStore[key] = gitVal;
|
73
|
-
} catch {
|
74
|
-
// Ignore error from git config get
|
75
|
-
this.gitStore[key] = undefined;
|
76
|
-
}
|
77
|
-
return this.gitStore[key];
|
78
|
-
}
|
79
|
-
getConfigNumeric(key: string): number | undefined {
|
80
|
-
const fromConfig = this.getConfig(key);
|
81
|
-
if (isNil(fromConfig)) return undefined;
|
82
|
-
const num = Number(fromConfig);
|
83
|
-
if (Number.isNaN(num)) {
|
84
|
-
throw new Error(`config of "${key}" is invalid. Expected number, got "${fromConfig}"`);
|
85
|
-
}
|
86
|
-
return num;
|
87
|
-
}
|
88
|
-
getConfigBoolean(key: string): boolean | undefined {
|
89
|
-
const result = this.getConfig(key);
|
90
|
-
if (isNil(result)) return undefined;
|
91
|
-
if (typeof result === 'boolean') return result;
|
92
|
-
if (result === 'true') return true;
|
93
|
-
if (result === 'false') return false;
|
94
|
-
throw new Error(`the configuration "${key}" has an invalid value "${result}". it should be boolean`);
|
95
|
-
}
|
96
|
-
listConfig() {
|
97
|
-
const store = this.store;
|
98
|
-
return store;
|
99
|
-
}
|
100
|
-
invalidateCache() {
|
101
|
-
this._store = undefined;
|
102
|
-
}
|
103
|
-
getGlobalStore(): Store {
|
104
|
-
return {
|
105
|
-
list: () => this.globalConfig.toPlainObject(),
|
106
|
-
set: (key: string, value: string) => this.globalConfig.set(key, value),
|
107
|
-
del: (key: string) => this.globalConfig.delete(key),
|
108
|
-
write: async () => this.globalConfig.write(),
|
109
|
-
invalidateCache: async () => this._globalConfig = undefined,
|
110
|
-
getPath: () => getGlobalConfigPath(),
|
111
|
-
};
|
112
|
-
}
|
113
|
-
}
|
114
|
-
|
115
|
-
export const configGetter = new ConfigGetter();
|
116
|
-
|
117
|
-
export function getConfig(key: string): string | undefined {
|
118
|
-
return configGetter.getConfig(key);
|
119
|
-
}
|
120
|
-
export function getNumberFromConfig(key: string): number | undefined {
|
121
|
-
return configGetter.getConfigNumeric(key);
|
122
|
-
}
|
123
|
-
export function listConfig(): Record<string, string> {
|
124
|
-
return configGetter.listConfig();
|
125
|
-
}
|
126
|
-
/**
|
127
|
-
* @deprecated use setConfig from the ConfigStore aspect instance
|
128
|
-
*/
|
129
|
-
export function setGlobalConfig(key: string, val: string) {
|
130
|
-
const globalStore = configGetter.getGlobalStore();
|
131
|
-
globalStore.set(key, val);
|
132
|
-
configGetter.globalConfig.writeSync();
|
133
|
-
globalStore.invalidateCache().catch(() => {});
|
134
|
-
configGetter.invalidateCache();
|
135
|
-
}
|
136
|
-
/**
|
137
|
-
* @deprecated use delConfig from the ConfigStore aspect instance
|
138
|
-
*/
|
139
|
-
export function delGlobalConfig(key: string) {
|
140
|
-
const globalStore = configGetter.getGlobalStore();
|
141
|
-
globalStore.del(key);
|
142
|
-
configGetter.globalConfig.writeSync();
|
143
|
-
globalStore.invalidateCache().catch(() => {});
|
144
|
-
configGetter.invalidateCache();
|
145
|
-
}
|
146
|
-
|
147
|
-
function toEnvVariableName(configName: string): string {
|
148
|
-
return `${ENV_VARIABLE_CONFIG_PREFIX}${configName.replace(/\./g, '_').toUpperCase()}`;
|
149
|
-
}
|
package/config-store.aspect.ts
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
|
2
|
-
import { compact } from 'lodash';
|
3
|
-
import { ConfigStoreAspect } from './config-store.aspect';
|
4
|
-
import { configGetter, Store } from './config-getter';
|
5
|
-
import { ConfigCmd } from './config-cmd';
|
6
|
-
|
7
|
-
export type StoreOrigin = 'scope' | 'workspace' | 'global';
|
8
|
-
|
9
|
-
export class ConfigStoreMain {
|
10
|
-
private _stores: { [origin: string]: Store } | undefined;
|
11
|
-
get stores (): { [origin: string]: Store } {
|
12
|
-
if (!this._stores) {
|
13
|
-
this._stores = {
|
14
|
-
global: configGetter.getGlobalStore()
|
15
|
-
};
|
16
|
-
}
|
17
|
-
return this._stores;
|
18
|
-
}
|
19
|
-
addStore(origin: StoreOrigin, store: Store) {
|
20
|
-
this.stores[origin] = store;
|
21
|
-
configGetter.addStore(store);
|
22
|
-
}
|
23
|
-
/**
|
24
|
-
* "global" must be first. the rest doesn't matter. can be scope or workspace.
|
25
|
-
*/
|
26
|
-
getStoresOrdered(): Store[] {
|
27
|
-
return compact([this.stores.global, this.stores.workspace, this.stores.scope].filter(Boolean)) as Store[];
|
28
|
-
}
|
29
|
-
invalidateCache() {
|
30
|
-
configGetter.invalidateCache();
|
31
|
-
const stores = this.getStoresOrdered();
|
32
|
-
stores.forEach((store) => {
|
33
|
-
configGetter.addStore(store);
|
34
|
-
});
|
35
|
-
}
|
36
|
-
async invalidateAllStoresCaches() {
|
37
|
-
configGetter.invalidateCache();
|
38
|
-
const stores = this.getStoresOrdered();
|
39
|
-
for await (const store of stores) {
|
40
|
-
await store.invalidateCache();
|
41
|
-
configGetter.addStore(store);
|
42
|
-
};
|
43
|
-
}
|
44
|
-
async setConfig(key: string, value: string, origin: StoreOrigin = 'global') {
|
45
|
-
const store = this.stores[origin];
|
46
|
-
if (!store) throw new Error(`unable to set config, "${origin}" origin is missing`);
|
47
|
-
store.set(key, value);
|
48
|
-
await store.write();
|
49
|
-
await this.invalidateCache();
|
50
|
-
}
|
51
|
-
getConfig(key: string): string | undefined {
|
52
|
-
return configGetter.getConfig(key);
|
53
|
-
}
|
54
|
-
getConfigBoolean(key: string): boolean | undefined {
|
55
|
-
return configGetter.getConfigBoolean(key);
|
56
|
-
}
|
57
|
-
getConfigNumeric(key: string): number | undefined {
|
58
|
-
return configGetter.getConfigNumeric(key);
|
59
|
-
}
|
60
|
-
async delConfig(key: string, origin?: StoreOrigin) {
|
61
|
-
const getOrigin = () => {
|
62
|
-
if (origin) return origin;
|
63
|
-
return Object.keys(this.stores).find(originName => key in this.stores[originName].list());
|
64
|
-
}
|
65
|
-
const foundOrigin = getOrigin();
|
66
|
-
if (!foundOrigin) return; // if the key is not found in any store (or given store), nothing to do.
|
67
|
-
const store = this.stores[foundOrigin];
|
68
|
-
store.del(key);
|
69
|
-
await store.write();
|
70
|
-
await this.invalidateCache();
|
71
|
-
}
|
72
|
-
listConfig() {
|
73
|
-
return configGetter.listConfig();
|
74
|
-
}
|
75
|
-
|
76
|
-
static slots = [];
|
77
|
-
static dependencies = [CLIAspect];
|
78
|
-
static runtime = MainRuntime;
|
79
|
-
static async provider([cli]: [CLIMain]) {
|
80
|
-
const configStore = new ConfigStoreMain();
|
81
|
-
cli.register(new ConfigCmd(configStore));
|
82
|
-
return configStore;
|
83
|
-
|
84
|
-
}
|
85
|
-
}
|
86
|
-
|
87
|
-
ConfigStoreAspect.addRuntime(ConfigStoreMain);
|
88
|
-
|
89
|
-
export default ConfigStoreMain;
|
package/global-config.ts
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
import fs from 'fs-extra';
|
2
|
-
import * as path from 'path';
|
3
|
-
|
4
|
-
import { GLOBAL_CONFIG, GLOBAL_CONFIG_FILE } from '@teambit/legacy.constants';
|
5
|
-
|
6
|
-
export function getGlobalConfigPath() {
|
7
|
-
return path.join(GLOBAL_CONFIG, GLOBAL_CONFIG_FILE);
|
8
|
-
}
|
9
|
-
|
10
|
-
export class GlobalConfig extends Map<string, string> {
|
11
|
-
toPlainObject() {
|
12
|
-
return mapToObject(this);
|
13
|
-
}
|
14
|
-
|
15
|
-
toJson() {
|
16
|
-
return JSON.stringify(this.toPlainObject(), null, 2);
|
17
|
-
}
|
18
|
-
|
19
|
-
write() {
|
20
|
-
return fs.outputFile(getGlobalConfigPath(), this.toJson());
|
21
|
-
}
|
22
|
-
|
23
|
-
writeSync() {
|
24
|
-
return fs.outputFileSync(getGlobalConfigPath(), this.toJson());
|
25
|
-
}
|
26
|
-
|
27
|
-
static loadSync(): GlobalConfig {
|
28
|
-
const configPath = getGlobalConfigPath();
|
29
|
-
if (!fs.existsSync(configPath)) {
|
30
|
-
const config = new GlobalConfig([]);
|
31
|
-
config.writeSync();
|
32
|
-
return config;
|
33
|
-
}
|
34
|
-
const contents = fs.readFileSync(configPath);
|
35
|
-
return new GlobalConfig(Object.entries(JSON.parse(contents.toString())));
|
36
|
-
}
|
37
|
-
|
38
|
-
static async load(): Promise<GlobalConfig> {
|
39
|
-
const configPath = getGlobalConfigPath();
|
40
|
-
const exists = await fs.pathExists(configPath);
|
41
|
-
if (!exists) {
|
42
|
-
const config = new GlobalConfig([]);
|
43
|
-
await config.write();
|
44
|
-
return config;
|
45
|
-
}
|
46
|
-
const contents = await fs.readFile(configPath);
|
47
|
-
return new GlobalConfig(Object.entries(JSON.parse(contents.toString())));
|
48
|
-
}
|
49
|
-
}
|
50
|
-
|
51
|
-
/**
|
52
|
-
* Cast a `Map` to a plain object.
|
53
|
-
* Keys are being casted by invoking `toString` on each key.
|
54
|
-
* @name mapToObject
|
55
|
-
* @param {Map} map to cast
|
56
|
-
* @returns {*} plain object
|
57
|
-
* @example
|
58
|
-
* ```js
|
59
|
-
* mapToObject(new Map([['key', 'val'], ['foo', 'bar']]));
|
60
|
-
* // => { key: 'val', foo: 'bar' }
|
61
|
-
* ```
|
62
|
-
*/
|
63
|
-
function mapToObject(map: Map<any, any>): { [key: string]: any } {
|
64
|
-
const object = {};
|
65
|
-
map.forEach((val, key) => {
|
66
|
-
object[key.toString()] = val;
|
67
|
-
});
|
68
|
-
return object;
|
69
|
-
}
|
package/index.ts
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
import { ConfigStoreAspect } from './config-store.aspect';
|
2
|
-
|
3
|
-
export type { ConfigStoreMain } from './config-store.main.runtime';
|
4
|
-
export default ConfigStoreAspect;
|
5
|
-
export { ConfigStoreAspect };
|
6
|
-
export { getConfig, getNumberFromConfig, listConfig, Store, setGlobalConfig, delGlobalConfig } from './config-getter';
|
File without changes
|