forked from k14v/nlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli
More file actions
executable file
·82 lines (71 loc) · 2.25 KB
/
Copy pathcli
File metadata and controls
executable file
·82 lines (71 loc) · 2.25 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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const argDir = process.argv[2];
const cwdDir = process.cwd();
const scanDir = path.join(cwdDir, argDir);
const cleanFlag = process.argv.includes('--clean');
const waterfall = (fn, arr, end, ...args) => {
if (arr.length === 0) {
if (end) end();
return;
}
const p = arr[0];
fn(p, ...args.concat([(err) => {
if (err) {
console.error(err.message);
process.exit(1);
} else waterfall(fn, arr.slice(1), end, ...args);
}]));
};
const wmkdirp = (paths, cb) => waterfall(mkdirp, paths, cb);
const wmkfile = (files, {cwdDir, argDir}, cb) => waterfall((file, next) => {
// Workaround to ensure relative works always as expected
const relPath = path.relative(path.dirname(path.join(cwdDir, file)), path.join(argDir, file)).replace(/\\/gi, '/');
fs.writeFile(path.join(cwdDir, file), `module.exports = require('./${relPath}');\n`, (err) => {
next(err);
});
}, files, cb);
glob('{*.js,**/*.js}', {
// Restrict scan directory
cwd: scanDir,
// Retrieve relative paths
absolute: false,
// Ignore files that it being used in local only by its sibling
ignore: '{*.*.js,**/*.*.js}',
}, (err, files) => {
// Get all root files
const rfiles = files
.filter(file => path.dirname(file) === '.');
// Get all unique directories and ignore root files
const rdirs = files
.map(path.dirname.bind(null))
.filter((file, index, self) => self.indexOf(file) === index && file !== '.');
// Remove files when clean flag is detected
if (cleanFlag) {
const rimrafFiles = rdirs.concat(rfiles);
const rimrafPattern = path.join(cwdDir, `{${rimrafFiles.join(',')}}`);
return rimraf(rimrafPattern, (err) => {
if (err) {
console.error(err);
return process.exit(1);
}
process.stderr.write(`${rimrafFiles.length} Files cleaned!\n`);
return 0;
});
}
// Create directory links
wmkdirp(rdirs, () => {
process.stderr.write(`${rdirs.length} Directories linked!\n`);
});
// Create file links
wmkfile(files, {
argDir,
cwdDir,
}, () => {
process.stderr.write(`${files.length} Files linked!\n`);
});
});