-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·92 lines (79 loc) · 2.18 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
#!/usr/bin/env node
const { promisify } = require("util");
const path = require("path");
const fs = require("fs");
const consola = require("consola");
const inquirer = require("inquirer");
const openAsync = promisify(fs.open);
const copyFileAsync = promisify(fs.copyFile);
const cliDir = __dirname;
const userDir = process.cwd();
const space = () => console.log(" ");
const prConfig = {
fileName: "PULL_REQUEST_TEMPLATE.md",
textName: "pull request"
};
const issueConfig = {
fileName: "ISSUE_TEMPLATE.md",
textName: "issue"
};
const createInquirerConfig = fileName => ({
type: "confirm",
name: "shouldWrite",
message: `The file ${fileName} already exists. Should it be replaced with this new version?`,
default: false
});
const startFileCreationSequence = async ({ fileName, textName }) => {
// Directories.
const cliFile = path.resolve(cliDir, fileName);
const userFile = path.resolve(userDir, fileName);
// Flags.
let shouldWrite = true;
// Check existence.
try {
await openAsync(userFile, "wx");
} catch (error) {
const inquirerResult = await inquirer.prompt([
createInquirerConfig(fileName)
]);
shouldWrite = inquirerResult.shouldWrite;
}
// Write.
if (shouldWrite) {
await copyFileAsync(cliFile, userFile);
consola.info(`Created new ${textName} template file.`);
} else {
consola.info(`Skiped ${textName} template file creation.`);
}
return shouldWrite;
};
(async () => {
consola.success("Starting sequence.");
const completed = [];
for (const { fileName, textName } of [prConfig, issueConfig]) {
space();
try {
const isComplete = await startFileCreationSequence({
fileName,
textName
});
if (isComplete) {
completed.push(fileName);
}
} catch (error) {
consola.fatal(
`Sorry, there was an error creating your ${fileName} template file.`
);
process.exit(1);
}
}
space();
if (completed.length) {
consola.success(`Sequence complete. Created file(s):`);
completed.forEach(fileName => consola.log(`> ${fileName}`));
} else {
consola.success("Sequence completed but created no files.");
}
space();
process.exit(0);
})();