-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(scripts): Add Jetbrains node_modules exclusion script (#1507)
Jetbrains IDEs will index the files in a project to provide intelligent suggestions to users. However, this project uses lerna and has cyclical symlinks inside node_module directories. The IDE will index until it runs out of memory following these links. Given the size of this project, doing the exclusions by hand is infeasable every time one does a git clean. This script will modify the .iml file directly to add the appropriate exclusions. * The JetBrains IDEs exclusion list assumed your iml file was named aws-cdk. It now uses the root directory's name instead. * IntelliJ stores the iml file at root. The script now accounts for that difference. * For uncommon setups, the script can take an optional path arguement to opt-out of conventional file discovery, and be explicit.
- Loading branch information
1 parent
948b5df
commit 4443d5c
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const os = require('os'); | ||
|
||
const getAllChildDirectories = (dir) => fs.readdirSync(dir).map(name => path.join(dir, name.toString())).filter(name => fs.lstatSync(name).isDirectory()); | ||
const isNodeModulesDirectory = (name) => name.toString().endsWith('node_modules'); | ||
|
||
function getAllNodeModulesPaths(dir) { | ||
let nodeModulesPaths = []; | ||
getAllChildDirectories(dir).forEach(name => { | ||
if (isNodeModulesDirectory(name)) { | ||
console.log('Excluding ' + name); | ||
nodeModulesPaths.push(name); | ||
} else { | ||
const subNodeModulesPaths = getAllNodeModulesPaths(name); | ||
nodeModulesPaths = nodeModulesPaths.concat(subNodeModulesPaths); | ||
} | ||
}); | ||
return nodeModulesPaths; | ||
} | ||
|
||
// 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 = getAllNodeModulesPaths('.'); | ||
|
||
// Hardcoded exclusions for this project (in addition to node_modules) | ||
const exclusions = nodeModulesPaths.map(path => `<excludeFolder url="file://$MODULE_DIR$/${path}" />`); | ||
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/.tmp" />'); | ||
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/docs" />'); | ||
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/temp" />'); | ||
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/tmp" />'); | ||
|
||
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...'); | ||
|
||
// "Delete" the file first to avoid strange concurrent use errors. | ||
fs.unlinkSync(fileName); | ||
|
||
fs.writeFileSync(fileName, toWrite); | ||
|
||
console.log('Done!'); |