-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
43 lines (38 loc) · 1.2 KB
/
main.js
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
const glob = require('glob');
const path = require('path');
const mkdirp = require('mkdirp');
const fs = require('fs');
const { getUniqueDirectories, handleError, printLn } = require('./utils');
function main({
scanPattern,
ignorePattern,
directoryToScan,
outputDirectory,
} = {}){
glob(scanPattern, {
// Restrict scan directory
cwd: directoryToScan,
// Retrieve relative paths
absolute: false,
// Ignore files by pattern
ignore: ignorePattern,
}, (err, files) => {
if(err) return handleError(err);
const directories = getUniqueDirectories(files);
// Create all the directories before creating all the files
// so they have a target directory
Promise.all(directories.map((directory) => mkdirp(directory)))
.then(() => {
printLn(`${directories.length} Directories linked!`);
}).then(() => {
files.forEach((file) => {
//To ensure it works in Windows
const relativePath = file.replace(/\\/gi, '/');
fs.writeFile(path.join(outputDirectory, file), `module.exports = require('./${relativePath}');\n`, handleError)
});
}).then(() => {
printLn(`${files.length} Files linked!`);
})
});
}
module.exports = main;