-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(installer): #32 make install a sub command
- Loading branch information
Showing
4 changed files
with
100 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.