Skip to content
This repository was archived by the owner on May 16, 2020. It is now read-only.

Commit b9022e1

Browse files
feat(atlauncher-scripts): if project uses lerna, run lint on packagesthis allows for atlauncher-scripts to be used in the root of a lerna project and it's commands (such as linting JS files) to be run on all lerna projects defined in lerna.json
1 parent 08cec65 commit b9022e1

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

packages/atlauncher-scripts/scripts/lint-js.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ if (args.length) {
1717
}
1818

1919
if (watch) {
20-
const processArguments = [
21-
utils.getProjectPath('src/**/*.js'),
22-
'--initial',
23-
'-c npm run lint:js',
24-
];
20+
const processArguments = [utils.getProjectPath('src/**/*.js'), '--initial', '-c npm run lint:js'];
2521

2622
utils.spawnSyncProcess(utils.getNodeModulesBinPath('chokidar'), processArguments);
2723

@@ -33,8 +29,11 @@ const processArguments = [
3329
utils.getConfigFile('.eslintrc'),
3430
'--ignore-path',
3531
utils.getConfigFile('.eslintignore'),
32+
'--ignore-pattern',
33+
'**/node_modules/**',
34+
'--ignore-pattern',
35+
'**/coverage/**',
3636
debug && '--debug',
37-
utils.getProjectPath('src/**/*.js'),
38-
];
37+
].concat(utils.getProjectPaths('**/*.js'));
3938

40-
utils.spawnSyncProcess(utils.getNodeModulesBinPath('eslint'), processArguments);
39+
utils.spawnSyncProcess(utils.getNodeModulesBinPath('eslint'), processArgumentsS);

packages/atlauncher-scripts/utils/index.js

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,76 @@ const spawn = require('cross-spawn');
55
const 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

Comments
 (0)