slidev-addon-sm
Version:
slidev-addon slidev sm
152 lines (126 loc) • 4.58 kB
JavaScript
import { $, cd, fs, path } from "zx";
import { v4 as uuidv4 } from 'uuid';
import { execSync, spawn } from 'child_process';
const args = process.argv;
const cmdPath = args[1];
// test
// const cmdPath = args[3]+"/test";
// process.chdir(path.dirname(cmdPath));
const projectPath = process.cwd();
let frontmatterFilePath = projectPath + "/frontmatter.md";
if (!fs.existsSync(frontmatterFilePath)) {
frontmatterFilePath = path.dirname(cmdPath) + "/frontmatter.md";
}
await fs.copyFile(frontmatterFilePath, "./slides.md");
const customSort = (a, b) => {
let str1 = a.split("-");
let str2 = b.split("-");
if (str1[0].length != str2[0].length) return str1[0].length - str2[0].length;
return 0;
};
/**
* 删除文件夹下所有问价及将文件夹下所有文件清空
* @param {*} path
*/
function emptyDir(path) {
if(fs.existsSync(path)) {
const files = fs.readdirSync(path);
files.forEach(file => {
const filePath = `${path}/${file}`;
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
emptyDir(filePath);
} else {
fs.unlinkSync(filePath);
}
});
}
}
const doneExec = (slidevPath) => {
// 过滤掉中文标题
let tArray = slidevPath.split('-');
let outDir = path.basename(tArray[0]).replace('.', '-');
outDir = '/' + outDir + '-' + tArray[2].replace(".md", "");
let outDirPath = './lessions' + outDir;
let cmd = 'pnpm build ' + slidevPath + " --base " + outDir + ' --out ' + outDirPath;
// console.log(`generate cmd: ${cmd}`);
emptyDir(outDirPath);
console.log(`清空${outDirPath}文件夹--->成功`);
execSync(cmd);
console.log(`生成${slidevPath}发布文件--->成功`);
fs.unlinkSync(slidevPath);
}
// 各章节ppt
const lessionSlidesCreate = (lessionName) => {
let dirPath = './';
let lessionFrontmatterFilePath = "./frontmatter-viewer.md";
if (!fs.existsSync(lessionFrontmatterFilePath)) {
lessionFrontmatterFilePath = path.dirname(cmdPath) + "/frontmatter-viewer.md";
}
let lessionSlidesPath = dirPath + "/"+lessionName+".md"
fs.copyFileSync(lessionFrontmatterFilePath, lessionSlidesPath);
let tArray = lessionName.split("-");
let removeUuidLessionName = tArray[0]+"-"+tArray[1];
fs.appendFileSync(lessionSlidesPath, "---\n");
fs.appendFileSync(
lessionSlidesPath,
"src: ./pages/" + removeUuidLessionName + "/README.md\n"
);
fs.appendFileSync(lessionSlidesPath, "---\n\n");
// 执行指令,生成dist
doneExec(lessionSlidesPath);
}
try {
//检索pages下的md文件,并以序号排序
let files = fs.readdirSync("./pages/");
files.sort(customSort);
let allSlidevFilePath = "./slides.md";
files.forEach((element) => {
fs.appendFileSync(allSlidevFilePath, "---\n");
fs.appendFileSync(
allSlidevFilePath,
"src: ./pages/" + element + "/README.md\n"
);
fs.appendFileSync(allSlidevFilePath, "---\n\n");
let currentLessionName = element+'-'+ uuidv4().replace(/-/g, '');
let lessionNamesFile = "./lessions/lessionNames.txt";
let generateFlag = 1; // 是否生成html
let bFound = 0; // 有没有在原记录文件名文件里找到名称
if (fs.existsSync(lessionNamesFile)) {
let lessionNames = fs.readFileSync(lessionNamesFile);
let allLessionNames = lessionNames.toString().split('\n')
// 找出原来的文件夹名称
allLessionNames.forEach((name) => {
if(name.toString().includes(element.trim().toString()) && !bFound) {
let tArray = name.split(' ');
if(tArray.length > 1) {
if(tArray[1].trim() == "0") {
console.log(tArray[0]+" not generated")
generateFlag = 0;
}
currentLessionName = tArray[0];
} else {
currentLessionName = name.trim().toString();
}
bFound = 1;
}
});
}
if(!bFound) { // 没找到原名称,则保存现在名称
fs.appendFileSync(
lessionNamesFile,
currentLessionName +'\n'
);
}
if(generateFlag) {
console.log(currentLessionName+" generated again!")
lessionSlidesCreate(currentLessionName);
console.log(currentLessionName+" generated end!\n")
} else {
console.log(currentLessionName+" pass!\n")
}
});
} catch (err) {
console.error(err);
}