UNPKG

2.66 kBJavaScriptView Raw
1"use strict";
2
3const argx = require('argx'),
4 apemanfile = require('apemanfile'),
5 fs = require('fs'),
6 os = require('os'),
7 path = require('path'),
8 stringcase = require('stringcase'),
9 startDaemon = require('./daemoning/start_daemon'),
10 stopDaemon = require('./daemoning/stop_daemon'),
11 showDaemon = require('./daemoning/show_daemon'),
12 deleteDaemon = require('./daemoning/delete_daemon'),
13 kill = require('./processing/kill'),
14 async = require('async');
15
16/** @lends apemanD */
17function apemanD(action, script, options, callback) {
18 let args = argx(arguments);
19 action = args.shift('string');
20 script = args.shift('string');
21 callback = args.pop('function') || argx.noop;
22 options = args.pop('object') || {};
23
24 async.waterfall([
25 (callback) => {
26 apemanfile.load(options.configuration, callback);
27 },
28 (apemanfile, callback) => {
29 let here = process.cwd(),
30 cwd = apemanfile.$cwd || process.cwd();
31
32 async.series([
33 (callback) => {
34 fs.exists(cwd, (exists) => {
35 if (exists) {
36 process.chdir(cwd);
37 callback(null);
38 } else {
39 callback(new Error(`[apeman-d] cwd not exists: ${cwd}`));
40 }
41 });
42 },
43 (callback) => {
44 switch (String(action).trim()) {
45 case 'start':
46 startDaemon(apemanfile, script, options, callback);
47 break;
48 case 'stop':
49 stopDaemon(apemanfile, script, options, callback);
50 break;
51 case 'show':
52 showDaemon(apemanfile, script, options, callback);
53 break;
54 case 'delete':
55 deleteDaemon(apemanfile, script, options, callback);
56 break;
57 }
58 },
59 (callback) => {
60 process.chdir(here);
61 callback(null);
62 }
63 ], (err, results) => {
64 callback(err, results[1]);
65 });
66 }
67
68 ], callback);
69}
70apemanD.start = apemanD.bind(apemanD, 'start');
71apemanD.stop = apemanD.bind(apemanD, 'stop');
72apemanD.show = apemanD.bind(apemanD, 'show');
73apemanD.delete = apemanD.bind(apemanD, 'delete');
74
75apemanD.kill = kill;
76
77module.exports = apemanD;