Skip to content

Commit

Permalink
feat: add more directories excluded and treated as source in the JetB…
Browse files Browse the repository at this point in the history
…rains script. (#1961)
  • Loading branch information
skinny85 authored Mar 13, 2019
1 parent 11ed691 commit a1df717
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions scripts/jetbrains-remove-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,59 @@ 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());
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 getAllNodeModulesPaths(dir) {
function getAllDirsWithNodeModules(dir) {
let nodeModulesPaths = [];
getAllChildDirectories(dir).forEach(name => {
if (isNodeModulesDirectory(name)) {
console.log('Excluding ' + name);
nodeModulesPaths.push(name);
nodeModulesPaths.push(dir);
} else {
const subNodeModulesPaths = getAllNodeModulesPaths(name);
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 = getAllNodeModulesPaths('.');
const nodeModulesPaths = getAllDirsWithNodeModules('.');

// 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" />');
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('<excludeFolder url="file://$MODULE_DIR$/temp" />');
exclusions.push('<excludeFolder url="file://$MODULE_DIR$/tmp" />');
exclusions.push('<excludePattern pattern=".jsii" />');

exclusionsString = exclusions.join(os.EOL);
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';
Expand All @@ -48,9 +69,10 @@ if (fs.existsSync('.idea/' + fileName)) {
// 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>`);
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...');
console.log(`${os.EOL}Writing to file: ${fileName} ...`);

// "Delete" the file first to avoid strange concurrent use errors.
fs.unlinkSync(fileName);
Expand Down

0 comments on commit a1df717

Please sign in to comment.