UNPKG

1.54 kBJavaScriptView Raw
1'use strict'
2
3var spawn = require('child_process').spawnSync
4var fs = require('fs-extra')
5var path = require('path')
6
7function cliContext (commands, dir) {
8 return {
9 cmdName:'test',
10 command: commands.test,
11 cwd: process.cwd(),
12 resDir: path.join(dir, '../res'),
13 resources: { lint: ['.eslintrc'] },
14 }
15}
16
17function copyResourcesToTarget(context) {
18 var allResources = context.resources[context.cmdName];
19 if (!allResources) {
20 return;
21 }
22
23 allResources.forEach(function(res) {
24 var srcFile = path.join(context.resDir, res);
25 var targetFile = path.join(context.cwd, res);
26 fs.copySync(srcFile, targetFile);
27 });
28}
29
30function cleanTargetResources(context) {
31 var allResources = context.resources[context.cmdName];
32 if (!allResources) {
33 return;
34 }
35
36 allResources.forEach(function(res) {
37 var targetFile = path.join(context.cwd, res);
38 fs.removeSync(targetFile);
39 });
40}
41
42function exec (commands, dir) {
43
44 let context = cliContext(commands, dir)
45
46 if (process.argv.length >= 3) {
47 context.cmdName = process.argv[2]
48 context.command = commands[context.cmdName]
49 }
50
51 if (!context.command) {
52 process.stderr.write('Savor: invalid command\n')
53 process.exit(1)
54 }
55
56 copyResourcesToTarget(context)
57
58 context.command.forEach(function(subcommand) {
59 spawn(subcommand.bin, subcommand.args, {
60 cwd: context.cwd,
61 stdio: "inherit"
62 })
63 })
64
65 cleanTargetResources(context)
66}
67
68module.exports = {
69 exec
70}