-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
390 lines (346 loc) · 10.8 KB
/
index.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import mongoose from 'mongoose';
import chalk from 'chalk';
import ora, { oraPromise } from 'ora';
import readline from 'readline';
import config from './config.js';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const defaultTextColor = 'cyan';
const spinnerColor = 'yellow';
const colors = {
text: chalk[defaultTextColor],
error: chalk.red,
success: chalk.green,
warn: chalk.yellow,
info: chalk.blue,
debug: chalk.magenta,
special: chalk.cyanBright,
};
const logLevels = { debug: 0, info: 1, warn: 2, error: 3, silent: 4 };
const logLevel = logLevels[config.logLevel.toLowerCase()] ?? logLevels.info;
const logDebug = (...text) => {
if (logLevel <= logLevels.debug) console.debug(colors.debug('🛠', ...text));
};
const logInfo = (...text) => {
if (logLevel <= logLevels.info) console.info(colors.info('ℹ', ...text));
};
const logWarn = (...text) => {
if (logLevel <= logLevels.warn) console.warn(colors.warn('⚠', ...text));
};
const logError = (...text) => {
if (logLevel <= logLevels.error) console.error(colors.error('⨯', ...text));
};
const promisify = (func) =>
new Promise((resolve, reject) => {
try {
const result = func();
resolve(result);
} catch (error) {
reject(error);
}
});
const oraOptions = {
color: spinnerColor,
spinner: 'dots',
isEnabled: true,
discardStdin: true,
};
const load = (text, color) => {
if (logLevel >= logLevels.warn)
return {
stop: () => {},
start: () => {},
fail: logLevel <= logLevels.error ? logError : () => {},
info: () => {},
succeed: () => {},
warn: logLevel === logLevels.warn ? logWarn : () => {},
};
return ora({ ...oraOptions, text: chalk[color ?? defaultTextColor](text) });
};
const loadPromise = (
promise,
{ color, text, failText, successText, ...options },
) => {
if (logLevel === logLevels.silent) return promise;
if (logLevel >= logLevels.warn)
return promise.catch((error) => {
logError(failText ?? text);
throw error;
});
return oraPromise(promise, {
...oraOptions,
...options,
text: chalk[color ?? defaultTextColor](text),
failText: colors.error(failText),
successText: colors.success(successText),
}).catch((error) => {
logDebug(error);
throw error;
});
};
const standardCollections = [];
function logDebugData() {
if (logLevel > logLevels.debug) return;
const { dbUri, ...printableConfig } = config;
logDebug(`[${logDebugData.name}]`, 'Log Level:', logLevel);
logDebug(`[${logDebugData.name}]`, 'Config:', JSON.stringify(config.insecureDebugLog ? printableConfig : config));
logDebug(`[${logDebugData.name}]`, 'Standard Collections:', JSON.stringify(standardCollections));
}
function validatePath({ path, collectionName }) {
const extensionIndex = path.lastIndexOf('.');
const extension = path.slice(extensionIndex + 1);
const allowedExtensions = ['json', 'js', 'mjs'];
if (path.lastIndexOf('.') === -1 || !allowedExtensions.includes(extension)) {
logError(
`Invalid file extension '.${extension}' in the path '${path}' of '${collectionName}'.`,
);
if (extension === 'cjs')
logWarn(
'CommonJS is not supported in this script, use ES6 modules instead',
);
return false;
}
return true;
}
async function evaluateCollectionValues({
path,
collectionName,
data,
model,
schema,
}) {
const { preferPath } = config;
let importedValues;
const getStandardValue = ({ configValue, pathValue }) =>
preferPath ? pathValue ?? configValue : configValue;
if (path)
try {
if (!validatePath({ path, collectionName })) return null;
importedValues = await import(path);
} catch (error) {
logError(`The path '${path}' of '${collectionName}' is invalid!`);
logDebug(`[${evaluateCollectionValues.name}]`, error);
return null;
}
return {
standardData: getStandardValue({
pathValue:
importedValues.default?.data ??
importedValues.default ??
importedValues.data,
configValue: data,
}),
standardModel: getStandardValue({
pathValue: importedValues.default?.model ?? importedValues.model,
configValue: model,
}),
standardSchema: getStandardValue({
pathValue: importedValues.default?.schema ?? importedValues.schema,
configValue: schema,
}),
};
}
async function initialise() {
// Validate Mongo URI values
const mongoUriValues = new Set(
['MONGO_URI', 'MONGODB_URI', 'mongoUri', 'DB_URI', 'DATABASE_URI'].filter(
(name) => name in process.env,
),
);
if (!config.mongoUri && !mongoUriValues.size) {
logError('No MongoDB URI provided');
throw new Error('No MongoDB URI provided');
}
if (
mongoUriValues.size > 1 ||
(mongoUriValues.size &&
config.mongoUri !== mongoUriValues.values().next())
) {
logError(
'Mismatch between MongoDB URIs:',
[config.mongoUri, ...Array.from(mongoUriValues)]
.filter((uri) => uri)
.join(', '),
);
throw new Error('Mismatch between MongoDB URIs');
}
// If config.mongoUri is unset, that means that the value is in the process.env
config.mongoUri ??= mongoUriValues.values().next();
// Connect to mongoose
await loadPromise(
mongoose.connect(
config.mongoUri ??
process.env[
[
'MONGO_URI',
'MONGODB_URI',
'mongoUri',
'DB_URI',
'DATABASE_URI',
].find((name) => name in process.env)
],
),
{
text: 'Connecting to MongoDB',
successText: 'Connected to MongoDB',
failText: 'Failed to connect to MongoDB',
},
);
// Create all models
const modelsPromise = () =>
Promise.all(
Object.keys(config.collections).map(async (collectionName) => {
const { model, schema, path, data } =
config.collections[collectionName];
if (!data?.length && !path) {
logError(`No data or path for '${collectionName}'!`);
return;
}
if (path && data?.length)
logWarn(
`Both data and path were provided in '${collectionName}', using ${
config.preferPath ? 'path' : 'data'
}. To change this behavior, change 'preferPath' in config.js.`,
);
const { standardModel, standardSchema, standardData, error } =
await loadPromise(
evaluateCollectionValues({
path,
collectionName,
model,
schema,
data,
}),
{
successText: `Evaluated the values of '${collectionName}'`,
failText: `Failed to evaluate the values of '${collectionName}'`,
text: `Evaluating the values of '${collectionName}'`,
},
).catch(() => ({ error: true }));
if (error) return;
if (!(standardData && standardSchema && standardModel)) {
logError(
`Invalid data, schema or model name for collection '${collectionName}'!`,
);
return;
}
standardCollections.push({
name: collectionName,
model: mongoose.model(standardModel, standardSchema, collectionName),
data: standardData,
});
}),
);
await loadPromise(modelsPromise, {
text: 'Creating models',
successText: 'Created models',
failText: 'Failed to create models',
});
logDebugData();
if (standardCollections.length === 0) {
logError('No valid collections found!');
throw new Error('No valid collections found!');
}
}
async function dryRun() {
await loadPromise(initialise(), {
text: 'Initialising',
successText: 'Initialised',
failText: 'Failed to initialise',
});
// Print how many documents would be deleted and inserted for each collection
await loadPromise(
Promise.all([
Promise.all(
standardCollections.map(async ({ name, model }) => {
const count = await model.countDocuments();
return `${colors.special(name)} - ${colors.special(count)}`;
}),
),
promisify(() =>
standardCollections.map(({ name, data }) =>
data
? `${colors.special(name)} - ${colors.special(data.length)}`
: logDebug(`[${dryRun.name}] Data is undefined in ${name}`),
),
),
]),
{
text: 'Counting documents',
failText: 'Failed to count documents',
successText: 'Counted documents',
},
).then(([deleteMessages, insertMessages]) =>
logInfo(
chalk.bold('Documents that would be deleted'),
'\n',
deleteMessages.join('\n'),
'\n\n',
chalk.bold('Documents that would be inserted'),
'\n',
insertMessages.join('\n'),
),
);
}
async function run() {
// Initialise
await loadPromise(initialise(), {
text: 'Initialising',
successText: 'Initialised',
failText: 'Failed to initialise',
});
// Drop all collections
await loadPromise(
Promise.all(standardCollections.map(({ model }) => model.deleteMany({}))),
{
text: 'Dropping collections',
successText: 'Dropped collections',
failText: 'Failed to drop collections',
},
);
// Insert all documents
await loadPromise(
Promise.all(
standardCollections.map(({ model, data }) => model.insertMany(data)),
),
{
text: 'Inserting documents',
successText: 'Inserted documents',
failText: 'Failed to insert documents',
},
);
}
function startDryRun() {
loadPromise(dryRun(), {
text: 'Performing a dry run',
successText: 'Dry run complete',
failText: 'Failed to perform a dry run',
})
.then(() => process.exit(0))
.catch(() => process.exit(1));
}
function startRun() {
// Prompt user whether they're sure they want to run this script
if (!config.noPrompt)
rl.question(
'Running this script will delete all existing documents in the collections specified in the configuration file. Are you sure you want to continue? (y/N) ',
(answer) => {
rl.close();
if (answer.toLowerCase() !== 'y') {
logInfo('Exiting');
process.exit(0);
}
},
);
loadPromise(run(), {
text: 'Performing a run',
successText: 'Run complete',
failText: 'Failed to perform a run',
})
.then(() => process.exit(0))
.catch(() => process.exit(1));
}
if (process.argv[2] === '--dry-run') startDryRun();
else startRun();