-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggestion: Add initial support for Lerna monorepos (#11)
* Add initial support for lerna monorepos * comment * add support for yarn + refactor * Add warning for invalid json file + comment about globstar support
- Loading branch information
1 parent
347dfc7
commit 4e36234
Showing
6 changed files
with
138 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
logs | ||
*.log | ||
npm-debug.log* | ||
.idea | ||
|
||
# Runtime data | ||
pids | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const path = require('path'); | ||
const loadJsonFile = require('load-json-file'); | ||
const glob = util.promisify(require('glob')); | ||
const { showWarning } = require('./utils'); | ||
|
||
const exists = util.promisify(fs.exists); | ||
|
||
const PACKAGE_JSON_FILE = 'package.json'; | ||
const LERNA_CONFIG_FILE = 'lerna.json'; | ||
const DOUBLE_STAR = '**'; // globstar | ||
|
||
const flat = arrays => [].concat.apply([], arrays); | ||
|
||
const distinct = array => [ ...new Set(array) ]; | ||
|
||
const findPatternMatches = async (root, pattern) => { | ||
// patterns with double star e.g. '/src/**/' are not supported at the moment, because they are too general and may match nested node_modules | ||
if (pattern.includes(DOUBLE_STAR)) return []; | ||
|
||
const matches = await glob(path.join(pattern, PACKAGE_JSON_FILE), { | ||
cwd: root | ||
}); | ||
|
||
return matches.map(match => path.join(match, '..')); | ||
}; | ||
|
||
const getLernaPackagesConfig = async root => { | ||
const lernaConfigFile = path.join(root, LERNA_CONFIG_FILE); | ||
if (!(await exists(lernaConfigFile))) { | ||
return []; | ||
} | ||
|
||
const config = await loadJsonFile(lernaConfigFile).catch(() => | ||
showWarning(`Ignoring invalid ${LERNA_CONFIG_FILE} file at: ${lernaConfigFile}`) | ||
); | ||
return config && Array.isArray(config.packages) ? config.packages : []; | ||
}; | ||
|
||
const getYarnWorkspacesConfig = async root => { | ||
const packageJsonFile = path.join(root, PACKAGE_JSON_FILE); | ||
if (!(await exists(packageJsonFile))) { | ||
return []; | ||
} | ||
|
||
const config = await loadJsonFile(packageJsonFile).catch(() => | ||
showWarning(`Ignoring invalid ${PACKAGE_JSON_FILE} file at: ${packageJsonFile}`) | ||
); | ||
return config && Array.isArray(config.workspaces) ? config.workspaces : []; | ||
}; | ||
|
||
const findChildPackages = async root => { | ||
const patterns = distinct([ | ||
...(await getLernaPackagesConfig(root)), | ||
...(await getYarnWorkspacesConfig(root)) | ||
]); | ||
|
||
const matchesArr = await Promise.all( | ||
patterns.map(pattern => findPatternMatches(root, pattern)) | ||
); | ||
|
||
return flat(matchesArr); | ||
}; | ||
|
||
module.exports = { findChildPackages }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const vscode = require('vscode'); | ||
|
||
const formatMsg = message => `Search node_modules: ${message}`; | ||
const showError = message => vscode.window.showErrorMessage(formatMsg(message)); | ||
const showWarning = message => vscode.window.showWarningMessage(formatMsg(message)); | ||
|
||
module.exports = { showError, showWarning }; |