UNPKG

@omnia/tooling

Version:

Provide basic stuffs extensible for omnia extension.

143 lines (142 loc) 4.83 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var path = require('path'), fs = require('fs') //, _extend = require('deep-extend') , _timer = {}; function rewriteFile(args) { args.path = args.path || process.cwd(); var fullPath = path.join(args.path, args.file); args.spliceWithinLine = args.spliceWithinLine || false; args.haystack = fs.readFileSync(fullPath, 'utf8'); var body = rewrite(args); fs.writeFileSync(fullPath, body, 'utf8'); } exports.rewriteFile = rewriteFile; function escapeRegExp(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } function rewrite(args) { // check if splicable is already in the body text var re = new RegExp(args.splicable.map(function (line) { return '\s*' + escapeRegExp(line); }).join('\r\n')); if (re.test(args.haystack)) { return args.haystack; } var lines = args.haystack.split('\r\n'); var otherwiseLineIndex = 0; lines.forEach(function (line, i) { if (line.indexOf(args.needle) !== -1) { otherwiseLineIndex = i; } }); if ((otherwiseLineIndex > 0) && (args.spliceWithinLine)) { var line = lines[otherwiseLineIndex]; var indexToSpliceAt = line.indexOf(args.needle); lines[otherwiseLineIndex] = line.substr(0, indexToSpliceAt) + args.splicable[0] + line.substr(indexToSpliceAt); return lines.join('\r\n'); } var spaces = 0; while (lines[otherwiseLineIndex].charAt(spaces) === ' ') { spaces += 1; } var spaceStr = ''; while ((spaces -= 1) >= 0) { spaceStr += ' '; } lines.splice(otherwiseLineIndex, 0, args.splicable.map(function (line) { return spaceStr + line; }).join('\r\n')); return lines.join('\r\n'); } function getCurrentDateTime() { var d = new Date(); return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); } exports.getCurrentDateTime = getCurrentDateTime; function flatArrayFiles(listFiles) { var flatFiles = ""; for (var i = 0; i < listFiles.length; i++) { flatFiles += listFiles[i].replace(/^.*[\\\/]/, '') + ", "; } if (flatFiles.length > 2 && flatFiles.substr(flatFiles.length - 2, 2) === ", ") { flatFiles = flatFiles.substr(0, flatFiles.length - 2); } return flatFiles; } function timeWatch(timerId, callBack, msWait) { if (_timer[timerId]) { clearTimeout(_timer[timerId]); } _timer[timerId] = setTimeout(callBack, msWait); } exports.timeWatch = timeWatch; // Helper functions var ROOT = process.cwd(); //var ROOT = path.resolve(__dirname, '..'); function hasProcessFlag(flag) { return process.argv.join('').indexOf(flag) > -1; } function isWebpackDevServer() { return process.argv[1] && !!(/webpack-dev-server/.exec(process.argv[1])); } function root(args) { args = Array.prototype.slice.call(arguments, 0); return path.join.apply(path, [ROOT].concat(args)); } exports.root = root; /** * Generate relatively-safe guid */ function generateGuid() { var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; } exports.generateGuid = generateGuid; ; function isValidGuid(value) { if (!isNullOrEmpty(value)) { var result = value.match(/^[{]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[}]?$/); return result !== null && result.length > 0; } return false; } exports.isValidGuid = isValidGuid; function isNull(obj) { if (obj === 0 || obj === false) return false; return (!obj || typeof obj === "undefined" || obj === null); } exports.isNull = isNull; function isNullOrEmpty(obj) { return isNull(obj) || obj === ""; } exports.isNullOrEmpty = isNullOrEmpty; function getDirectories(path) { return fs.readdirSync(path).filter(function (file) { return fs.statSync(path + '/' + file).isDirectory(); }); } exports.getDirectories = getDirectories; function getGuidValue(value) { if (value) { var matchs = value.match(/([A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12})/i); if (matchs) return matchs[0]; } return null; } exports.getGuidValue = getGuidValue; function getFileName(source) { if (source) { var extension = path.extname(source); return path.basename(source, extension); } return null; } exports.getFileName = getFileName;