-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-folder-layout.js
More file actions
464 lines (426 loc) · 15.5 KB
/
Copy pathbuild-folder-layout.js
File metadata and controls
464 lines (426 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const child = require("child_process");
let minimatch;
try {
minimatch = require('minimatch');
} catch (ex) {
if (typeof(minimatch) === 'undefined') {
try {
child.execSync("npm install -g minimatch");
minimatch = require('minimatch');
} catch(ex1) {
try {
child.execSync("npm install --save-dev minimatch");
minimatch = require('minimatch');
} catch(ex2) { }
}
}
}
const globals = {};
function sutSplit(string, delimiters) {
delimiters = (Array.isArray(delimiters) ? delimiters : Array.from(arguments).slice(1)).reverse();
const splits = string.split(delimiters.pop());
for (let delimiter of delimiters) {
for (let index = splits.length - 1; index >= 0; index--) {
const split = splits[index].split(delimiter);
if (split.length > 1) {
splits[index] = split[0];
for (let splitIndex = 1; splitIndex < split.length; splitIndex++) {
splits.splice(index + splitIndex, 0, currentSplit);
}
}
}
}
return splits;
}
// Return a list of files of the specified fileTypes in the provided dir,
// with the file path relative to the given dir
// dir: path of the directory you want to search the files for
// fileTypes: array of file types you are search files, ex: ['.txt', '.jpg']
function getFilesFromDir(fileTypes, dir = ".") {
let filesToReturn = [];
function walkDir(currentPath) {
const files = fs.readdirSync(currentPath);
for (var i in files) {
var curFile = path.join(currentPath, files[i]);
if (fs.statSync(curFile).isFile()) {
if (minimatch(path.basename(curFile), fileTypes)) {
filesToReturn.push(curFile);
}
} else if (fs.statSync(curFile).isDirectory()) {
walkDir(curFile);
}
}
};
walkDir(dir);
return filesToReturn;
}
//************************************************************************************** */
//* Get Functions */
//************************************************************************************** */
globals.getFiles = function(path) {
if (typeof(path) !== 'string' || fs.existsSync(path)) {
return path; // If getFiles is passed an array, assume files have already been found.
}
const fileTypes = path.basename(path);
const dir = path.dirname(path.normalize(path));
let foundPaths;
try {
foundPaths = getFilesFromDir(fileTypes, dir);
} catch (ex) {
console.error("While finding files, ", path, " cound not be found. ", ex);
foundPaths = [];
}
if (foundPaths.length > 1) {
return foundPaths;
} else if (typeof(foundPaths[0]) === 'undefined') {
return path; // Path exists, but no files could be found... Assume path is ok as-is.
}
return foundPaths[0];
}
globals.match = function(pattern, value) {
const regex = new RegExp(pattern);
return regex.test(value);
}
function getExecPath(exec) {
let result;
try {
result = child.execSync("which " + exec, {stdio: [process.stdin, process.stdout]}).toString().trim();
} catch(ex) {
try {
result = child.execSync("where " + exec, {stdio: [process.stdin, process.stdout]}).toString().trim();
} catch(ex2) {
return;
}
}
if (result.toLowerCase().indexOf("command not found") !== -1 || result.toLowerCase().indexOf("could not find files") !== -1) {
return;
}
return result;
}
globals.isExec = function(value) {
value = value.split(" ")[0];
if (process.platform === "win32") {
switch(Path.GetExtension(value).toLowerCase()) {
case "exe": case "bat": case "cmd": case "vbs": case "ps1": {
return true;
}
}
}
try {
if (fs.lstatSync(value).isDirectory()) {
return false
}
} catch(ex) {}
try {
// Check if linux has execution rights
fs.accessSync(value, fs.constants.X_OK);
return true;
} catch(ex) {
}
// Exists on the system path
return typeof(getExecPath(value)) !== 'undefined';
}
globals.isProbablyFile = function(value) {
return value.startsWith(".") && !value.endsWith(".");
}
globals.isProbablyDirectory = function(value) {
return value.endsWith(".");
}
//************************************************************************************** */
//* Action Functions */
//************************************************************************************** */
globals.fail = function() {
return false;
}
globals.success = function() {
return true;
}
globals.runCommand = function(itemCommand, ignoreFailure = false) {
try {
child.execSync(itemCommand);
return true;
} catch(ex) {
}
return ignoreFailure;
}
// Path should always end with /. or a /filename.extension.
// /path/ refers to all files within a path. It is not a path.
// /path refers only to a file. This file could also be a directory.
//
// Returns true to continue processing only if a new path is created.
globals.mkdir = function(dirPath) {
const relativePath = path.relative("./.", path.normalize(path.resolve(path.dirname(dirPath))));
while (relativePath.endsWith(".")) {
relativePath = relativePath.substring(0, relativePath.length - 1);
}
if (relativePath === "") {
return true;
}
try {
fs.mkdirSync(relativePath);
} catch(e) {
const splitPath = sutSplit(relativePath, "/", "\\");
let currentPath = "";
for (let folderIndex = 0; folderIndex < splitPath.length; folderIndex++) {
const folder = splitPath[folderIndex];
currentPath = currentPath + folder + "/";
try {
fs.mkdirSync(currentPath);
} catch (ex) {
return false;
}
}
}
return true;
}
// Only returns true to continue processing if a new file is written.
globals.touch = function(path) {
globals.mkdir(path);
const now = Date.now();
try {
fs.utimesSync(path, now, now); // Update last modified date
} catch (ex) {
fs.closeSync(fs.openSync(path, 'w'));
return true; // New blank file was created...return success (and possibly continue processing)
}
return false;
}
globals.copy = function(source, destination, copied = []) {
source = globals.getFiles(source);
if (typeof(source) !== 'string' || typeof(destination) !== 'string') {
for (let destKey in (typeof(destionation) === 'object' ? destionation : [destination])) {
const dest = destionation[destKey];
for (let srcKey in (typeof(source) === 'object' ? source : [source])) {
const src = source[srcKey];
globals.copyFiles(src, dest);
}
}
return (Array.isArray(copied) && copied.length > 0 ? copied : undefined);
}
globals.mkdir(destination);
if (source === destination) {
try {
fs.closeSync(fs.openSync(destination, 'w')); // Update last modified date
} catch (ex) {}
return (Array.isArray(copied) && copied.length > 0 ? copied : undefined);
}
try {
fs.copyFile(source, destionation);
copied.push(source);
} catch(ex) {
console.error("Unable to copy file ", source, " -> ", destionation, " ", err)
}
return (Array.isArray(copied) && copied.length > 0 ? copied : undefined);
}
globals.remove = function(path) {
path = globals.getFiles(path);
if (typeof(path) !== 'string') {
let removed = false;
for (let pathKey in path) {
const pathItem = path[pathKey];
removed = globals.remove(pathItem) || removed;
}
return removed;
}
if (typeof(file) === 'string') {
try {
fs.unlinkSync(file);
return true;
} catch (ex) {
console.error("Unable to delete file ", file, " ", ex);
}
}
return false;
}
globals.move = function(source, destination) {
let moved = false;
for (let copiedFile of globals.copy(source, destination)) {
if (globals.remove(copiedFile)) {
movied = true;
}
}
return moved;
}
//************************************************************************************** */
//* Reflection - Dynamic variable substitution and function execution */
//************************************************************************************** */
function getVariable(name, scope, globalScope) {
if (typeof(scope) !== 'undefined' && typeof(scope[name]) !== 'undefined') {
return scope[name];
}
if (typeof(globalScope) !== 'undefined' && typeof(globalScope[name]) !== 'undefined') {
return globalScope[name];
}
return undefined;
}
function trySubstitutingVariable(variable, scope = this, globalScope = globals) {
if (variable.indexOf("$") === -1) {
return variable;
}
const argParts = variable.split(/(?=[^A-Za-z0-9]+)/g);
for (let partIndex = 0; partIndex < argParts.length; partIndex++) {
if (argParts[partIndex] === "$" && partIndex < argParts.length - 2 && argParts[partIndex + 1].startsWith("{") && argParts[partIndex + 2] === "}") { // Assume ${variable}
argParts.splice(partIndex, 1); // remove $
argParts[partIndex] = argParts[partIndex].substring(1); // remove {
argParts.splice(partIndex + 1, 1) // remove }
} else if (argParts[partIndex].startsWith("$") && argParts[partIndex].length > 1) {
argParts[partIndex] = argParts[partIndex].substring(1);
} else {
continue;
}
argParts[partIndex] = getVariable(argParts[partIndex], scope, globalScope);
}
return argParts.join("");
}
function runFunction(callbackFunc, scope) {
const func = callbackFunc[0];
const args = Array.from(callbackFunc.length === 2 && Array.isArray(callbackFunc[1]) ? callbackFunc[1] : Array.from(callbackFunc).slice(1));
for (let argIndex in args) {
args[argIndex] = trySubstitutingVariable(args[argIndex], scope);
}
return func.apply(scope, args);
}
function func(callback, args = []) {
if (arguments.length === 2 && Array.isArray(arguments[1])) {
return [callback, args];
}
return [arguments[0], Array.from(arguments).slice(1)];
}
//************************************************************************************** */
//* Lookup Commands */
//************************************************************************************** */
const defaultCommands = [
{ "getFiles": func(globals.getFiles, "$value") },
{ "mkdir": func(globals.mkdir, "$value") },
{ "touch": func(globals.touch, "$value") },
{ "copy": func(globals.copy, "$source", "$value" )},
{ "mv": func(globals.move, "$source", "$value" )},
{ "rm": func(globals.remove, "$value") },
{ "exec": func(globals.runCommand, "$value") },
{ "link": func(globals.link, "$value") }, // TODO: Use Atom file-system-watcher to emulate link, when links are not available
{ "logerror": func(globals.log, "$value") }
];
const defaultPatternAliases = [
{ "getFiles": func(globals.match, "\\*\\.(\\*|[a-zA-Z0-9]+)$", "$value") }, // TODO: Get this working later...matches wild cards
{ "exec": func(globals.isExec, "$value") },
{ "touch": func(globals.isProbablyFile, "$value") },
{ "mkdir": func(globals.isProbablyDirectory, "$value") },
{ "logerror": func(globals.success) }
];
//************************************************************************************** */
//* Process Folder Layout */
//************************************************************************************** */
function getCommand(layoutCommand) {
const commandId = layoutCommand.split(" ")[0];
if (defaultCommands.some(function(item) { return typeof(item[commandId]) !== 'undefined' } )) {
return commandId;
}
return;
}
function getAlias(layoutCommand) {
let result = false;
this.value = layoutCommand;
for (let aliasKey in defaultPatternAliases) {
const aliasMatch = defaultPatternAliases[aliasKey];
for (let alias in aliasMatch) {
if (runFunction(aliasMatch[alias], this)) {
return alias;
}
}
}
if (!result) {
defaultPatternAliases.logerror;
}
return;
}
function getCommandFunction(commandName) {
return defaultCommands.find(function(item) {
return typeof(item[commandName]) !== 'undefined';
})[commandName];
}
function processItem(layoutItem) {
const foundCommand = getCommand(layoutItem);
const foundAlias = (typeof(foundCommand) === 'undefined' ? getAlias(layoutItem) : undefined);
const command = getCommandFunction(foundCommand || foundAlias);
this.value = layoutItem;
const processedResult = runFunction(command, this);
console.log("Process item:", layoutItem, (processedResult ? "was successful" : "has failed"), "for", typeof(foundAlias) === 'undefined' ? "command: " + foundCommand : "found alias: " + foundAlias);
return processedResult;
}
try {
globals.asyncForeach = async function(fn, argsOfArgs = [[]], scope = this) {
const promises = [];
for (let args of argsOfArgs) {
if (!array.IsArray(args)) {
args = [args];
}
promises.push(async function() { fn.apply(scope, args); }());
}
return await Promise.all(promises);
}
} catch(ex) {}
// Similar to startProcessingFolderLayout. But if anything fails in a group, break out early.
function processFolderLayout(layoutItem) {
if (typeof(layoutItem) === 'string') {
return processItem(layoutItem);
} else if (Array.isArray(layoutItem)) {
for (let item of layoutItem) {
if (!processItem(item)) {
return false;
}
}
return true;
} else if (typeof(layoutItem) === 'object') {
for (let commandKey in layoutItem) {
if (commandKey === "folderLayout" || commandKey === "folderStructure" || commandKey === "layout" || commandKey === "all") {
processFolderLayout(layoutItem[commandKey]);
} else if (commandKey === "parallel") {
try {
globals.asyncForeach(processItem, layoutItem[commandKey]);
} catch(ex) {
processFolderLayout(layoutItem[commandKey]);
}
} else if (commandKey === "group" || commandKey === "chain") {
return processFolderLayout(layoutItem[commandKey]);
} else if (!processFolderLayout(commandKey + " " + layoutItem[commandKey])) {
return false;
}
}
return true;
}
console.error("Not sure how to execute ", layoutItem, ", as it is not a string, array, or object.");
return false;
}
// For the first layer, don't fail out. Continue processing.
function startProcessingFolderLayout(layoutItem) {
let result = false;
if (typeof(layoutItem) === 'string') {
result = processFolderLayout(layoutItem);
} else if (Array.isArray(layoutItem)) {
for (let item of layoutItem) {
result = processFolderLayout(item) || result;
}
} else if (typeof(layoutItem) === 'object') {
for (let commandKey in layoutItem) {
if (commandKey === "folderLayout" || commandKey === "folderStructure" || commandKey === "layout" || commandKey === "all") {
result = processFolderLayout(layoutItem[commandKey]) || result;
} else if (commandKey === "parallel") {
try {
globals.asyncForeach(processFolderLayout, layoutItem[commandKey]);
} catch(ex) {
processFolderLayout(layoutItem[commandKey]);
}
result = true;
} else {
result = processFolderLayout(commandKey + " " + layoutItem[commandKey]) || result;
}
}
}
return result;
}
const packagejson = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json")));
const folderLayout = packagejson.folderLayout || packagejson.folderStructure || packagejson.layout;
startProcessingFolderLayout(folderLayout);