From a1df717306a2254e29bfab5e077a85f1da1dc89d Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Wed, 13 Mar 2019 13:44:22 -0700 Subject: [PATCH] feat: add more directories excluded and treated as source in the JetBrains script. (#1961) --- scripts/jetbrains-remove-node-modules.js | 46 +++++++++++++++++------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/scripts/jetbrains-remove-node-modules.js b/scripts/jetbrains-remove-node-modules.js index 875ebfbc3b231..8821037a5e70e 100644 --- a/scripts/jetbrains-remove-node-modules.js +++ b/scripts/jetbrains-remove-node-modules.js @@ -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 = [``]; + if (isCdkPackageDirectory(path)) { + ret.push(``); + ret.push(``); + ret.push(``); + ret.push(``); + } + 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 => ``); -exclusions.push(''); +const exclusions = nodeModulesPaths.map(cdkPackageEntries); +exclusions.push(''); +exclusions.push(''); +exclusions.push(''); exclusions.push(''); -exclusions.push(''); -exclusions.push(''); +exclusions.push(''); -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'; @@ -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(/(?:\s.+)+\/content>/m, `${os.EOL}${exclusionsString}${os.EOL}`); +const toWrite = exclusionInfo.toString().replace(/(?:\s.+)+\/content>/m, + `${os.EOL}${exclusionsString}${os.EOL}`); -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);