@eclipsefdn/create-revealjs-solstice
Version:
Create starter template for revealjs-solstice
144 lines (125 loc) • 3.83 kB
JavaScript
import chalk from "chalk";
import path from "node:path";
import prompts from "prompts";
import minimist from "minimist";
import fs from "fs-extra";
import { fileURLToPath } from "node:url";
import { x } from "tinyexec";
import { globSync } from "glob";
async function init() {
const argv = minimist(process.argv.slice(2));
const cwd = process.cwd();
let targetDir = argv._[0];
const defaultProjectName = "my-revealjs-presentation";
let result = {};
try {
result = await prompts(
[
{
type: targetDir ? null : "text",
name: "projectName",
message: "Project name:",
initial: defaultProjectName,
onState: (state) =>
(targetDir = state.value.trim() || defaultProjectName),
},
{
type: () =>
!fs.existsSync(targetDir) || fs.readdirSync(targetDir).length === 0
? null
: "confirm",
name: "overwrite",
message: () =>
`Target directory "${targetDir}" is not empty. Remove existing files and continue?`,
},
{
name: "overwriteChecker",
type: (prev, values) => {
if (values.overwrite === false) {
throw new Error(chalk.red("✖") + " Operation cancelled");
}
return null;
},
},
],
{
onCancel: () => {
throw new Error(chalk.red("✖") + " Operation cancelled");
},
}
);
} catch (cancelled) {
console.log(cancelled.message);
return;
}
const { overwrite } = result;
const root = path.join(cwd, targetDir);
if (overwrite) {
fs.emptyDirSync(root);
} else if (!fs.existsSync(root)) {
fs.mkdirSync(root);
}
console.log(`\nScaffolding project in ${root}...`);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const templateDir = path.join(__dirname, "template");
fs.copySync(templateDir, root);
// Rename __ files to dot files. See build.mjs for more details.
const files = globSync('**/*', { cwd: root, dot: true });
for (const file of files) {
const dirname = path.dirname(file);
const basename = path.basename(file);
if (basename.startsWith('__')) {
const newName = '.' + basename.substring(2);
const oldPath = path.join(root, file);
const newPath = path.join(root, dirname, newName);
fs.renameSync(oldPath, newPath);
}
}
const pkg = fs.readJsonSync(path.join(root, "package.json"));
pkg.name = targetDir;
fs.writeJsonSync(path.join(root, "package.json"), pkg, { spaces: 2 });
console.log(chalk.green(`Done. \n`));
const { yes } = await prompts({
type: "confirm",
name: "yes",
initial: "Y",
message: "Install and start dev server?",
});
const pkgManager =
/pnpm/.test(process.env.npm_execpath || "") ||
/pnpm/.test(process.env.npm_config_user_agent || "")
? "pnpm"
: /yarn/.test(process.env.npm_execpath || "")
? "yarn"
: "npm";
if (yes) {
const { agent } = await prompts({
type: "select",
name: "agent",
message: "Select the package manager to use",
choices: ["yarn", "npm", "pnpm"].map((value) => ({
title: value,
value,
})),
});
if (!agent) return;
await x(agent, ["install"], {
nodeOptions: { stdio: "inherit", cwd: root },
});
await x(agent, ["run", "dev"], {
nodeOptions: { stdio: "inherit", cwd: root },
});
} else {
console.log(
`${chalk.dim("\n Start it later by:")}
${root !== cwd ? `\n ${chalk.blue(`cd ${chalk.bold(targetDir)}`)}` : ""}
${chalk.blue(`${pkgManager} install`)}
${chalk.blue(`${pkgManager} run dev`)}
`
);
}
}
init().catch((e) => {
console.error(e);
});