aboutsummaryrefslogtreecommitdiff
path: root/node_modules/jake/lib/utils
diff options
context:
space:
mode:
authorMinteck <minteck@phoenixnet.tech>2021-10-17 20:08:04 +0200
committerMinteck <minteck@phoenixnet.tech>2021-10-17 20:08:04 +0200
commit3a94acf27ea1eff8bd1125450f07c0c366332e80 (patch)
tree5bd619df0b7e04afc174df98736a527477522acb /node_modules/jake/lib/utils
downloadwolfeye-js-3a94acf27ea1eff8bd1125450f07c0c366332e80.tar.gz
wolfeye-js-3a94acf27ea1eff8bd1125450f07c0c366332e80.tar.bz2
wolfeye-js-3a94acf27ea1eff8bd1125450f07c0c366332e80.zip
Initial commit
Diffstat (limited to 'node_modules/jake/lib/utils')
-rw-r--r--node_modules/jake/lib/utils/file.js286
-rw-r--r--node_modules/jake/lib/utils/index.js297
-rw-r--r--node_modules/jake/lib/utils/logger.js24
3 files changed, 607 insertions, 0 deletions
diff --git a/node_modules/jake/lib/utils/file.js b/node_modules/jake/lib/utils/file.js
new file mode 100644
index 0000000..a436def
--- /dev/null
+++ b/node_modules/jake/lib/utils/file.js
@@ -0,0 +1,286 @@
+/*
+ * Utilities: A classic collection of JavaScript utilities
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+
+let fs = require('fs');
+let path = require('path');
+
+/**
+ @name file
+ @namespace file
+*/
+
+let fileUtils = new (function () {
+
+ // Recursively copy files and directories
+ let _copyFile = function (fromPath, toPath, opts) {
+ let from = path.normalize(fromPath)
+ let to = path.normalize(toPath)
+ let options = opts || {}
+ let fromStat;
+ let toStat;
+ let destExists;
+ let destDoesNotExistErr;
+ let content;
+ let filename;
+ let dirContents;
+ let targetDir;
+
+ fromStat = fs.statSync(from);
+
+ try {
+ //console.dir(to + ' destExists');
+ toStat = fs.statSync(to);
+ destExists = true;
+ }
+ catch(e) {
+ //console.dir(to + ' does not exist');
+ destDoesNotExistErr = e;
+ destExists = false;
+ }
+ // Destination dir or file exists, copy into (directory)
+ // or overwrite (file)
+ if (destExists) {
+
+ // If there's a rename-via-copy file/dir name passed, use it.
+ // Otherwise use the actual file/dir name
+ filename = options.rename || path.basename(from);
+
+ // Copying a directory
+ if (fromStat.isDirectory()) {
+ dirContents = fs.readdirSync(from);
+ targetDir = path.join(to, filename);
+ // We don't care if the target dir already exists
+ try {
+ fs.mkdirSync(targetDir, {mode: fromStat.mode & 0o777});
+ }
+ catch(e) {
+ if (e.code !== 'EEXIST') {
+ throw e;
+ }
+ }
+ for (let i = 0, ii = dirContents.length; i < ii; i++) {
+ _copyFile(path.join(from, dirContents[i]), targetDir, {preserveMode: options.preserveMode});
+ }
+ }
+ // Copying a file
+ else {
+ content = fs.readFileSync(from);
+ let mode = fromStat.mode & 0o777;
+ let targetFile = to;
+
+ if (toStat.isDirectory()) {
+ targetFile = path.join(to, filename);
+ }
+
+ let fileExists = fs.existsSync(targetFile);
+ fs.writeFileSync(targetFile, content);
+
+ // If the file didn't already exist, use the original file mode.
+ // Otherwise, only update the mode if preserverMode is true.
+ if(!fileExists || options.preserveMode) {
+ fs.chmodSync(targetFile, mode);
+ }
+ }
+ }
+ // Dest doesn't exist, can't create it
+ else {
+ throw destDoesNotExistErr;
+ }
+ };
+
+ // Remove the given directory
+ let _rmDir = function (dirPath) {
+ let dir = path.normalize(dirPath);
+ let paths = [];
+ paths = fs.readdirSync(dir);
+ paths.forEach(function (p) {
+ let curr = path.join(dir, p);
+ let stat = fs.lstatSync(curr);
+ if (stat.isDirectory()) {
+ _rmDir(curr);
+ }
+ else {
+ try {
+ fs.unlinkSync(curr);
+ } catch(e) {
+ if (e.code === 'EPERM') {
+ fs.chmodSync(curr, parseInt(666, 8));
+ fs.unlinkSync(curr);
+ } else {
+ throw e;
+ }
+ }
+ }
+ });
+ fs.rmdirSync(dir);
+ };
+
+ /**
+ @name file#cpR
+ @public
+ @function
+ @description Copies a directory/file to a destination
+ @param {String} fromPath The source path to copy from
+ @param {String} toPath The destination path to copy to
+ @param {Object} opts Options to use
+ @param {Boolean} [opts.preserveMode] If target file already exists, this
+ determines whether the original file's mode is copied over. The default of
+ false mimics the behavior of the `cp` command line tool. (Default: false)
+ */
+ this.cpR = function (fromPath, toPath, options) {
+ let from = path.normalize(fromPath);
+ let to = path.normalize(toPath);
+ let toStat;
+ let doesNotExistErr;
+ let filename;
+ let opts = options || {};
+
+ if (from == to) {
+ throw new Error('Cannot copy ' + from + ' to itself.');
+ }
+
+ // Handle rename-via-copy
+ try {
+ toStat = fs.statSync(to);
+ }
+ catch(e) {
+ doesNotExistErr = e;
+
+ // Get abs path so it's possible to check parent dir
+ if (!this.isAbsolute(to)) {
+ to = path.join(process.cwd(), to);
+ }
+
+ // Save the file/dir name
+ filename = path.basename(to);
+ // See if a parent dir exists, so there's a place to put the
+ /// renamed file/dir (resets the destination for the copy)
+ to = path.dirname(to);
+ try {
+ toStat = fs.statSync(to);
+ }
+ catch(e) {}
+ if (toStat && toStat.isDirectory()) {
+ // Set the rename opt to pass to the copy func, will be used
+ // as the new file/dir name
+ opts.rename = filename;
+ //console.log('filename ' + filename);
+ }
+ else {
+ throw doesNotExistErr;
+ }
+ }
+
+ _copyFile(from, to, opts);
+ };
+
+ /**
+ @name file#mkdirP
+ @public
+ @function
+ @description Create the given directory(ies) using the given mode permissions
+ @param {String} dir The directory to create
+ @param {Number} mode The mode to give the created directory(ies)(Default: 0755)
+ */
+ this.mkdirP = function (dir, mode) {
+ let dirPath = path.normalize(dir);
+ let paths = dirPath.split(/\/|\\/);
+ let currPath = '';
+ let next;
+
+ if (paths[0] == '' || /^[A-Za-z]+:/.test(paths[0])) {
+ currPath = paths.shift() || '/';
+ currPath = path.join(currPath, paths.shift());
+ //console.log('basedir');
+ }
+ while ((next = paths.shift())) {
+ if (next == '..') {
+ currPath = path.join(currPath, next);
+ continue;
+ }
+ currPath = path.join(currPath, next);
+ try {
+ //console.log('making ' + currPath);
+ fs.mkdirSync(currPath, mode || parseInt(755, 8));
+ }
+ catch(e) {
+ if (e.code != 'EEXIST') {
+ throw e;
+ }
+ }
+ }
+ };
+
+ /**
+ @name file#rmRf
+ @public
+ @function
+ @description Deletes the given directory/file
+ @param {String} p The path to delete, can be a directory or file
+ */
+ this.rmRf = function (p, options) {
+ let stat;
+ try {
+ stat = fs.lstatSync(p);
+ if (stat.isDirectory()) {
+ _rmDir(p);
+ }
+ else {
+ fs.unlinkSync(p);
+ }
+ }
+ catch (e) {}
+ };
+
+ /**
+ @name file#isAbsolute
+ @public
+ @function
+ @return {Boolean/String} If it's absolute the first character is returned otherwise false
+ @description Checks if a given path is absolute or relative
+ @param {String} p Path to check
+ */
+ this.isAbsolute = function (p) {
+ let match = /^[A-Za-z]+:\\|^\//.exec(p);
+ if (match && match.length) {
+ return match[0];
+ }
+ return false;
+ };
+
+ /**
+ @name file#absolutize
+ @public
+ @function
+ @return {String} Returns the absolute path for the given path
+ @description Returns the absolute path for the given path
+ @param {String} p The path to get the absolute path for
+ */
+ this.absolutize = function (p) {
+ if (this.isAbsolute(p)) {
+ return p;
+ }
+ else {
+ return path.join(process.cwd(), p);
+ }
+ };
+
+})();
+
+module.exports = fileUtils;
+
diff --git a/node_modules/jake/lib/utils/index.js b/node_modules/jake/lib/utils/index.js
new file mode 100644
index 0000000..17d686b
--- /dev/null
+++ b/node_modules/jake/lib/utils/index.js
@@ -0,0 +1,297 @@
+/*
+ * Jake JavaScript build tool
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+
+
+let util = require('util'); // Native Node util module
+let spawn = require('child_process').spawn;
+let EventEmitter = require('events').EventEmitter;
+let logger = require('./logger');
+let file = require('./file');
+let Exec;
+
+const _UUID_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
+
+let parseArgs = function (argumentsObj) {
+ let args;
+ let arg;
+ let cmds;
+ let callback;
+ let opts = {
+ interactive: false,
+ printStdout: false,
+ printStderr: false,
+ breakOnError: true
+ };
+
+ args = Array.prototype.slice.call(argumentsObj);
+
+ cmds = args.shift();
+ // Arrayize if passed a single string command
+ if (typeof cmds == 'string') {
+ cmds = [cmds];
+ }
+ // Make a copy if it's an actual list
+ else {
+ cmds = cmds.slice();
+ }
+
+ // Get optional callback or opts
+ while((arg = args.shift())) {
+ if (typeof arg == 'function') {
+ callback = arg;
+ }
+ else if (typeof arg == 'object') {
+ opts = Object.assign(opts, arg);
+ }
+ }
+
+ // Backward-compat shim
+ if (typeof opts.stdout != 'undefined') {
+ opts.printStdout = opts.stdout;
+ delete opts.stdout;
+ }
+ if (typeof opts.stderr != 'undefined') {
+ opts.printStderr = opts.stderr;
+ delete opts.stderr;
+ }
+
+ return {
+ cmds: cmds,
+ opts: opts,
+ callback: callback
+ };
+};
+
+/**
+ @name jake
+ @namespace jake
+*/
+let utils = new (function () {
+ /**
+ @name jake.exec
+ @static
+ @function
+ @description Executes shell-commands asynchronously with an optional
+ final callback.
+ `
+ @param {String[]} cmds The list of shell-commands to execute
+ @param {Object} [opts]
+ @param {Boolean} [opts.printStdout=false] Print stdout from each command
+ @param {Boolean} [opts.printStderr=false] Print stderr from each command
+ @param {Boolean} [opts.breakOnError=true] Stop further execution on
+ the first error.
+ @param {Boolean} [opts.windowsVerbatimArguments=false] Don't translate
+ arguments on Windows.
+ @param {Function} [callback] Callback to run after executing the
+ commands
+
+ @example
+ let cmds = [
+ 'echo "showing directories"'
+ , 'ls -al | grep ^d'
+ , 'echo "moving up a directory"'
+ , 'cd ../'
+ ]
+ , callback = function () {
+ console.log('Finished running commands.');
+ }
+ jake.exec(cmds, {stdout: true}, callback);
+ */
+ this.exec = function (a, b, c) {
+ let parsed = parseArgs(arguments);
+ let cmds = parsed.cmds;
+ let opts = parsed.opts;
+ let callback = parsed.callback;
+
+ let ex = new Exec(cmds, opts, callback);
+
+ ex.addListener('error', function (msg, code) {
+ if (opts.breakOnError) {
+ fail(msg, code);
+ }
+ });
+ ex.run();
+
+ return ex;
+ };
+
+ this.createExec = function (a, b, c) {
+ return new Exec(a, b, c);
+ };
+
+ // From Math.uuid.js, https://github.com/broofa/node-uuid
+ // Robert Kieffer (robert@broofa.com), MIT license
+ this.uuid = function (length, radix) {
+ var chars = _UUID_CHARS
+ , uuid = []
+ , r
+ , i;
+
+ radix = radix || chars.length;
+
+ if (length) {
+ // Compact form
+ i = -1;
+ while (++i < length) {
+ uuid[i] = chars[0 | Math.random()*radix];
+ }
+ } else {
+ // rfc4122, version 4 form
+
+ // rfc4122 requires these characters
+ uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
+ uuid[14] = '4';
+
+ // Fill in random data. At i==19 set the high bits of clock sequence as
+ // per rfc4122, sec. 4.1.5
+ i = -1;
+ while (++i < 36) {
+ if (!uuid[i]) {
+ r = 0 | Math.random()*16;
+ uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
+ }
+ }
+ }
+
+ return uuid.join('');
+ };
+
+})();
+
+Exec = function () {
+ let parsed = parseArgs(arguments);
+ let cmds = parsed.cmds;
+ let opts = parsed.opts;
+ let callback = parsed.callback;
+
+ this._cmds = cmds;
+ this._callback = callback;
+ this._config = opts;
+};
+
+util.inherits(Exec, EventEmitter);
+
+Object.assign(Exec.prototype, new (function () {
+
+ let _run = function () {
+ let self = this;
+ let sh;
+ let cmd;
+ let args;
+ let next = this._cmds.shift();
+ let config = this._config;
+ let errData = '';
+ let shStdio;
+ let handleStdoutData = function (data) {
+ self.emit('stdout', data);
+ };
+ let handleStderrData = function (data) {
+ let d = data.toString();
+ self.emit('stderr', data);
+ // Accumulate the error-data so we can use it as the
+ // stack if the process exits with an error
+ errData += d;
+ };
+
+ // Keep running as long as there are commands in the array
+ if (next) {
+ let spawnOpts = {};
+ this.emit('cmdStart', next);
+
+ // Ganking part of Node's child_process.exec to get cmdline args parsed
+ if (process.platform == 'win32') {
+ cmd = 'cmd';
+ args = ['/c', next];
+ if (config.windowsVerbatimArguments) {
+ spawnOpts.windowsVerbatimArguments = true;
+ }
+ }
+ else {
+ cmd = '/bin/sh';
+ args = ['-c', next];
+ }
+
+ if (config.interactive) {
+ spawnOpts.stdio = 'inherit';
+ sh = spawn(cmd, args, spawnOpts);
+ }
+ else {
+ shStdio = [
+ process.stdin
+ ];
+ if (config.printStdout) {
+ shStdio.push(process.stdout);
+ }
+ else {
+ shStdio.push('pipe');
+ }
+ if (config.printStderr) {
+ shStdio.push(process.stderr);
+ }
+ else {
+ shStdio.push('pipe');
+ }
+ spawnOpts.stdio = shStdio;
+ sh = spawn(cmd, args, spawnOpts);
+ if (!config.printStdout) {
+ sh.stdout.addListener('data', handleStdoutData);
+ }
+ if (!config.printStderr) {
+ sh.stderr.addListener('data', handleStderrData);
+ }
+ }
+
+ // Exit, handle err or run next
+ sh.on('exit', function (code) {
+ let msg;
+ if (code !== 0) {
+ msg = errData || 'Process exited with error.';
+ msg = msg.trim();
+ self.emit('error', msg, code);
+ }
+ if (code === 0 || !config.breakOnError) {
+ self.emit('cmdEnd', next);
+ setTimeout(function () { _run.call(self); }, 0);
+ }
+ });
+
+ }
+ else {
+ self.emit('end');
+ if (typeof self._callback == 'function') {
+ self._callback();
+ }
+ }
+ };
+
+ this.append = function (cmd) {
+ this._cmds.push(cmd);
+ };
+
+ this.run = function () {
+ _run.call(this);
+ };
+
+})());
+
+utils.Exec = Exec;
+utils.file = file;
+utils.logger = logger;
+
+module.exports = utils;
+
diff --git a/node_modules/jake/lib/utils/logger.js b/node_modules/jake/lib/utils/logger.js
new file mode 100644
index 0000000..8f72686
--- /dev/null
+++ b/node_modules/jake/lib/utils/logger.js
@@ -0,0 +1,24 @@
+let util = require('util');
+
+let logger = new (function () {
+ let _output = function (type, out) {
+ let quiet = typeof jake != 'undefined' && jake.program &&
+ jake.program.opts && jake.program.opts.quiet;
+ let msg;
+ if (!quiet) {
+ msg = typeof out == 'string' ? out : util.inspect(out);
+ console[type](msg);
+ }
+ };
+
+ this.log = function (out) {
+ _output('log', out);
+ };
+
+ this.error = function (out) {
+ _output('error', out);
+ };
+
+})();
+
+module.exports = logger;