forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jetbrains-remove-node-modules.js
82 lines (65 loc) · 3.18 KB
/
jetbrains-remove-node-modules.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
70
71
72
73
74
75
76
77
78
79
80
81
82
const fs = require('fs');
const path = require('path');
const os = require('os');
function isDirectory(path) {
return fs.lstatSync(path).isDirectory();
}
const getAllChildDirectories = (dir) => fs.readdirSync(dir).map(name => path.join(dir, name.toString())).filter(isDirectory);
const isNodeModulesDirectory = (name) => name.toString().endsWith('node_modules');
function getAllDirsWithNodeModules(dir) {
let nodeModulesPaths = [];
getAllChildDirectories(dir).forEach(name => {
if (isNodeModulesDirectory(name)) {
console.log('Excluding ' + name);
nodeModulesPaths.push(dir);
} else {
const subNodeModulesPaths = getAllDirsWithNodeModules(name);
nodeModulesPaths = nodeModulesPaths.concat(subNodeModulesPaths);
}
});
return nodeModulesPaths;
}
function isCdkPackageDirectory(path) {
return fs.existsSync(path + '/lib') && isDirectory(path + '/lib') &&
fs.existsSync(path + '/package.json');
}
function cdkPackageEntries(path) {
let ret = [`<excludeFolder url="file://$MODULE_DIR$/${path}/node_modules" />`];
if (isCdkPackageDirectory(path)) {
ret.push(`<excludeFolder url="file://$MODULE_DIR$/${path}/.nyc_output" />`);
ret.push(`<excludeFolder url="file://$MODULE_DIR$/${path}/coverage" />`);
ret.push(`<sourceFolder url="file://$MODULE_DIR$/${path}/lib" isTestSource="false" />`);
ret.push(`<sourceFolder url="file://$MODULE_DIR$/${path}/test" isTestSource="true" />`);
}
return ret.join(os.EOL);
}
// Should be run at the root directory
if (!fs.existsSync('lerna.json')) {
throw new Error('This script should be run from the root of the repo.');
}
const nodeModulesPaths = getAllDirsWithNodeModules('.');
// Hardcoded exclusions for this project (in addition to node_modules)
const exclusions = nodeModulesPaths.map(cdkPackageEntries);
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/.github" />');
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/.idea" />');
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/.tools" />');
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/docs" />');
exclusions.push('<excludePattern pattern=".jsii" />');
const exclusionsString = exclusions.join(os.EOL);
// Let filename be passed in as an override
let fileName = process.argv[2] || process.cwd().split('/').slice(-1).pop() + '.iml';
// Jetbrains IDEs store iml in .idea except for IntelliJ, which uses root.
if (fs.existsSync('.idea/' + fileName)) {
fileName = '.idea/' + fileName;
} else if (!fs.existsSync(fileName)) {
throw new Error('iml file not found in .idea or at root. Please pass in a path explicitly as the first argument.');
}
// Keep the contents. We are only updating exclusions.
const exclusionInfo = fs.readFileSync(fileName);
const toWrite = exclusionInfo.toString().replace(/<content url="file:\/\/\$MODULE_DIR\$">(?:\s.+)+\/content>/m,
`<content url="file://$MODULE_DIR$">${os.EOL}${exclusionsString}${os.EOL}</content>`);
console.log(`${os.EOL}Writing to file: ${fileName} ...`);
// "Delete" the file first to avoid strange concurrent use errors.
fs.unlinkSync(fileName);
fs.writeFileSync(fileName, toWrite);
console.log('Done!');