-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·106 lines (92 loc) · 3.45 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
const path = require ("path");
const npm = require (path.resolve (`${process.argv[0]}/../../lib/node_modules/npm`));
const chalk = require("chalk");
const figlet = require("figlet");
const fs = require ("fs");
const minimist = require ("minimist");
const runPackageScript = require ("./lib/runPackageScript.js");
const runCommandSync = require ("./lib/runCommandSync.js");
var params = minimist(process.argv.slice(2));
var multiPackage = {};
try {
multiPackage = require(path.resolve(`./multi-package.json`));
}
catch (ex){
if (params._.includes("verbose")) {
console.warn ("No multi-package.json found");
}
}
var internalCommands = {};
var packagesScripts = [];
function loadInternalCommands() {
if (params._.includes("verbose")) {
console.log ("loading internal commands..");
}
var defaultCommands = fs.readdirSync(`${__dirname}/commands`);
defaultCommands.forEach (defaultCommand => {
try {
internalCommands[defaultCommand] = require(`./commands/${defaultCommand}`);
}
catch (ex){
console.warn (`Command ${defaultCommand} wasn't loaded: ${ex}`);
}
});
if (params._.includes("verbose")) {
console.log (`Internal commands loaded: ${Object.keys (internalCommands)}`);
}
}
function loadCustomCommands() {
if (!multiPackage.packages) return;
var packagesScriptsArray = [];
Object.values(multiPackage.packages).forEach (packageConfig => {
packagesScriptsArray = packagesScriptsArray.concat(Object.keys(packageConfig.scripts || []));
});
packagesScripts = new Set (packagesScriptsArray);
packagesScripts.forEach (packageScript => {
if (multiPackage.scripts && Object.keys (multiPackage.scripts).includes (packageScript) ) {
console.warn (chalk.yellow(`[WARN] The script ${packageScript} found in packages will be ignored because the script name is already in use in the global custom scripts. Please choose a different name`));
}
})
}
function run () {
loadInternalCommands();
loadCustomCommands();
var mainCommand = process.argv[2], secondaryCommand = process.argv[3];
if (!mainCommand || mainCommand === "--help") {
console.log ();
console.log (`Commands:`);
console.log ();
Object.values(internalCommands)
.forEach (command => console.log(`${chalk.bold("\t" + command.name)}\t\t${command.title}`));
packagesScripts
.forEach (command => console.log(`${chalk.bold("\t" + command)}\t\tRun the '${command}' command`));
console.log ();
console.log ();
console.log ();
console.log ();
console.log ();
}
else if (mainCommand in internalCommands){
internalCommands[mainCommand].run(multiPackage, params);
}
else if (mainCommand in multiPackage.scripts) {
console.log (chalk.green(`Running custom command: '${chalk.bold(mainCommand)}'`));
try {
runCommandSync(multiPackage.scripts[mainCommand]);
}
catch (ex) {
console.error (`Error running script '${mainCommand}': ${ex}`);
}
}
else if (packagesScripts.has (mainCommand) || Object.keys(npm.commands).includes(mainCommand)) {
runPackageScript (mainCommand, multiPackage);
}
else {
console.log (chalk.red(`Hey, I don't know any command named '${mainCommand}', you are welcomed to add it..`));
}
};
run();
module.exports = {
run,
}