UNPKG

6.52 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license Utils
4 * (c) 2017 Amin Paks <amin.pakseresht@hotmail.com>
5 * License: MIT
6 */
7var __assign = (this && this.__assign) || Object.assign || function(t) {
8 for (var s, i = 1, n = arguments.length; i < n; i++) {
9 s = arguments[i];
10 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11 t[p] = s[p];
12 }
13 return t;
14};
15Object.defineProperty(exports, "__esModule", { value: true });
16var fs = require("fs");
17var glob = require("glob");
18var path = require("path");
19var keys = Object.keys;
20function copyFromTo(_a) {
21 var pattern = _a.pattern, rootDir = _a.rootDir, toDir = _a.toDir, _b = _a.excludes, excludes = _b === void 0 ? [] : _b;
22 return new Promise(function (resolve, reject) {
23 glob(pattern, {
24 cwd: rootDir,
25 nodir: true,
26 ignore: ['node_modules/**/*'].concat(excludes),
27 }, function (err, files) {
28 // Rejects on error
29 if (err) {
30 reject(err);
31 }
32 var results = files.map(function (file) {
33 return new Promise(function (fileResolve, fileReject) {
34 var origin = path.join(rootDir, file);
35 var dest = path.join(toDir, file);
36 var rd = fs.createReadStream(origin);
37 rd.on('error', function (err_1) { return fileReject("Cannot read " + origin + ": " + err_1); });
38 rd.on('open', function () {
39 ensureMakeDir(path.dirname(dest));
40 var wr = fs.createWriteStream(dest);
41 wr.on('error', function (err_1) { return fileReject("Cannot write to " + dest + ": " + err_1); });
42 wr.on('close', function () { return fileResolve(file); });
43 // Finish the copy
44 rd.pipe(wr);
45 });
46 });
47 });
48 Promise.all(results)
49 .then(function (values) {
50 if (values.length === files.length) {
51 resolve(values);
52 }
53 else {
54 var notCopied = files.filter(function (file) { return !values.includes(file); });
55 reject('Not all files were copied.\n' + notCopied.join('\n'));
56 }
57 })
58 .catch(function (err_1) {
59 reject(err_1);
60 });
61 });
62 });
63}
64exports.copyFromTo = copyFromTo;
65function readFile(filePath, encoding) {
66 return fs.readFileSync(filePath, { encoding: encoding || 'utf8' });
67}
68exports.readFile = readFile;
69function writeFile(filePath, content, encoding) {
70 return fs.writeFileSync(filePath, content, { encoding: encoding || 'utf8' });
71}
72exports.writeFile = writeFile;
73function isDirectory(validPath) {
74 try {
75 return fs.lstatSync(validPath).isDirectory();
76 }
77 catch (err) {
78 return false;
79 }
80}
81exports.isDirectory = isDirectory;
82function isFile(validPath) {
83 try {
84 return fs.lstatSync(validPath).isFile();
85 }
86 catch (err) {
87 return false;
88 }
89}
90exports.isFile = isFile;
91function isString(value) {
92 return (typeof value === 'string');
93}
94exports.isString = isString;
95function isNil(value) {
96 return (value == undefined);
97}
98exports.isNil = isNil;
99function isEmpty(value) {
100 if (typeof value === 'string') {
101 return value.length === 0;
102 }
103 else if (value instanceof Array) {
104 return value.length === 0;
105 }
106 else if (typeof value === 'object') {
107 return keys(value).length === 0;
108 }
109 else {
110 return true;
111 }
112}
113exports.isEmpty = isEmpty;
114function isArray(variable) {
115 return (variable instanceof Array);
116}
117exports.isArray = isArray;
118function ensureMakeDir(validPath) {
119 if (!isDirectory(validPath)) {
120 var parentPath = path.dirname(validPath);
121 ensureMakeDir(parentPath);
122 fs.mkdirSync(validPath);
123 }
124}
125exports.ensureMakeDir = ensureMakeDir;
126function ensureRemoveDir(validPath) {
127 if (isDirectory(validPath)) {
128 var dirItems = fs.readdirSync(validPath, 'utf8');
129 for (var _i = 0, dirItems_1 = dirItems; _i < dirItems_1.length; _i++) {
130 var item = dirItems_1[_i];
131 var currentItem = path.resolve(validPath, item);
132 if (isDirectory(currentItem)) {
133 ensureRemoveDir(currentItem);
134 }
135 else if (isFile(currentItem)) {
136 fs.unlinkSync(currentItem);
137 }
138 }
139 fs.rmdirSync(validPath);
140 }
141}
142exports.ensureRemoveDir = ensureRemoveDir;
143function mergeLevelFromTo(from, to) {
144 var result = __assign({}, to);
145 var _loop_1 = function (key) {
146 if (isArray(from[key])) {
147 var fromArray_1 = from[key];
148 if (isArray(result[key])) {
149 var toArray = result[key];
150 result[key] = toArray
151 .filter(function (item) { return fromArray_1.indexOf(item) < 0; })
152 .concat(fromArray_1);
153 }
154 else {
155 result[key] = fromArray_1.slice();
156 }
157 }
158 else if (typeof from[key] === 'object') {
159 result[key] = mergeLevelFromTo(from[key], result[key]);
160 }
161 else {
162 result[key] = from[key];
163 }
164 };
165 for (var _i = 0, _a = keys(from); _i < _a.length; _i++) {
166 var key = _a[_i];
167 _loop_1(key);
168 }
169 return result;
170}
171exports.mergeLevelFromTo = mergeLevelFromTo;
172function uniqueArray(unique, second, predicate) {
173 if (predicate === void 0) { predicate = function (item, searchIn) { return !searchIn.includes(item); }; }
174 return unique.concat.apply(unique, second.filter(function (item) { return predicate(item, unique); }));
175}
176exports.uniqueArray = uniqueArray;
177function mergeInto(origin, dest) {
178 return mergeLevelFromTo(origin, dest);
179}
180exports.mergeInto = mergeInto;
181function removeComments(input) {
182 var removedMultilineComments = input.replace(/\/\*(.|[\r\n])*?\*\//g, '');
183 var removedSingleLineComments = removedMultilineComments.replace(/\/\/.*/gm, '');
184 return removedSingleLineComments;
185}
186exports.removeComments = removeComments;
187function isAngularLib(externals) {
188 return keys(externals).some(function (item) { return /^@angular/i.test(item); });
189}
190exports.isAngularLib = isAngularLib;
191function isOfType(_value, check) {
192 return check && check(_value);
193}
194exports.isOfType = isOfType;