-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlib.ts
More file actions
165 lines (133 loc) · 4.32 KB
/
lib.ts
File metadata and controls
165 lines (133 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { resolve, basename } from 'path';
import fs from 'fs';
import os from 'os';
import parseEnv from 'parse-dotenv';
import globby from 'globby';
import pkgConf from 'pkg-conf';
const DEFAULT_ENV_PATH = resolve(process.cwd(), '.env');
const DEFAULT_SAMPLE_ENV = resolve(process.cwd(), '.env.example');
interface EnvObject {
[key: string]: any;
}
interface Config {
preserve?: [string];
emptyLines?: boolean;
comments?: boolean;
}
export const fileExists = (path: string) => fs.existsSync(path);
export const getObjKeys = (obj: object) => Object.keys(obj);
export const envToString = (parsed: EnvObject) =>
getObjKeys(parsed)
.map((key) => `${key}=${parsed[key] || ''}`)
.join(os.EOL)
.replace(/(__\w+_\d+__=)/g, '');
export const writeToSampleEnv = (path: string, parsedEnv: object) => {
try {
fs.writeFileSync(path, envToString(parsedEnv));
} catch (error: unknown) {
throw new Error(
`Sync failed. ${error instanceof Error ? error.message : String(error)}`
);
}
};
export const emptyObjProps = (obj: EnvObject) => {
const objCopy = { ...obj };
Object.keys(objCopy).forEach((key) => {
if (objCopy[key].includes('#')) {
if (objCopy[key].match(/(".*"|'.*')/g)) {
const objArr = objCopy[key].split(/(".*"|'.*')/);
objCopy[key] = objArr.slice(-1)[0].trim();
} else {
const objArr = objCopy[key].split('#');
objCopy[key] = `#${objArr.slice(-1)[0]}`;
}
return;
}
/* istanbul ignore else */
if (!key.startsWith('__COMMENT_')) {
objCopy[key] = '';
}
});
return objCopy;
};
export const getUniqueVarsFromEnvs = async (
env: EnvObject,
envExample: EnvObject,
config: Config = {}
) => {
const ignoreKeys = config.preserve || [];
const uniqueKeys = new Set(getObjKeys(env));
const uniqueKeysArray: Array<string> = Array.from(uniqueKeys);
const uniqueFromSource = uniqueKeysArray.map((key: string) => {
if (key.startsWith('__COMMENT_')) return { [key]: env[key] };
return { [key]: envExample[key] || '' };
});
const presevedVars = getObjKeys(envExample)
.map((key) => ({ [key]: envExample[key] }))
.filter(
// eslint-disable-next-line no-shadow
(env) => ignoreKeys.length && ignoreKeys.includes(getObjKeys(env)[0])
);
return [...uniqueFromSource, ...presevedVars];
};
export const syncWithSampleEnv = async (
envPath: string,
envExamplePath: string,
initialConfig?: Config
) => {
// We do this so we can pass it via test as well
const config: Config =
initialConfig || ((await pkgConf('sync-dotenv')) as any);
// Set defaults
config.comments =
typeof config.comments === 'undefined' ? true : config.comments;
config.emptyLines =
typeof config.emptyLines === 'undefined' ? true : config.comments;
const sourceEnv = emptyObjProps(
parseEnv(envPath, {
emptyLines: !!config.emptyLines,
comments: !!config.comments,
})
);
const targetEnv = parseEnv(envExamplePath);
const uniqueVars = await getUniqueVarsFromEnvs(sourceEnv, targetEnv, config);
const envCopy: EnvObject = {};
uniqueVars.forEach((env) => {
const [key] = getObjKeys(env);
envCopy[key] = env[key];
});
writeToSampleEnv(envExamplePath, envCopy);
};
const exit = (message: string, code: number = 1) =>
// eslint-disable-next-line prefer-promise-reject-errors
Promise.reject({ message, code });
export const syncEnv = async (
sampleEnv?: string,
source?: string,
samples?: string
): Promise<{ msg: string; code: number } | string> => {
if (sampleEnv && (sampleEnv === '.env' || basename(sampleEnv) === '.env'))
return exit('Cannot sync .env with .env');
const SAMPLE_ENV_PATHS: string[] = !samples
? [resolve(process.cwd(), sampleEnv || DEFAULT_SAMPLE_ENV)]
: globby
.sync(samples)
.map((sample: string) => resolve(process.cwd(), sample));
// eslint-disable-next-line no-nested-ternary
const envPath = source
? fileExists(source)
? source
: null
: DEFAULT_ENV_PATH;
if (envPath === null) return exit(`${source} not found`);
if (!source && !fileExists(envPath)) return exit(".env doesn't exists");
if (!SAMPLE_ENV_PATHS.length)
return exit(`${samples} did not match any file`);
if (!fileExists(SAMPLE_ENV_PATHS[0]))
return exit(`${sampleEnv || basename(DEFAULT_SAMPLE_ENV)} not found`);
const sourcePath = envPath;
for (const samplePath of SAMPLE_ENV_PATHS) {
await syncWithSampleEnv(sourcePath, samplePath);
}
return Promise.resolve(SAMPLE_ENV_PATHS.join(' '));
};