diff --git a/src/cli.ts b/src/cli.ts index 44196e8..aead38d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,5 +1,6 @@ import { hideBin } from "yargs/helpers"; import yargs from "yargs/yargs"; +import { eject } from "./eject.js"; import { generateComponent, generateHelper, @@ -149,6 +150,21 @@ yargs(hideBin(process.argv)) }); }, }) + .command({ + command: "eject", + describe: "Eject all documents", + + builder(yargs) { + return yargs.option("path", { + default: "", + description: "Eject all documents at a custom path", + type: "string", + }); + }, + handler(options) { + eject({ path: options.path }); + }, + }) .demandCommand() .strict() .parse(); diff --git a/src/eject.ts b/src/eject.ts new file mode 100644 index 0000000..ab21649 --- /dev/null +++ b/src/eject.ts @@ -0,0 +1,30 @@ +import { copy } from "fs-extra"; +import { isAbsolute, join, parse, relative } from "node:path"; +import { cwd as processCwd } from "node:process"; +import { getDocumentsPath } from "./helpers.js"; +import { success } from "./logging.js"; + +export async function eject({ + cwd = processCwd(), + path = "", +}: { cwd?: string; path?: string } = {}) { + const destination = getDestinationPath(cwd, path); + + await copy(getDocumentsPath(), destination, { + filter: (src) => parse(src).base !== "config.ts", + }); + + success(`Ejected all documents at \`${relative(cwd, destination)}\`.`); +} + +function getDestinationPath(cwd: string, path: string): string { + if (path) { + if (isAbsolute(path)) { + return path; + } else { + return join(cwd, path); + } + } + + return join(cwd, ".gember"); +} diff --git a/src/generate-document.ts b/src/generate-document.ts index 362f155..50c0cde 100644 --- a/src/generate-document.ts +++ b/src/generate-document.ts @@ -1,12 +1,12 @@ -import chalk from "chalk"; import { camelCase, kebabCase, pascalCase } from "change-case"; import { ensureDir } from "fs-extra"; import { writeFile } from "node:fs/promises"; -import { dirname, isAbsolute, join, parse, relative } from "node:path"; +import { isAbsolute, join, parse, relative } from "node:path"; import { cwd as processCwd } from "node:process"; -import { fileURLToPath } from "node:url"; import { type GenerateInputs, loadScaffdog } from "scaffdog"; import { getConfig } from "./config.js"; +import { getDocumentsPath } from "./helpers.js"; +import { success } from "./logging.js"; import { type DocumentName } from "./types.js"; export async function generateDocument( @@ -22,8 +22,7 @@ export async function generateDocument( path?: string; } = {}, ) { - const directory = dirname(fileURLToPath(import.meta.url)); - const scaffdog = await loadScaffdog(join(directory, "../documents")); + const scaffdog = await loadScaffdog(getDocumentsPath()); const documents = await scaffdog.list(); const document = documents.find((document) => document.name === documentName); @@ -55,10 +54,8 @@ export async function generateDocument( await ensureDir(parse(file.path).dir); await writeFile(file.path, file.content); - console.log( - chalk.green( - `🫚 Generated ${documentName} \`${entityName}\` at \`${relative(cwd, file.path)}\`.`, - ), + success( + `Generated ${documentName} \`${entityName}\` at \`${relative(cwd, file.path)}\`.`, ); } diff --git a/src/helpers.ts b/src/helpers.ts new file mode 100644 index 0000000..d68e4ed --- /dev/null +++ b/src/helpers.ts @@ -0,0 +1,6 @@ +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +export function getDocumentsPath(): string { + return join(dirname(fileURLToPath(import.meta.url)), "../documents"); +} diff --git a/src/logging.ts b/src/logging.ts new file mode 100644 index 0000000..e7583bf --- /dev/null +++ b/src/logging.ts @@ -0,0 +1,5 @@ +import chalk from "chalk"; + +export function success(message: string): void { + console.log(chalk.green(`🫚 ${message}`)); +}