Skip to content

Commit

Permalink
Collect @foo/bar modules from node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetten committed Nov 1, 2017
1 parent 06803be commit 79c8825
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions node/internal/parse_yarn_lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ function main() {
//
const entries = Object.keys(yarn.object).map(key => makeYarnEntry(key, yarn.object[key]));

// For all top-level folders in the node_modules directory that
// For all top-level or @-scoped folders in the node_modules directory that
// contain a package.json file...
const getModulesIn = p => fs.readdirSync(p)
.filter(f =>
fs.statSync(path.join(p, f)).isDirectory() &&
fs.existsSync(path.join(p, f, 'package.json')) &&
fs.statSync(path.join(p, f, 'package.json')).isFile());
const getModulesIn = p => fs.readdirSync(p).filter(f => isPackage(p, undefined, f));
const findScopes = p => fs.readdirSync(p).filter(f => f.startsWith("@") && fs.statSync(path.join(p, f)).isDirectory());
const getModulesInScope = (p, s) => fs.readdirSync(path.join(p, s)).filter(f => isPackage(p, s, f));

// ... parse ithem
const modules = getModulesIn('node_modules').map(dir => parseNodeModulePackageJson(dir));
// ... parse them
let topLevelModuleDirs = getModulesIn('node_modules');
let scopeModuleDirs = findScopes('node_modules').map(scope => getModulesInScope('node_modules', scope).map(m => scope+'/'+m)).reduce((a, b) => a.concat(b), []);
let moduleDirs = topLevelModuleDirs.concat(scopeModuleDirs);
const modules = moduleDirs.map(dir => parseNodeModulePackageJson(dir));

// Iterate all the modules and merge the information from yarn into
// the module
Expand Down Expand Up @@ -68,6 +69,12 @@ function main() {
print("# EOF");
}

function isPackage(p, s, f) {
let dir = s ? path.join(p, s, f) : path.join(p, f);
return fs.statSync(dir).isDirectory() &&
fs.existsSync(path.join(dir, 'package.json')) &&
fs.statSync(path.join(dir, 'package.json')).isFile()
}

/**
* Given a list of yarn entries and a target module, find an exact
Expand Down

0 comments on commit 79c8825

Please sign in to comment.