@@ -5,15 +5,76 @@ const spawn = require('cross-spawn');
55const isWindows = require ( 'is-windows' ) ;
66
77/**
8- * This gets a path to the project (where atlauncher-scripts was called from).
8+ * This will read the lerna.json file (if available).
9+ *
10+ * @returns {string[] }
11+ */
12+ function getLernaPackagePaths ( ) {
13+ const lernaJsonPath = path . resolve ( process . cwd ( ) , 'lerna.json' ) ;
14+
15+ if ( ! fs . existsSync ( lernaJsonPath ) ) {
16+ return [ ] ;
17+ }
18+
19+ const { packages = [ ] } = JSON . parse ( fs . readFileSync ( lernaJsonPath , 'utf8' ) ) ;
20+
21+ return packages ;
22+ }
23+
24+ /**
25+ * This ensures that the given path always ends with a / (or not);
26+ *
27+ * @param {string } path
28+ * @param {boolean } [ensureLastSlash=true]
29+ * @returns string
30+ */
31+ function ensureLastSlash ( path , ensureLastSlash = true ) {
32+ if ( ensureLastSlash && path . substr ( path , - 1 ) !== '/' ) {
33+ return `${ path } /` ;
34+ }
35+
36+ if ( ! ensureLastSlash && path . substr ( path , - 1 ) === '/' ) {
37+ return path . substr ( path , 0 , - 1 ) ;
38+ }
39+
40+ return path ;
41+ }
42+
43+ /**
44+ * This gets a path (or paths if lerna) to the project (where atlauncher-scripts was called from).
945 *
1046 * @param {string } [pathString=''] the path to get, otherwise the root
11- * @returns {string }
47+ * @param {boolean } [lernaCheck=false] if we should check for lerna
48+ * @returns {string|string[] }
1249 */
13- function getProjectPath ( pathString = '' ) {
50+ function getProjectPath ( pathString = '' , lernaCheck = false ) {
51+ if ( lernaCheck ) {
52+ const lernaPackages = getLernaPackagePaths ( ) ;
53+
54+ if ( lernaPackages . length ) {
55+ return lernaPackages . map ( ( package ) => path . resolve ( process . cwd ( ) , ensureLastSlash ( package ) , pathString ) ) ;
56+ }
57+ }
58+
1459 return path . resolve ( process . cwd ( ) , pathString ) ;
1560}
1661
62+ /**
63+ * This gets the paths (if lerna) to the project (where atlauncher-scripts was called from).
64+ *
65+ * @param {string } [pathString=''] the path to get, otherwise the root
66+ * @returns {string[] }
67+ */
68+ function getProjectPaths ( pathString = '' ) {
69+ const paths = getProjectPath ( pathString , true ) ;
70+
71+ if ( ! Array . isArray ( paths ) ) {
72+ return [ paths ] ;
73+ }
74+
75+ return paths ;
76+ }
77+
1778/**
1879 * This reads in the projects package.json (if it exists) and returns it as an object.
1980 *
@@ -186,17 +247,17 @@ function spawnSyncProcess(command = 'node', processes = [], workingDirectory = g
186247 console . log (
187248 chalk . white . bgRed (
188249 'Commit Linting failed because the process exited too early. ' +
189- 'This probably means the system ran out of memory or someone called ' +
190- '`kill -9` on the process.'
191- )
250+ 'This probably means the system ran out of memory or someone called ' +
251+ '`kill -9` on the process.' ,
252+ ) ,
192253 ) ;
193254 } else if ( result . signal === 'SIGTERM' ) {
194255 console . log (
195256 chalk . white . bgRed (
196257 'Commit Linting failed because the process exited too early. ' +
197- 'Someone might have called `kill` or `killall`, or the system could ' +
198- 'be shutting down.'
199- )
258+ 'Someone might have called `kill` or `killall`, or the system could ' +
259+ 'be shutting down.' ,
260+ ) ,
200261 ) ;
201262 }
202263
@@ -212,6 +273,7 @@ module.exports = {
212273 getScripts,
213274 getConfigFile,
214275 getProjectPath,
276+ getProjectPaths,
215277 spawnSyncProcess,
216278 getNodeModulesPath,
217279 getNodeModulesBinPath,
0 commit comments