UNPKG

3.23 kBJavaScriptView Raw
1/**
2 * Utility functions used by windowslib.
3 *
4 * @module utilities
5 *
6 * @copyright
7 * Copyright (c) 2014 by Appcelerator, Inc. All Rights Reserved.
8 *
9 * @license
10 * Licensed under the terms of the Apache Public License.
11 * Please see the LICENSE included with this distribution for details.
12 */
13
14const
15 appc = require('node-appc'),
16 EventEmitter = require('events').EventEmitter,
17 fs = require('fs'),
18 __ = appc.i18n(__dirname).__;
19
20/**
21 * Creates an event emitter, validates that the platform is Windows,
22 * normalizes the 'options' and 'callback' arguments, and passes all
23 * these goodies to the 'body' function. It's magik!
24 *
25 * @param {Object} [options] - An object containing various settings.
26 * @param {Function} [callback(err, ...)] - A function to call with the task is complete. This is guaranteed to be called asynchronously.
27 *
28 * @returns {EventEmitter}
29 */
30exports.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
55exports.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 * Determine if an executable needs to be rebuilt, based on whether the destination
76 * file exists or has an older modified timestamp than the source file used to
77 * generate it.
78 *
79 * @param {String} srcFile - Path to the source file used to generate the exe
80 * @param {String} destFile - Path to the destination exe file
81 * @param {Function} [callback(err, outdated)] - A function to call after checking
82 **/
83exports.checkOutdated = function checkOutdated(srcFile, destFile, callback) {
84 // Be smart about rebuilding wstool if source is newer
85 fs.stat(destFile, function (err, stats) {
86 if (err) {
87 // file does not exist, build the tool and then run
88 if (err.code === 'ENOENT') {
89 return callback(null, true); // rebuild
90 }
91 // some other error
92 return callback(err);
93 }
94
95 // Compare src and dest modified times
96 var sourceStats = fs.statSync(srcFile);
97 if (sourceStats.mtime > stats.mtime) {
98 // delete generated file and and rebuild
99 return fs.unlink(destFile, function (err) {
100 if (err) {
101 return callback(err);
102 }
103 return callback(null, true); // rebuild
104 });
105 }
106 return callback(null, false); // run
107 });
108};