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

feat: add more directories excluded and treated as source in the JetBrains script #1961

Merged
merged 1 commit into from
Mar 13, 2019
Merged
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
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" />');
costleya marked this conversation as resolved.
Show resolved Hide resolved
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