1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const
|
15 | appc = require('node-appc'),
|
16 | EventEmitter = require('events').EventEmitter,
|
17 | fs = require('fs'),
|
18 | __ = appc.i18n(__dirname).__;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | exports.magik = function magik(options, callback, body) {
|
31 | var emitter = new EventEmitter;
|
32 | emitter.on('error', function () {});
|
33 |
|
34 | process.nextTick(function () {
|
35 | if (typeof options === 'function') {
|
36 | callback = options;
|
37 | options = {};
|
38 | } else if (!options) {
|
39 | options = {};
|
40 | }
|
41 | typeof callback === 'function' || (callback = function () {});
|
42 |
|
43 | if (process.platform !== 'win32') {
|
44 | var err = new Error(__('Unsupported platform "%s"', process.platform));
|
45 | emitter.emit('error', err);
|
46 | return callback(err);
|
47 | }
|
48 |
|
49 | body(emitter, options, callback);
|
50 | });
|
51 |
|
52 | return emitter;
|
53 | };
|
54 |
|
55 | exports.mix = function mix(src, dest) {
|
56 | Object.keys(src).forEach(function (name) {
|
57 | if (Array.isArray(src[name])) {
|
58 | if (Array.isArray(dest[name])) {
|
59 | dest[name] = dest[name].concat(src[name]);
|
60 | } else {
|
61 | dest[name] = src[name];
|
62 | }
|
63 | } else if (src[name] !== null && typeof src[name] === 'object') {
|
64 | dest[name] || (dest[name] = {});
|
65 | Object.keys(src[name]).forEach(function (key) {
|
66 | dest[name][key] = src[name][key];
|
67 | });
|
68 | } else {
|
69 | dest[name] = src[name];
|
70 | }
|
71 | });
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | exports.checkOutdated = function checkOutdated(srcFile, destFile, callback) {
|
84 |
|
85 | fs.stat(destFile, function (err, stats) {
|
86 | if (err) {
|
87 |
|
88 | if (err.code === 'ENOENT') {
|
89 | return callback(null, true);
|
90 | }
|
91 |
|
92 | return callback(err);
|
93 | }
|
94 |
|
95 |
|
96 | var sourceStats = fs.statSync(srcFile);
|
97 | if (sourceStats.mtime > stats.mtime) {
|
98 |
|
99 | return fs.unlink(destFile, function (err) {
|
100 | if (err) {
|
101 | return callback(err);
|
102 | }
|
103 | return callback(null, true);
|
104 | });
|
105 | }
|
106 | return callback(null, false);
|
107 | });
|
108 | };
|