Skip to content

Commit

Permalink
refactor(installer): #32 make install a sub command
Browse files Browse the repository at this point in the history
  • Loading branch information
sdorra committed Nov 13, 2024
1 parent 145d1b2 commit 32678fd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 60 deletions.
5 changes: 5 additions & 0 deletions packages/content-collections/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"name": "content-collections",
"description": "Installer for content-collections",
"version": "0.1.0",
"type": "module",
"main": "dist/index.js",
"types": "./dist/index.d.ts",
"bin": {
"content-collections": "./dist/index.js"
},
"exports": {
"./package.json": "./package.json",
".": {
Expand All @@ -22,6 +26,7 @@
"@content-collections/installer": "workspace:^",
"@inquirer/prompts": "^7.0.0",
"chalk": "^5.3.0",
"clerc": "^0.44.0",
"listr2": "^8.2.5",
"zod": "^3.23.8"
},
Expand Down
66 changes: 66 additions & 0 deletions packages/content-collections/src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env node
import { createMigrator, type Option } from "@content-collections/installer";
import { input, select } from "@inquirer/prompts";
import chalk from "chalk";
import { Listr } from "listr2";

async function ask(option: Option) {
if (option.type === "list") {
const result = await select({
message: option.description || `Select an ${option.name}`,
default: option.defaultValue,
choices: option.choices,
});
if (typeof result !== "string") {
throw new Error("Invalid selection, the result must be a string");
}

return result;
}

return input({
default: option.defaultValue,
message: option.description || `Enter ${option.name}`,
});
}

export default async function install(directory: string) {
console.log("Searching for migrator in", directory);
const migrator = await createMigrator(directory);

console.log();
console.log(
"Found migrator",
chalk.bold(migrator.name) + ",",
"collecting options",
);
const migration = await migrator.createMigration(ask);
console.log();
console.log("Migration tasks:");

const tasks = new Listr(
migration.map((t) => ({
title: chalk.bold(t.name),
task: async (_, task) => {
const result = await t.run();
if (result.status === "skipped") {
task.skip(`${chalk.bold(t.name)}: ${result.message} [SKIPPED]`);
} else if (result.status === "error") {
throw new Error(`${chalk.bold(t.name)}: ${result.message} [ERROR]`);
}
},
})),
{
concurrent: false,
collectErrors: "minimal",
exitOnError: false,
},
);

await tasks.run();
if (tasks.errors.length > 0) {
console.log();
console.error("Errors occurred during migration");
process.exit(1);
}
}
86 changes: 26 additions & 60 deletions packages/content-collections/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,34 @@
#!/usr/bin/env node
import { createMigrator, type Option } from "@content-collections/installer";
import { input, select } from "@inquirer/prompts";
import chalk from "chalk";
import { Listr } from "listr2";

async function ask(option: Option) {
if (option.type === "list") {
const result = await select({
message: option.description || `Select an ${option.name}`,
default: option.defaultValue,
choices: option.choices,
});
if (typeof result !== "string") {
throw new Error("Invalid selection, the result must be a string");
}
import { Clerc, completionsPlugin, helpPlugin, versionPlugin } from "clerc";
import install from "./commands/install.js";

return result;
}
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const packageJson = require("../package.json");

return input({
default: option.defaultValue,
message: option.description || `Enter ${option.name}`,
});
const name = "content-collections";
if (!packageJson.bin[name]) {
throw new Error(`Missing bin entry for ${name} in package.json`);
}

async function install() {
console.log("Searching for migrator in", process.cwd());
const migrator = await createMigrator(process.cwd());

console.log();
console.log(
"Found migrator",
chalk.bold(migrator.name) + ",",
"collecting options",
);
const migration = await migrator.createMigration(ask);
console.log();
console.log("Migration tasks:");

const tasks = new Listr(
migration.map((t) => ({
title: chalk.bold(t.name),
task: async (_, task) => {
const result = await t.run();
if (result.status === "skipped") {
task.skip(`${chalk.bold(t.name)}: ${result.message} [SKIPPED]`);
} else if (result.status === "error") {
throw new Error(`${chalk.bold(t.name)}: ${result.message} [ERROR]`);
}
Clerc.create()
.scriptName(name)
.description(packageJson.description)
.version(packageJson.version)
.command("install", "Installs content-collection to a existing project", {
flags: {
directory: {
type: String,
description: "Path to the existing project",
default: ".",
},
})),
{
concurrent: false,
collectErrors: "minimal",
exitOnError: false,
},
);

await tasks.run();
if (tasks.errors.length > 0) {
console.log();
console.error("Errors occurred during migration");
process.exit(1);
}
}

install();
})
.on("install", (context) => {
return install(context.flags.directory);
})
.use(helpPlugin())
.use(versionPlugin())
.use(completionsPlugin())
.parse();
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 32678fd

Please sign in to comment.