forked from erda-project/erda-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-build-status.ts
133 lines (120 loc) · 5.04 KB
/
check-build-status.ts
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
/*
* Copyright (c) 2021 Terminus, Inc.
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-disable no-await-in-loop */
import { promisify } from 'util';
import child_process from 'child_process';
import fs from 'fs';
import path from 'path';
import { EOL } from 'os';
import { logInfo, logSuccess, logWarn, logError } from './util/log';
const asyncExec = promisify(child_process.exec);
const externalModules = ['fdp', 'admin'];
const subModules = ['market', 'uc'];
const allModules = ['shell', 'core', 'admin', 'fdp', 'market', 'uc'];
const checkBuildStatus = async () => {
const cwd = process.cwd();
const enterprisePath = path.resolve(cwd, 'erda-ui-enterprise');
const requireBuildMap = allModules.reduce<{ [k: string]: string }>((acc, module) => {
acc[`skip-${module}-build`] = 'true';
return acc;
}, {});
for (const moduleName of allModules) {
const staticPath = path.join(cwd, `public/static/${moduleName}`);
logInfo('-----------------------------------------');
logInfo('Looking for build cache at:', staticPath);
logInfo(`Start compare diff for ${moduleName}`);
const gitCwd = externalModules.includes(moduleName) ? enterprisePath : cwd;
let { stdout: headSha } = await asyncExec('git rev-parse --short HEAD', { cwd: gitCwd });
headSha = headSha.replace(/\n/, '');
let { stdout: externalHeadSha } = await asyncExec('git rev-parse --short HEAD', {
cwd: enterprisePath,
});
externalHeadSha = externalHeadSha.replace(/\n/, '');
const skipKey = `skip-${moduleName}-build`;
try {
const gitVersionPath = path.resolve(staticPath, '.git-version');
let prevGitSha = null;
let skipBuild = true;
let nextSha = headSha;
if (fs.existsSync(gitVersionPath)) {
prevGitSha = fs.readFileSync(gitVersionPath, { encoding: 'utf8' }).replace('\n', '');
logInfo('Found previous git sha:', prevGitSha);
const [prevSha, prevExternalSha] = prevGitSha.split('/');
const { stdout: diff } = await asyncExec(`git diff --name-only ${prevSha} ${headSha}`, { cwd: gitCwd });
const fileRegex = new RegExp(
`(^${subModules.includes(moduleName) ? `modules/${moduleName}` : moduleName}/.*)`,
'gm',
);
let match = fileRegex.exec(diff);
if (match) {
logInfo('File diff:');
skipBuild = false;
}
while (match) {
logInfo(match[1]);
match = fileRegex.exec(diff);
}
if (moduleName === 'shell') {
logWarn('In case shell has part of code maintained under enterprise, need to check enterprise code base');
nextSha = `${headSha}/${externalHeadSha}`;
const { stdout: externalDiff } = await asyncExec(
`git diff --name-only ${prevExternalSha} ${externalHeadSha}`,
{ cwd: enterprisePath },
);
if (new RegExp('^cmp/', 'gm').test(externalDiff) || new RegExp('^msp/', 'gm').test(externalDiff)) {
logWarn(
`Diff detected in enterprise for shell between ${prevExternalSha} and ${externalHeadSha}`,
externalDiff,
);
skipBuild = false;
} else {
logInfo('No diff found for enterprise part of shell');
}
}
}
if (skipBuild) {
logInfo(`no change detected between ${prevGitSha} and ${headSha}. will skip build ${moduleName}`);
} else {
logWarn(`Diff detected between ${prevGitSha} and ${headSha}, will start new built for ${moduleName}`);
requireBuildMap[skipKey] = nextSha;
}
} catch (error) {
logError(`Compare diff failed for ${moduleName}`, error.toString());
requireBuildMap[skipKey] = moduleName === 'shell' ? `${headSha}/${externalHeadSha}` : headSha;
}
}
const metaPath = process.env.METAFILE;
Object.keys(requireBuildMap).forEach((moduleKey) => {
fs.appendFile(metaPath!, `${moduleKey}=${requireBuildMap[moduleKey]}${EOL}`, (err) => {
if (err) {
logError('err', err);
}
logSuccess(`emit ${moduleKey} value ${requireBuildMap[moduleKey]} to METAFILE`);
});
});
const data = { version: Date.parse(new Date().toString()) };
// generate version.json
const publicPath = path.resolve(process.cwd(), 'public');
fs.mkdir(publicPath, '0777', () => {
fs.writeFile(`${publicPath}/version.json`, JSON.stringify(data), (err) => {
if (err) {
logError('version.json generated fail', err);
} else {
logSuccess('version.json generated ok.');
}
});
});
};
export default checkBuildStatus;