-
Notifications
You must be signed in to change notification settings - Fork 105
/
lerna.js
executable file
·56 lines (44 loc) · 1.08 KB
/
lerna.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const {execSync} = require('child_process');
const path = require('path');
const fs = require('fs');
const isLerna = (state) =>
fs.existsSync(path.join(state.root, 'lerna.json'));
const isDir = (root) => (name) => {
const filepath = path.join(root, name);
try {
const stats = fs.statSync(filepath);
return stats.isDirectory();
} catch (error) {
return false;
}
};
const getAllPackages = (state) => {
try {
const dir = path.join(state.root, 'packages');
return fs.readdirSync(dir).filter(isDir(dir));
} catch (error) {
return [];
}
};
const getChangedFiles = () =>
execSync('git diff --cached --name-only 2>/dev/null')
.toString()
.trim()
.split('\n');
const getChangedPackages = () => {
const unique = {};
const changedFiles = getChangedFiles();
const regex = /^packages\/([^/]+)\//;
for (const filename of changedFiles) {
const matches = filename.match(regex);
if (matches) {
unique[matches[1]] = 1;
}
}
return Object.keys(unique);
};
module.exports = {
getAllPackages,
getChangedPackages,
isLerna
};