-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
86 lines (69 loc) · 2.41 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
#!/usr/bin/env node
require('dotenv').config();
const chalk = require('chalk');
const fs = require('fs');
const mri = require('mri');
const ora = require('ora');
const path = require('path');
const prettier = require('prettier');
const util = require('util');
const Wizard = require('./lib/wizard');
(async () => {
const configPath = path.join(process.cwd(), 'sourcebit.js');
let currentConfig = {};
try {
currentConfig = require(configPath);
} catch (_) {}
const parameters = mri(process.argv.slice(2));
const wizard = new Wizard(parameters);
const { errors, plugins, envFileOptions } = await wizard.start({
currentConfig
});
const moduleExports = util.inspect(
{
plugins
},
{ compact: false, depth: null }
);
let config = `module.exports = ${moduleExports}\n`;
try {
config = prettier.format(config, { parser: 'babel', trailingComma: 'none' });
} catch (error) {
ora('Could not format configuration file.').warn();
}
console.log('');
// Writing configuration file.
try {
fs.writeFileSync(configPath, config);
const errorCount = Object.keys(errors).length;
if (errorCount > 0) {
ora(
`Configuration saved to ${chalk.bold(configPath)}, but ${chalk.bold(errorCount)} ${
errorCount > 1 ? 'errors' : 'error'
} occurred.\n`
).warn();
} else {
ora(`Configuration saved to ${chalk.bold(configPath)}.\n`).succeed();
}
} catch (error) {
ora('ERROR: Could not create configuration file.').fail();
console.log(error);
process.exit(1);
}
// Writing env file.
try {
const envFileData = Object.keys(envFileOptions)
.map((key) => `${key}=${envFileOptions[key]}`)
.join('\n');
if (envFileData.length > 0) {
const envFilePath = path.join(process.cwd(), '.env');
fs.writeFileSync(envFilePath, envFileData + '\n');
ora(`Environment file saved to ${chalk.bold(envFilePath)}.`).succeed();
ora(`Make sure not to commit this file to version control. Perhaps you want to add it to ${chalk.bold('.gitignore')}?`).warn();
}
} catch (error) {
console.log(error);
ora('Could not create environment file.\n').fail();
process.exit(1);
}
})();