Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(scripts): Add Jetbrains node_modules exclusion script #1507

Merged
merged 3 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!');