-
Notifications
You must be signed in to change notification settings - Fork 51
/
copy.js
69 lines (57 loc) · 2.13 KB
/
copy.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
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
const fs = require('fs');
const path = require('path');
const { mkdirpSync } = require('mkdirp');
const ignore = require('ignore');
const { fdir } = require("fdir");
const async = require('async');
console.time("runtime")
const sourceDir = '.';
const targetDir = path.join('./data/environments', process.argv[2] || 'prod');
function readAndSplitGitIgnore(filePath) {
const gitIgnoreContent = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf-8') : '';
return gitIgnoreContent.split(/\r?\n/).filter((rule) => rule.trim() !== '');
}
function copyFiles(source, target, ignoreList) {
mkdirpSync(target);
const files = fs.readdirSync(source);
files.forEach((file) => {
const sourcePath = path.join(source, file);
const targetPath = path.join(target, file);
const stat = fs.statSync(sourcePath);
const relativePath = path.relative(sourceDir, sourcePath);
const ig = ignore().add(ignoreList);
if (!ig.ignores(relativePath) && file != '.git') {
if (stat.isDirectory()) {
const dirGitIgnorePath = path.join(sourcePath, '.gitignore');
const dirGitIgnoreRules = readAndSplitGitIgnore(dirGitIgnorePath);
copyFiles(sourcePath, targetPath, [...ignoreList, ...dirGitIgnoreRules]);
} else {
fs.copyFileSync(sourcePath, targetPath);
}
}
});
}
async function copyFile(source) {
const stat = await fs.promises.lstat(source)
if(stat.isDirectory()) {
await fs.promises.mkdir(path.join(targetDir, source))
} else {
await fs.copyFile(source, path.join(targetDir, source));
}
}
const gitIgnorePath = path.join(sourceDir, '.gitignore');
const gitIgnoreRules = readAndSplitGitIgnore(gitIgnorePath);
copyFiles(sourceDir, targetDir, gitIgnoreRules);
console.log('Base files copied, copying node_modules....')
const api = new fdir().withDirs().withBasePath().crawl("node_modules");
const files = api.sync();
const maxParallelCopies = 16;
const queue = async.queue(async function (task, completed) {
await copyFile(task);
completed();
}, maxParallelCopies);
files.forEach(file => queue.push(file));
queue.drain(function() {
console.log('Done!');
console.timeEnd("runtime")
});