-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·114 lines (99 loc) · 2.93 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
107
108
109
110
111
112
113
114
#!/usr/bin/env node
const { program } = require('commander');
const ora = require('ora');
const { formatModuleNameAndVersion } = require('./lib/utils');
const {
moduleIsValid,
createModuleConfig,
createModuleDir,
initModule,
installDeps,
createIndex,
runModule
} = require('./lib/modules');
program.version('1.0.7');
program
.option('-c, --call-module-as-function', 'call the exported module as a function intead of object')
.option('-f, --function-name [functionName]', 'call a specific function from exported module')
.option('-n, --npm-module-version [version]', 'run a specific version of the npm module')
.option('-p, --params [parameters...]', 'list of params that will be passed to the module/function call')
.option('-s, --silent', 'print only the module output, without progress or logs')
.option('-t, --transform-output [transformFunction]', 'define a function to modify module/function return')
.option('-u, --sub-module [subModule]', 'import a submodule, such as "crypto-js/sha256"')
program.parse(process.argv);
const moduleName = program.args[0];
const namedExport = program.args[1];
const {
silent,
npmModuleVersion,
functionName,
params,
callModuleAsFunction,
subModule,
transformOutput
} = program.opts();
const steps = {
registry: 'Checking module on npm registry',
install: 'Installing dependencies',
run: `Running ${formatModuleNameAndVersion(moduleName, npmModuleVersion)}`,
fail: `Fail running ${formatModuleNameAndVersion(moduleName, npmModuleVersion)}`
};
if (!moduleName) {
console.error('the module name is required. \n$ runit-on-cli <module-name>');
return;
}
let spinner;
if(!silent) {
spinner = ora(steps.registry).start();
}
if (!moduleIsValid(moduleName)) {
if(!silent) {
spinner.fail(`NPM module ${moduleName} is invalid.`);
}
return;
}
if(!silent) {
spinner.succeed(steps.registry);
}
(async () => {
createModuleConfig(moduleName, npmModuleVersion);
await createModuleDir(moduleName);
initModule(moduleName);
if(!silent) {
spinner.start(steps.install);
}
installDeps(moduleName);
if(!silent) {
spinner.succeed(steps.install);
}
await createIndex(
moduleName,
namedExport,
functionName,
params,
callModuleAsFunction,
subModule,
transformOutput
);
if(!silent) {
spinner.start(steps.run);
}
try {
const result = await runModule(moduleName);
if(!silent) {
spinner.succeed(steps.run);
console.log('\n===== Result =====');
console.log(result);
console.log('==================\n');
} else {
console.log(result);
}
} catch (error) {
if(!silent) {
spinner.fail(steps.fail);
console.log(error);
} else {
console.log(error);
}
}
})();