forked from novuhq/novu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint-affected-array.mjs
175 lines (141 loc) · 4.56 KB
/
print-affected-array.mjs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { getPackageFolders } from './get-packages-folder.mjs';
import spawn from 'cross-spawn';
import { fileURLToPath } from 'url';
import path from 'path';
import * as fs from 'fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const processArguments = process.argv.slice(2);
const ALL_FLAG = '--all';
const TASK_NAME = processArguments[0];
const BASE_BRANCH_NAME = processArguments[1];
const GROUP = processArguments[2];
const ROOT_PATH = path.resolve(__dirname, '..');
const ENCODING_TYPE = 'utf8';
const NEW_LINE_CHAR = '\n';
class CliLogs {
constructor() {
this._logs = [];
this.log = this.log.bind(this);
}
log(log) {
const cleanLog = log.trim();
if (cleanLog.length) {
this._logs.push(cleanLog);
}
}
get logs() {
return this._logs;
}
get joinedLogs() {
return this.logs.join(NEW_LINE_CHAR);
}
}
function pnpmRun(...args) {
const logData = new CliLogs();
let pnpmProcess;
return new Promise((resolve, reject) => {
const processOptions = {
cwd: ROOT_PATH,
env: process.env,
};
pnpmProcess = spawn('pnpm', args, processOptions);
pnpmProcess.stdin.setEncoding(ENCODING_TYPE);
pnpmProcess.stdout.setEncoding(ENCODING_TYPE);
pnpmProcess.stderr.setEncoding(ENCODING_TYPE);
pnpmProcess.stdout.on('data', logData.log);
pnpmProcess.stderr.on('data', logData.log);
pnpmProcess.on('close', (code) => {
if (code !== 0) {
reject(logData.joinedLogs);
} else {
resolve(logData.joinedLogs);
}
});
});
}
function commaSeparatedListToArray(str) {
return str
.trim()
.split(',')
.map((element) => element.trim())
.filter((element) => !!element.length);
}
function getAffectedCommandResult(str) {
const outputLines = str.trim().split(/\r?\n/);
if (outputLines.length > 2) {
return outputLines.slice(-1)[0];
}
return '';
}
async function affectedProjectsContainingTask(taskName, baseBranch) {
const cachePath = taskName + baseBranch.replace('/', '').replace('/', '') + '-contain-task-cache.json';
const isCacheExists = fs.existsSync(cachePath);
if (isCacheExists) {
const cache = fs.readFileSync(cachePath, 'utf8');
return JSON.parse(cache);
}
// pnpm nx print-affected --target=[task] --base [base branch] --select=tasks.target.project
const result = commaSeparatedListToArray(
getAffectedCommandResult(
await pnpmRun('nx', 'print-affected', '--target', taskName, '--base', baseBranch, '--select=tasks.target.project')
)
);
fs.writeFileSync(cachePath, JSON.stringify(result));
return result;
}
async function allProjectsContainingTask(taskName) {
const cachePath = taskName + '-all-contain-task-cache.json';
const isCacheExists = fs.existsSync(cachePath);
if (isCacheExists) {
const cache = fs.readFileSync(cachePath, 'utf8');
return JSON.parse(cache);
}
// pnpm nx print-affected --target=[task] --files package.json --select=tasks.target.project
const result = commaSeparatedListToArray(
getAffectedCommandResult(
await pnpmRun(
'nx',
'print-affected',
'--target',
taskName,
'--files',
'package.json',
'--select=tasks.target.project'
)
)
);
fs.writeFileSync(cachePath, JSON.stringify(result));
return result;
}
async function printAffectedProjectsContainingTask() {
const { providers, packages , libs } = await getPackageFolders(['providers', 'packages', 'libs']);
let projects =
BASE_BRANCH_NAME === ALL_FLAG
? await allProjectsContainingTask(TASK_NAME)
: await affectedProjectsContainingTask(TASK_NAME, BASE_BRANCH_NAME);
const foundProviders = projects.filter((project) => providers.includes(project));
if (foundProviders.length) {
projects = projects.filter((project) => !providers.includes(project));
}
const foundPackages = projects.filter((project) => packages.includes(project));
if (foundPackages.length) {
projects = projects.filter((project) => !packages.includes(project));
}
const foundLibs = projects.filter((project) => libs.includes(project));
if (foundLibs.length) {
projects = projects.filter((project) => !libs.includes(project));
}
if (GROUP === 'providers') {
console.log(JSON.stringify(foundProviders));
} else if (GROUP === 'packages') {
console.log(JSON.stringify(foundPackages));
} else if (GROUP === 'libs') {
console.log(JSON.stringify(foundLibs));
} else {
console.log(JSON.stringify(projects));
}
}
printAffectedProjectsContainingTask().catch((error) => {
console.error(error);
process.exit(1);
});