forked from commitizen/cz-jira-smart-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·85 lines (77 loc) · 2.4 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
#!/usr/bin/env node
const fs = require("fs");
const readPackage = require("read-pkg");
const writePackage = require("write-pkg");
const filepaths = [".cz.json", "index.js", "validate.js"];
const destinationFolder = "./.commitconfig/";
const forceFlags = process.argv.filter(
(variable) => variable === "-f" || variable === "-F" || variable === "--force"
);
let endMessage = "All good";
function makeFile(filepath) {
try {
const fileContent = fs.readFileSync(
`node_modules/transparent-commitizen/.commitconfig/${filepath}`
);
fs.writeFileSync(`.commitconfig/${filepath}`, fileContent);
} catch {
console.log(
`Could not copy over ${filepath}, try copying it from node_modules/transparent-commitizen/.commitconfig/ to ${destinationFolder} manually`
);
}
}
function moveFiles() {
try {
if (fs.existsSync(destinationFolder)) {
console.log(
"Did not create" + destinationFolder + "because it already exists"
);
} else {
fs.mkdirSync(destinationFolder);
}
} catch (err) {
console.error(err);
}
filepaths.forEach((filepath) => {
try {
if (fs.existsSync(`${destinationFolder + filepath}`)) {
if (forceFlags.length > 0) {
makeFile(filepath);
} else {
console.log(`Did not edit ${filepath} because it already exists.`);
endMessage =
"\nTo perform a fresh install use the -F flag, or copy and edit files manually";
}
} else {
makeFile(filepath);
}
} catch (err) {
console.error(err);
}
});
}
function editPackageJson() {
const packageJson = readPackage.sync();
if (
forceFlags.length === 0 &&
packageJson.config &&
packageJson.config.commitizen
) {
console.log(
`Did not edit package.json because there seems to already be a commitizen config.`
);
endMessage =
"\nTo perform a fresh install use the -F flag, or copy and edit files manually";
return;
}
writePackage.sync(`${destinationFolder}backup`, packageJson)
packageJson.config
? (packageJson.config.commitizen = { path: destinationFolder })
: (packageJson.config = { commitizen: { path: destinationFolder } });
writePackage.sync(packageJson);
}
moveFiles();
editPackageJson();
console.log(endMessage);
// editPackage doesn't edit if there's already a config and no force flag
// the value of destination folder is derived from -n --name flag