Skip to content

Commit

Permalink
chore(scripts): Add Jetbrains node_modules exclusion script (#1507)
Browse files Browse the repository at this point in the history
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
costleya authored and AlexChesters committed Jan 14, 2019
1 parent 948b5df commit 4443d5c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ $ cd docs
$ BUILD_DOCS_DEV=1 ./build-docs.sh
```

### Tooling Assists
#### Jetbrains (WebStorm/IntelliJ)
This project uses lerna and utilizes symlinks inside nested node_modules directories. You may encounter an issue during
indexing where the IDE attempts to index these directories and keeps following links until the process runs out of
available memory and crashes. To fix this, you can run ```node ./scripts/jetbrains-remove-node-modules.js``` to exclude
these directories.

## Dependencies

### Adding Dependencies
Expand Down
60 changes: 60 additions & 0 deletions scripts/jetbrains-remove-node-modules.js
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!');

0 comments on commit 4443d5c

Please sign in to comment.