@erda-ui/cli
Version:
Command line interface for rapid Erda UI development
98 lines (97 loc) • 5.37 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const os_1 = require("os");
const log_1 = require("./util/log");
const gen_version_1 = __importDefault(require("./util/gen-version"));
const git_commands_1 = require("./util/git-commands");
const externalModules = ['admin'];
const subModules = ['market', 'uc'];
const allModules = ['shell', 'core', 'admin', 'market', 'uc'];
const checkBuildStatus = () => __awaiter(void 0, void 0, void 0, function* () {
const cwd = process.cwd();
const enterprisePath = path_1.default.resolve(cwd, 'erda-ui-enterprise');
const requireBuildMap = allModules.reduce((acc, module) => {
acc[`skip-${module}-build`] = 'true';
return acc;
}, {});
for (const moduleName of allModules) {
const staticPath = path_1.default.join(cwd, `public/static/${moduleName}`);
(0, log_1.logInfo)('-----------------------------------------');
(0, log_1.logInfo)('Looking for build cache at:', staticPath);
(0, log_1.logInfo)(`Start compare diff for ${moduleName}`);
const gitCwd = externalModules.includes(moduleName) ? enterprisePath : cwd;
const headSha = yield (0, git_commands_1.getGitShortSha)(gitCwd);
const externalHeadSha = yield (0, git_commands_1.getGitShortSha)(enterprisePath);
const skipKey = `skip-${moduleName}-build`;
try {
const gitVersionPath = path_1.default.resolve(staticPath, '.git-version');
let prevGitSha = null;
let skipBuild = true;
let nextSha = headSha;
if (fs_1.default.existsSync(gitVersionPath)) {
prevGitSha = fs_1.default.readFileSync(gitVersionPath, { encoding: 'utf8' }).replace('\n', '');
(0, log_1.logInfo)('Found previous git sha:', prevGitSha);
const [prevSha, prevExternalSha] = prevGitSha.split('/');
const diff = yield (0, git_commands_1.getGitDiffFiles)(prevSha, headSha, gitCwd);
const fileRegex = new RegExp(`(^${subModules.includes(moduleName) ? `modules/${moduleName}` : moduleName}/.*)`, 'gm');
let match = fileRegex.exec(diff);
if (match) {
(0, log_1.logInfo)('File diff:');
skipBuild = false;
}
while (match) {
(0, log_1.logInfo)(match[1]);
match = fileRegex.exec(diff);
}
if (moduleName === 'shell') {
(0, log_1.logWarn)('In case shell has part of code maintained under enterprise, need to check enterprise code base');
nextSha = `${headSha}/${externalHeadSha}`;
const externalDiff = yield (0, git_commands_1.getGitDiffFiles)(prevExternalSha, externalHeadSha, enterprisePath);
if (new RegExp('^msp/', 'gm').test(externalDiff)) {
(0, log_1.logWarn)(`Diff detected in enterprise for shell between ${prevExternalSha} and ${externalHeadSha}`, externalDiff);
skipBuild = false;
}
else {
(0, log_1.logInfo)('No diff found for enterprise part of shell');
}
}
}
if (skipBuild && prevGitSha) {
(0, log_1.logInfo)(`no change detected between ${prevGitSha} and ${headSha}. will skip build ${moduleName}`);
}
else {
(0, log_1.logWarn)(`Diff detected between ${prevGitSha} and ${headSha}, will start new built for ${moduleName}`);
requireBuildMap[skipKey] = nextSha;
}
}
catch (error) {
(0, log_1.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_1.default.appendFile(metaPath, `${moduleKey}=${requireBuildMap[moduleKey]}${os_1.EOL}`, (err) => {
if (err) {
(0, log_1.logError)('err', err);
}
(0, log_1.logSuccess)(`emit ${moduleKey} value ${requireBuildMap[moduleKey]} to METAFILE`);
});
});
(0, gen_version_1.default)();
});
exports.default = checkBuildStatus;