-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
148 lines (123 loc) · 3.67 KB
/
cli.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
#!/usr/bin/env node
// @ts-nocheck
// Require modules
const fs = require('fs').promises;
const path = require('path');
const chalk = require('chalk');
const { program } = require('commander');
const inquirer = require('inquirer');
const isThere = require("is-there").promises;
const mkdirp = require('mkdirp');
const cliProgress = require('cli-progress');
const wait = require('wait');
const isWritable = require("is-writable");
const pkg = require('./package.json');
const logger = {
/**
* @param {string} level
* @param {string|Error} message
* @param {NodeJS.WriteStream} out
*/
_format(level, message, out) {
out.write(`${chalk.dim('[')}${level}${chalk.dim(']')} > ${message}\n`);
},
/**
* Writes a newline to stdout.
*/
newline() {
process.stdout.write('\n');
},
/**
* Logs an informational message.
*
* @param {string|Error} message
*/
info(message) {
this._format(chalk.blue('info'), message, process.stdout);
},
/**
* Logs a warning message.
*
* @param {string} message
*/
warn(message) {
this._format(chalk.yellow('warn'), message, process.stderr);
},
/**
* Logs an error message.
*
* @param {string|Error} message
*/
error(message) {
this._format(chalk.red('error'), message, process.stderr);
},
/**
* Logs an error message and terminates the program.
* @param {string|Error} message
*/
fail(message) {
this.error(message);
process.exit(-1);
}
};
program
.name('cli')
.version(pkg.version)
.description('Installs the theme files to a h5ai installation.')
.arguments('<directory>');
program
.option('--dry-run', 'runs the program without modifying any files');
program.on('option:dry-run', () => {
logger.warn('Dry run enabled! Installation will be simulated.');
});
program.action(async (dir, options) => {
const directory = path.resolve(dir);
const overrides = require('./overrides.json');
const h5aiTarget = path.join(directory, overrides.target);
const papirusBase = path.resolve(overrides.papirusBase);
if (! options.dryRun) {
if (! await isWritable(directory).catch(logger.fail.bind(logger))) {
logger.fail(`Directory ${chalk.underline(directory)} is not writable!`);
}
if (! await isThere.directory(directory).catch(logger.fail.bind(logger))) {
logger.fail(`Directory ${chalk.underline(directory)} does not exist!`);
}
await mkdirp(h5aiTarget);
for (const dirToCheck of [h5aiTarget, papirusBase]) {
if (! await isThere.directory(dirToCheck).catch(logger.fail.bind(logger))) {
logger.fail(`Directory ${chalk.underline(dirToCheck)} does not exist!`);
}
}
}
logger.newline();
const progressBar = new cliProgress.SingleBar({
format: `Working... | ${chalk.cyan('{bar}')} | ${chalk.magenta('{percentage}%')} || ${chalk.blue('{value}')}/${chalk.blue('{total}')} files copied`,
hideCursor: true
}, cliProgress.Presets.shades_classic);
const toCopy = Object.keys(overrides.copy);
progressBar.start(toCopy.length, 0);
for (const targetFile of toCopy) {
const targetAbsolute = path.join(h5aiTarget, targetFile);
const sourceAbsolute = path.join(papirusBase, overrides.copy[targetFile]);
try {
if (options.dryRun) {
await wait(250);
} else {
await fs.copyFile(sourceAbsolute, targetAbsolute);
}
} catch (error) {
logger.error(error);
}
progressBar.increment();
}
logger.newline();
progressBar.stop();
logger.info('Done.');
});
(async () => {
if (process.argv.slice(2, process.argv.length).length === 0) {
program.help();
} else {
await program.parseAsync(process.argv);
}
})();