Skip to content

Commit

Permalink
✨feat: cli checking animation for default presets
Browse files Browse the repository at this point in the history
  • Loading branch information
BoyYangzai committed Jun 28, 2023
1 parent 4e6316a commit 104d289
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 52 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/generateDiy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default function generateDiy({
let output: string[] = [];
async function run() {
const commands = diyCommands;

// for cancel single default preset animation
process.env.IS_DIY_PRESET = "true";

for (const command of commands) {
const ptyProcess = await spawn(
"npx",
Expand Down
148 changes: 96 additions & 52 deletions packages/core/src/generatePreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,61 +71,105 @@ export default function generatePreset({
type: ApplyPluginsType.add,
});

//----------------- checking ------------------
const checkedResult = (
await api.applyPlugins({
key: `addDoctor${transformString(command)}Check`,
type: ApplyPluginsType.add,
args: meta,
})
).filter(Boolean);
const mergedRes = sort(mergeObjectsByProp(checkedResult.filter(Boolean)));
mergedRes.forEach((rule, index) => {
console.log(
chalk.greenBright(
`${chalk.greenBright(
`${index++ < 10 ? "0" + index++ : index++}. ${toUpperUnderscore(
rule.label
)}`
)}\n`
)
);
rule.descriptions.forEach((i) => {
switch (i?.level) {
case DoctorLevel.SUCCESS:
console.log(
`${chalk.bgGreenBright(" DoctorLevel SUCCESS 🎉🎉 ")}`
);
console.log(`${chalk.greenBright(" WHY ")}${i.suggestion} \n`);
break;
break;
case DoctorLevel.WARN:
console.log(`${chalk.bgYellowBright("DoctorLevel WARN ")}`);
console.log(
`${chalk.greenBright(" SUGGESTION ")}${i.suggestion} \n`
);
break;
case DoctorLevel.ERROR:
console.log(`${chalk.bgRedBright(" DoctorLevel Error ")}`);
console.log(
`${chalk.greenBright(" SUGGESTION ")}${i.suggestion} \n`
);
break;
default:
break;
}
if (process.env.IS_DIY_PRESET !== "true") {
const { spinner: load } = await import("@astrojs/cli-kit");
//----------------- checking ------------------
await load({
start: "Doctor Rule Checking",
end: "Check end",
while: () => {
return new Promise(async (resolve) => {
CheckingAndEnd({
api,
command,
meta,
resolve,
animationFn: () =>
new Promise((res) => {
setTimeout(() => {
res(void 0);
}, 1000);
}),
delayFn: () =>
new Promise((res) => {
setTimeout(() => {
res(void 0);
}, 100);
}),
});
});
},
});
});
} else {
CheckingAndEnd({ api, command, meta });
}
},
});
}

async function CheckingAndEnd({
api,
command,
meta,
resolve = (res: undefined) => {},
animationFn = async () => {},
delayFn = async () => {},
}) {
//----------------- checking ------------------
const checkedResult = (
await api.applyPlugins({
key: `addDoctor${transformString(command)}Check`,
type: ApplyPluginsType.add,
args: meta,
})
).filter(Boolean);

//----------------- check end ------------------
if (mergedRes.some((i) => i.doctorLevel === DoctorLevel.ERROR)) {
process.exit(1);
// for cli animation
await animationFn();
resolve(void 0);

// for delay output end 100 ms
await delayFn();

const mergedRes = sort(mergeObjectsByProp(checkedResult.filter(Boolean)));
mergedRes.forEach((rule, index) => {
console.log(
chalk.greenBright(
`${chalk.greenBright(
`${index++ < 10 ? "0" + index++ : index++}. ${toUpperUnderscore(
rule.label
)}`
)}\n`
)
);
rule.descriptions.forEach((i) => {
switch (i?.level) {
case DoctorLevel.SUCCESS:
console.log(`${chalk.bgGreenBright(" DoctorLevel SUCCESS 🎉🎉 ")}`);
console.log(`${chalk.greenBright(" WHY ")}${i.suggestion} \n`);
break;
break;
case DoctorLevel.WARN:
console.log(`${chalk.bgYellowBright("DoctorLevel WARN ")}`);
console.log(`${chalk.greenBright(" SUGGESTION ")}${i.suggestion} \n`);
break;
case DoctorLevel.ERROR:
console.log(`${chalk.bgRedBright(" DoctorLevel Error ")}`);
console.log(`${chalk.greenBright(" SUGGESTION ")}${i.suggestion} \n`);
break;
default:
break;
}
});
});

await api.applyPlugins({
key: `addDoctor${transformString(command)}CheckEnd`,
type: ApplyPluginsType.add,
});
},
//----------------- check end ------------------
if (mergedRes.some((i) => i.doctorLevel === DoctorLevel.ERROR)) {
process.exit(1);
}

await api.applyPlugins({
key: `addDoctor${transformString(command)}CheckEnd`,
type: ApplyPluginsType.add,
});
}

0 comments on commit 104d289

Please sign in to comment.