2020-02-21 00:59:18 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
const utils = {};
|
|
|
|
|
|
|
|
utils.isLinux = () => {
|
|
|
|
return process && process.platform === 'linux';
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.isWindows = () => {
|
|
|
|
return process && process.platform === 'win32';
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.isMac = () => {
|
|
|
|
return process && process.platform === 'darwin';
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.execCommand = function(command) {
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-06-03 18:07:50 +02:00
|
|
|
exec(command, { maxBuffer: 1024 * 1024 }, (error, stdout) => {
|
2020-02-21 00:59:18 +02:00
|
|
|
if (error) {
|
|
|
|
if (error.signal == 'SIGTERM') {
|
|
|
|
resolve('Process was killed');
|
|
|
|
} else {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve(stdout.trim());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.dirname = function(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
2020-03-14 01:46:14 +02:00
|
|
|
const s = path.split(/\/|\\/);
|
2020-02-21 00:59:18 +02:00
|
|
|
s.pop();
|
|
|
|
return s.join('/');
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.basename = function(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
2020-03-14 01:46:14 +02:00
|
|
|
const s = path.split(/\/|\\/);
|
2020-02-21 00:59:18 +02:00
|
|
|
return s[s.length - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.filename = function(path, includeDir = false) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
|
|
|
let output = includeDir ? path : utils.basename(path);
|
|
|
|
if (output.indexOf('.') < 0) return output;
|
|
|
|
|
|
|
|
output = output.split('.');
|
|
|
|
output.pop();
|
|
|
|
return output.join('.');
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.toSystemSlashes = function(path) {
|
|
|
|
if (utils.isWindows()) return path.replace(/\//g, '\\');
|
|
|
|
return path.replace(/\\/g, '/');
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.fileExtension = function(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const output = path.split('.');
|
2020-02-21 00:59:18 +02:00
|
|
|
if (output.length <= 1) return '';
|
|
|
|
return output[output.length - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.replaceFileText = async function(filePath, regex, toInsert) {
|
|
|
|
const content = await fs.readFile(filePath, 'utf8');
|
|
|
|
const newContent = content.replace(regex, toInsert);
|
|
|
|
if (newContent === content) return Promise.resolve();
|
|
|
|
await fs.writeFile(filePath, newContent);
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.copyDir = async function(src, dest, options) {
|
|
|
|
const os = require('os');
|
|
|
|
|
|
|
|
options = Object.assign({}, {
|
|
|
|
excluded: [],
|
2020-03-10 01:24:57 +02:00
|
|
|
delete: true,
|
2020-02-21 00:59:18 +02:00
|
|
|
}, options);
|
|
|
|
|
|
|
|
src = utils.toSystemSlashes(src);
|
|
|
|
dest = utils.toSystemSlashes(dest);
|
|
|
|
|
|
|
|
await fs.mkdirp(dest);
|
|
|
|
|
|
|
|
if (utils.isWindows()) {
|
|
|
|
let excludedFlag = '';
|
|
|
|
let tempFile = null;
|
|
|
|
if (options.excluded.length) {
|
|
|
|
tempFile = `${os.tmpdir()}\\xcopy_excluded_${Date.now()}.txt`;
|
|
|
|
await fs.writeFile(tempFile, options.excluded.join('\n'));
|
|
|
|
excludedFlag = `/EXCLUDE:${tempFile}`;
|
|
|
|
}
|
|
|
|
|
2020-03-10 01:24:57 +02:00
|
|
|
// TODO: add support for delete flag
|
|
|
|
|
2020-03-23 02:47:25 +02:00
|
|
|
await utils.execCommand(`xcopy /C /I /H /R /Y /S ${excludedFlag} "${src}" "${dest}"`);
|
2020-02-21 00:59:18 +02:00
|
|
|
|
|
|
|
if (tempFile) await fs.remove(tempFile);
|
|
|
|
} else {
|
|
|
|
let excludedFlag = '';
|
|
|
|
if (options.excluded.length) {
|
2020-05-21 10:14:33 +02:00
|
|
|
excludedFlag = options.excluded.map(f => {
|
2020-02-21 00:59:18 +02:00
|
|
|
return `--exclude "${f}"`;
|
|
|
|
}).join(' ');
|
|
|
|
}
|
|
|
|
|
2020-03-10 01:24:57 +02:00
|
|
|
let deleteFlag = '';
|
|
|
|
if (options.delete) deleteFlag = '--delete';
|
|
|
|
|
|
|
|
await utils.execCommand(`rsync -a ${deleteFlag} ${excludedFlag} "${src}/" "${dest}/"`);
|
2020-02-21 00:59:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.copyFile = async function(src, dest) {
|
|
|
|
await fs.copy(src, dest);
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.rootDir = function() {
|
|
|
|
return utils.dirname(utils.dirname(__dirname));
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.registerGulpTasks = function(gulp, tasks) {
|
|
|
|
for (const taskName in tasks) {
|
|
|
|
gulp.task(taskName, tasks[taskName].fn);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = utils;
|