@@ -4,17 +4,21 @@ const chalk = require('chalk');
44const spawn = require ( 'cross-spawn' ) ;
55const isWindows = require ( 'is-windows' ) ;
66
7- function getScripts ( ) {
8- const scripts = fs . readdirSync ( getATLauncherScriptsBasePath ( 'scripts' ) ) ;
9-
10- // remove the file extension and turn all '-' to ':' so 'lint-js' is run by using 'lint:js'
11- return scripts . map ( ( script ) => script . replace ( '.js' , '' ) . replace ( '-' , ':' ) ) ;
12- }
13-
14- function getProjectBasePath ( ) {
15- return path . resolve ( process . cwd ( ) ) ;
7+ /**
8+ * This gets a path to the project (where atlauncher-scripts was called from).
9+ *
10+ * @param {string } [pathString=''] the path to get, otherwise the root
11+ * @returns {string }
12+ */
13+ function getProjectPath ( pathString = '' ) {
14+ return path . resolve ( process . cwd ( ) , pathString ) ;
1615}
1716
17+ /**
18+ * This reads in the projects package.json (if it exists) and returns it as an object.
19+ *
20+ * @returns {Object }
21+ */
1822function getProjectPackageJson ( ) {
1923 const packageJsonPath = getProjectPath ( 'package.json' ) ;
2024
@@ -25,6 +29,15 @@ function getProjectPackageJson() {
2529 return JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
2630}
2731
32+ /**
33+ * This gets the specified value from the projects package.json config option if it exists.
34+ *
35+ * If it doesn't exist, the optional defaultValue will be returned.
36+ *
37+ * @param {string } name
38+ * @param {any } [defaultValue=null]
39+ * @returns {any }
40+ */
2841function getConfigFromPackageJson ( name , defaultValue = null ) {
2942 const packageJson = getProjectPackageJson ( ) ;
3043
@@ -43,39 +56,68 @@ function getConfigFromPackageJson(name, defaultValue = null) {
4356 return packageJson . config . atlauncher [ name ] ;
4457}
4558
59+ /**
60+ * This gets the base path for the atlauncher-scripts directory,
61+ *
62+ * @param {string } [pathString='']
63+ * @returns {string }
64+ */
4665function getATLauncherScriptsBasePath ( pathString = '' ) {
4766 return path . resolve ( __dirname , '../' , pathString ) ;
4867}
4968
50- function getSourceCodePath ( ) {
51- return getProjectPath ( 'src' ) ;
52- }
69+ /**
70+ * This gets the list of scripts available to be executed by atlauncher-scripts.
71+ *
72+ * @returns {string[] }
73+ */
74+ function getScripts ( ) {
75+ const scripts = fs . readdirSync ( getATLauncherScriptsBasePath ( 'scripts' ) ) ;
5376
54- function getProjectPath ( pathString = '/' ) {
55- return path . resolve ( getProjectBasePath ( ) , pathString ) ;
77+ // remove the file extension and turn all '-' to ':' so 'lint-js' is run by using 'lint:js'
78+ return scripts . map ( ( script ) => script . replace ( '.js' , '' ) . replace ( '-' , ':' ) ) ;
5679}
5780
81+ /**
82+ * This gets a path from the node_modules folder.
83+ *
84+ * It first checks the projects node_modules folder, and then if that doesn't exist will check the atlauncher-scripts
85+ * node_modules folder. Finally if that doesn't exist it will assume it's being run from lerna and use lerna's hoisted
86+ * node_modules folder if it exists, else null.
87+ *
88+ * @param {string } filename
89+ * @returns {string|null }
90+ */
5891function getNodeModulesPath ( filename ) {
59- const projectBasePath = getProjectBasePath ( ) ;
60-
61- const projectRootFile = `${ projectBasePath } /node_modules/${ filename } ` ;
92+ const projectRootFile = getProjectPath ( `node_modules/${ filename } ` ) ;
6293
6394 if ( fs . existsSync ( projectRootFile ) ) {
6495 return projectRootFile ;
6596 }
6697
67- const atlauncherScriptsBasePath = getATLauncherScriptsBasePath ( ) ;
68- const atlauncherScriptsFile = `${ atlauncherScriptsBasePath } /node_modules/${ filename } ` ;
98+ const atlauncherScriptsFile = getATLauncherScriptsBasePath ( `node_modules/${ filename } ` ) ;
6999
70100 if ( fs . existsSync ( atlauncherScriptsFile ) ) {
71101 return atlauncherScriptsFile ;
72102 }
73103
74- const lernaRootBasePath = path . resolve ( atlauncherScriptsBasePath , ' ../../../' ) ;
104+ const lernaRootBasePath = getATLauncherScriptsBasePath ( ` ../../../node_modules/ ${ filename } ` ) ;
75105
76- return `${ lernaRootBasePath } /node_modules/${ filename } ` ;
106+ if ( fs . existsSync ( lernaRootBasePath ) ) {
107+ return lernaRootBasePath ;
108+ }
109+
110+ return null ;
77111}
78112
113+ /**
114+ * This will get the path to a file in the .bin directory of node_modules.
115+ *
116+ * If on Windows it will return the path to the `.cmd` version if it exists.
117+ *
118+ * @param {string } packageName
119+ * @returns {string }
120+ */
79121function getNodeModulesBinPath ( packageName ) {
80122 if ( isWindows ( ) ) {
81123 const cmdBin = getNodeModulesPath ( `.bin/${ packageName } .cmd` ) ;
@@ -88,24 +130,31 @@ function getNodeModulesBinPath(packageName) {
88130 return getNodeModulesPath ( `.bin/${ packageName } ` ) ;
89131}
90132
91- function getRootFile ( filename ) {
92- const projectBasePath = getProjectBasePath ( ) ;
93-
94- const projectRootFile = `${ projectBasePath } /${ filename } ` ;
133+ /**
134+ * This will get the path to a config file, either in the projects base directory, or provided as a default by
135+ * atlauncher-scripts.
136+ *
137+ * @param {string } filename
138+ * @returns {string|null }
139+ */
140+ function getConfigFile ( filename ) {
141+ const projectRootFile = getProjectPath ( filename ) ;
95142
96143 if ( fs . existsSync ( projectRootFile ) ) {
97144 return projectRootFile ;
98145 }
99146
100- const atlauncherScriptsBasePath = getATLauncherScriptsBasePath ( ) ;
147+ const defaultRootFile = getATLauncherScriptsBasePath ( `config/ ${ filename } ` ) ;
101148
102- const defaultRootFile = `${ atlauncherScriptsBasePath } /config/${ filename } ` ;
149+ if ( fs . existsSync ( defaultRootFile ) ) {
150+ return defaultRootFile ;
151+ }
103152
104- return defaultRootFile ;
153+ return null ;
105154}
106155
107156function getRootGlob ( type = 'js' ) {
108- let sourceCodePath = getProjectBasePath ( ) ;
157+ let sourceCodePath = getProjectPath ( ) ;
109158
110159 if ( sourceCodePath . substr ( - 1 ) === '/' ) {
111160 sourceCodePath = sourceCodePath . substr ( 0 , sourceCodePath . length - 1 ) ;
@@ -119,7 +168,7 @@ function getRootGlob(type = 'js') {
119168}
120169
121170function getSourceCodeGlob ( type = 'js' ) {
122- let sourceCodePath = getSourceCodePath ( ) ;
171+ let sourceCodePath = getProjectPath ( 'src' ) ;
123172
124173 if ( sourceCodePath . substr ( - 1 ) === '/' ) {
125174 sourceCodePath = sourceCodePath . substr ( 0 , sourceCodePath . length - 1 ) ;
@@ -134,7 +183,7 @@ function getSourceCodeGlob(type = 'js') {
134183 return `${ sourceCodePath } /**/*` ;
135184}
136185
137- function spawnSyncProcess ( command = 'node' , processes = [ ] , workingDirectory = getProjectBasePath ( ) ) {
186+ function spawnSyncProcess ( command = 'node' , processes = [ ] , workingDirectory = getProjectPath ( ) ) {
138187 if ( typeof processes !== 'object' ) {
139188 return ;
140189 }
@@ -181,15 +230,15 @@ function spawnSyncProcess(command = 'node', processes = [], workingDirectory = g
181230 } ) ;
182231}
183232
233+ // eslint-disable-next-line immutable/no-mutation
184234module . exports = {
185235 getScripts,
186- getRootFile,
236+ getRootGlob,
237+ getConfigFile,
187238 getProjectPath,
188239 spawnSyncProcess,
189240 getSourceCodeGlob,
190- getSourceCodePath,
191241 getNodeModulesPath,
192- getProjectBasePath,
193242 getNodeModulesBinPath,
194243 getConfigFromPackageJson,
195244} ;
0 commit comments