tiddlywiki-plugin-dev
Version:
[](https://github.com/tiddly-gittly)
102 lines (101 loc) • 3.6 kB
JavaScript
import fs from "fs";
import path from "path";
import { execSync } from "child_process";
import chalk from "chalk";
import inquirer from "inquirer";
import simpleGitt from "simple-git";
const init = async (project, githubUrl, npmUrl) => {
if (fs.existsSync(project)) {
console.error(`${project} already exists!`);
return;
}
const { npm, authorName } = await inquirer.prompt([
{
type: "list",
name: "npm",
message: "Which package manager do you use?",
choices: ["npm", "yarn", "pnpm", "tnpm", "cnpm"],
default: "npm"
},
{
type: "input",
name: "authorName",
message: "What's your name ($:/plugins/<fill in your name>/plugin-name)"
}
]);
const { pluginName } = await inquirer.prompt([
{
type: "input",
name: "pluginName",
message: `What's the plugin's name ($:/plugins/${authorName}/<fill in plugin name>)`
}
]);
console.log(chalk.green.bold(`Cloning template project from ${githubUrl}`));
await simpleGitt().clone(githubUrl, project, ["-b", "template"]);
const git = simpleGitt({
baseDir: path.resolve(project)
});
await git.removeRemote("origin");
await git.branch(["-m", "template", "master"]);
const shallowPath = path.resolve(project, ".git", "shallow");
if (fs.existsSync(shallowPath)) {
fs.rmSync(path.resolve(project, ".git", "shallow"));
}
if (npmUrl) {
fs.writeFileSync(
path.resolve(project, ".npmrc"),
[
...fs.readFileSync(path.resolve(project, ".npmrc"), "utf-8").split("\n"),
`registry=${npmUrl}`
].map((line) => line.trim()).filter((line) => line !== "").join("\n")
);
}
execSync(`${npm} install`, { cwd: path.resolve(project), stdio: "inherit" });
execSync(`${npm} run update`, {
cwd: path.resolve(project),
stdio: "inherit"
});
execSync(`${npm} install`, { cwd: path.resolve(project), stdio: "inherit" });
const ciPath = path.resolve(project, ".github", "workflows");
if (fs.existsSync(ciPath)) {
for (const file of fs.readdirSync(ciPath)) {
if (path.extname(file) !== ".yml") {
continue;
}
const filePath = path.resolve(ciPath, file);
const content = fs.readFileSync(filePath, "utf-8").replace(
/npm\s+install\s+-g\s+pnpm\s+&&\s+/g,
npm === "npm" ? "" : `npm install -g ${npm} && `
).replace(/pnpm\s+install/g, `${npm} install`).replace(/pnpm\s+run/g, `${npm} run`);
fs.writeFileSync(filePath, content, "utf-8");
}
}
execSync(`npx dprint config update`, {
cwd: path.resolve(project),
stdio: "inherit"
});
const nameFrom = "$:/plugins/your-name/plugin-name";
const nameTo = `$:/plugins/${authorName}/${pluginName}`;
const pluginPathFrom = path.resolve(project, "src", "plugin-name");
const pluginPathTo = path.resolve(project, "src", pluginName);
replaceStringInFilesSync(pluginPathFrom, nameFrom, nameTo);
fs.renameSync(pluginPathFrom, pluginPathTo);
replaceStringInFilesSync(path.resolve(project, "wiki"), nameFrom, nameTo);
};
function replaceStringInFilesSync(dir, searchString, replaceString) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isFile()) {
const data = fs.readFileSync(filePath, "utf8");
const result = data.replaceAll(searchString, replaceString);
fs.writeFileSync(filePath, result, "utf8");
} else if (stats.isDirectory()) {
replaceStringInFilesSync(filePath, searchString, replaceString);
}
});
}
export {
init
};