-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
83 lines (74 loc) · 2.5 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
#!/usr/bin/env node
const _ = require('lodash');
const figlet = require('figlet');
const chalk = require('chalk');
const checkDockerIsInstalled = require('./lib/checkCommands').checkDockerIsInstalled;
const commandLineArgs = require('command-line-args');
const engine = require('./lib/engine');
const fs = require('fs');
const path = require('path');
const optionDefinitions = require('./lib/options');
const usage = require('./lib/usage');
const version = require('./package.json').version;
const wizzard = require('./lib/wizzard');
const hawkinitWizzard = (save, timeout, full) => {
// clear the screen
console.log('\x1B[2J');
// show the ascii logo
const logo = fs.readFileSync(path.join(__dirname, 'logo'), 'utf8');
console.log(logo);
// .. and the ascii label
process.stdout.write(
chalk.blue(
figlet.textSync('Hawkinit', {
horizontalLayout: 'fitted'
}).slice(0, -50)
)
);
console.log(chalk.grey(` (v${version})\n`));
checkDockerIsInstalled(() => wizzard.show(save, timeout, full));
};
const hawkinitRun = (answers, timeout) => {
checkDockerIsInstalled(() => engine.run(answers, timeout));
};
const printVersion = () => console.log(version);
const parseArgs = () => {
try {
const options = commandLineArgs(optionDefinitions);
if (_.isEmpty(options)) {
hawkinitWizzard();
} else if (options.version) {
printVersion();
} else if (options.help) {
usage.printUsage();
} else {
const save = options['save-answers'];
const full = options.full;
const timeout = !isNaN(options.timeout) ? options.timeout : 0;
const answerFile = options['answer-file'];
if (answerFile) {
if (!answerFile.exists) {
console.log(chalk.red(`The specified answer file ${answerFile.filename} doesn't exist.`));
process.exit(1);
}
const answers = fs.readFileSync(answerFile.filename, 'utf8');
console.log(chalk.green(`Running the hawkinit with answer file ${answerFile.filename}`));
try {
const parsedAnswers = JSON.parse(answers);
hawkinitRun(parsedAnswers, timeout);
} catch (err) {
console.log(chalk.red(`Unable to parse the specified answer file ${answerFile.filename}.
\nBecause of:\n ${err}`));
process.exit(2);
}
} else {
hawkinitWizzard(save, timeout, full);
}
}
} catch (err) {
console.log(`\n${chalk.red(err.message)}`);
usage.printUsage();
process.exit(3);
}
};
parseArgs();