@lcap/nasl
Version:
NetEase Application Specific Language
166 lines • 7.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deployFail = exports.deploySuccess = exports.deployStart = exports.assertIgnoreBackend = exports.assertIdeInitialize = exports.assertBatchInstruct = void 0;
const rules_1 = require("./rules");
const nasl_utils_1 = require("@lcap/nasl-utils");
const types_1 = require("./types");
const storage_1 = require("./storage");
/** 修改状态 */
async function setInstructStateWithDeploy(state) {
const deployState = await (0, storage_1.getDeploymentState)();
// 未发布时
if (deployState === types_1.DeploymentState.NotDeployment) {
await (0, storage_1.setInstructState)(state);
}
// 发布时
else {
await (0, storage_1.setInstructStateInDeploy)(state);
}
}
/** Nasl 变更后判断是否可以忽略后端发布 */
async function assertBatchInstruct(operationList, context) {
const [isIgnoreBackendNow, deployState,] = await Promise.all([
(0, storage_1.getInstructState)(),
(0, storage_1.getDeploymentState)(),
]);
if (
// 本地调试时每次都检查
!nasl_utils_1.isLocalMode &&
// 当前没有在发布
deployState === types_1.DeploymentState.NotDeployment &&
// 当前还未发布过,必须要先发布一次
isIgnoreBackendNow === types_1.InstructState.Initial ||
// 当前已经包含了后端内容,必须要发布后端,后面的判断都可以省略
isIgnoreBackendNow === types_1.InstructState.HasBackendModify) {
if (nasl_utils_1.isDebugMode) {
console.dir('当前状态必须发布后端,跳过判断');
}
return;
}
const rules = rules_1.rules.filter((rule) => rule.afterInstruct);
const actions = operationList.reduce((ans, item) => ans.concat(item.actionItem.list ?? []), []);
const ruleContext = {
...context,
actions,
};
for (const action of actions ?? []) {
const ruleResult = await Promise.all(rules.map((rule) => rule.afterInstruct(action, ruleContext)));
if (nasl_utils_1.isDebugMode) {
console.dir(`变更规则判定结果\n${ruleResult.map((rule, i) => `${rules[i].name}: ${types_1.RuleResult[rule]};`).join('\n')}`);
}
if (
// 只要有一个判定为后端,则为后端
ruleResult.includes(types_1.RuleResult.Backend) ||
// 全部是未匹配,则为兜底,也设定为后端
ruleResult.every((item) => item === types_1.RuleResult.UnMatch)) {
await setInstructStateWithDeploy(types_1.InstructState.HasBackendModify);
if (nasl_utils_1.isDebugMode) {
console.dir('判定为后端修改');
}
return;
}
// 不满足上述两个条件的就只剩下,同时存在“未匹配”和“前端修改”,此时可以判定为前端修改
}
if (nasl_utils_1.isDebugMode) {
console.dir('判定为前端修改');
}
return setInstructStateWithDeploy(types_1.InstructState.PureFrontendModify);
}
exports.assertBatchInstruct = assertBatchInstruct;
/** IDE 初始化时运行钩子 */
async function assertIdeInitialize(context) {
await Promise.all(rules_1.rules
.filter((rule) => rule.afterInit)
.map((rule) => rule.afterInit(context)));
}
exports.assertIdeInitialize = assertIdeInitialize;
/** 发布前判断是否可以忽略后端发布 */
async function assertIgnoreBackend(context) {
const state = await (0, storage_1.getInstructState)();
// 非纯前端时,不能忽略后端发布
if (state !== types_1.InstructState.PureFrontendModify &&
state !== types_1.InstructState.DeploymentRightNow) {
return false;
}
// 运行发布前校验规则
const rules = rules_1.rules.filter((rule) => rule.beforeDeploy);
const ruleResult = await Promise.all(rules.map((rule) => rule.beforeDeploy(context)));
if (nasl_utils_1.isDebugMode) {
console.dir(`发布规则判定结果\n${ruleResult.map((rule, i) => `${rules[i].name}: ${types_1.RuleResult[rule]};`).join('\n')}`);
}
if (
// 只要有一个判定为后端,则为后端
ruleResult.includes(types_1.RuleResult.Backend) ||
// 全部是未匹配,则为兜底,也设定为后端
ruleResult.every((item) => item === types_1.RuleResult.UnMatch)) {
await setInstructStateWithDeploy(types_1.InstructState.HasBackendModify);
if (nasl_utils_1.isDebugMode) {
console.dir('判定必须发布后端');
}
return false;
}
return true;
}
exports.assertIgnoreBackend = assertIgnoreBackend;
/** 发布开始 */
async function deployStart(type) {
return (0, storage_1.setDeploymentState)(type === 'normal' ? types_1.DeploymentState.NormalDeploying : types_1.DeploymentState.PreviewDeploying);
}
exports.deployStart = deployStart;
/**
* 发布成功结束
*
* @description 此时一定发布成功
*/
async function deploySuccess(context) {
const [deployState, modifyState,] = await Promise.all([
(0, storage_1.getDeploymentState)(),
(0, storage_1.getInstructStateInDeploy)(),
]);
// 普通发布
if (deployState === types_1.DeploymentState.NormalDeploying) {
// 有值的时候设置为修改状态
if (modifyState === types_1.InstructState.HasBackendModify) {
await (0, storage_1.setInstructState)(types_1.InstructState.HasBackendModify);
}
else {
await (0, storage_1.setInstructState)(types_1.InstructState.DeploymentRightNow);
}
}
else if (deployState === types_1.DeploymentState.PreviewDeploying) {
// 有值的时候设置为修改状态,否则保持不变
if (modifyState === types_1.InstructState.HasBackendModify) {
await (0, storage_1.setInstructState)(types_1.InstructState.HasBackendModify);
}
}
await Promise.all([
// 清空修改时的发布状态
(0, storage_1.setInstructStateInDeploy)(),
// 清空发布状态
(0, storage_1.setDeploymentState)(types_1.DeploymentState.NotDeployment),
]);
// 发布成功时的钩子
await Promise.all(rules_1.rules
.filter((rule) => rule.afterSuccessDeploy)
.map((rule) => rule.afterSuccessDeploy(context)));
}
exports.deploySuccess = deploySuccess;
/**
* 发布失败时
*/
async function deployFail(context) {
await Promise.all([
// 发布失败时下次一定全量发布
(0, storage_1.setInstructState)(types_1.InstructState.HasBackendModify),
// 清空发布状态
(0, storage_1.setDeploymentState)(types_1.DeploymentState.NotDeployment),
// 清空修改时的发布状态
(0, storage_1.setInstructStateInDeploy)(),
]);
// 发布失败时的钩子
await Promise.all(rules_1.rules
.filter((rule) => rule.afterFailDeploy)
.map((rule) => rule.afterFailDeploy(context)));
}
exports.deployFail = deployFail;
//# sourceMappingURL=ruler.js.map