-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
61 lines (48 loc) · 1.55 KB
/
Copy pathsetup.js
File metadata and controls
61 lines (48 loc) · 1.55 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
const fs = require('fs');
const os = require('os');
const path = require('path');
const SEP = os.platform() === 'win32' ? '\\\\' : '/';
const EXCLUDED_DIRS = [
'node_modules',
'.git',
'.idea',
'dist'
];
const handlePackageJson = dir => {
const inputPackageJsonPath = `${dir}/_package.json`;
const outputPackageJsonPath = `${dir}/package.json`;
console.log(`Processing ${inputPackageJsonPath} to ${outputPackageJsonPath}...`);
const content = fs.readFileSync(inputPackageJsonPath, 'UTF-8');
const newContent = content.replace(/\.\.\//g, `..${SEP}`)
.replace(/\/\.bin\//g, `${SEP}.bin${SEP}`);
fs.writeFileSync(outputPackageJsonPath, newContent);
};
const handlerForDirectory = dir => file => {
const qualifiedName = path.join(dir, file);
fs.stat(qualifiedName, (error, stat) => {
if (error) {
console.error(`Error stating file '${file}' in '${dir}' (${qualifiedName})`, error);
throw error;
}
if (stat.isFile() && file === '_package.json') {
handlePackageJson(dir);
} else if (stat.isDirectory()) {
if (!EXCLUDED_DIRS.includes(file)) {
processDir(qualifiedName);
}
}
});
};
// Read all entries in directory and invoke handler
const processDir = dir => {
fs.readdir(dir, (err, files) => {
if (err) {
console.error(err);
throw err;
}
const handler = handlerForDirectory(dir);
files.forEach(handler);
})
};
// Start
processDir('./code');