Skip to content

Commit

Permalink
Support ejecting all documents
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Nov 14, 2024
1 parent a69905a commit 0dbee32
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { eject } from "./eject.js";
import {
generateComponent,
generateHelper,
Expand Down Expand Up @@ -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();
30 changes: 30 additions & 0 deletions src/eject.ts
Original file line number Diff line number Diff line change
@@ -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");
}
15 changes: 6 additions & 9 deletions src/generate-document.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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);

Expand Down Expand Up @@ -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)}\`.`,
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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");
}
5 changes: 5 additions & 0 deletions src/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import chalk from "chalk";

export function success(message: string): void {
console.log(chalk.green(`🫚 ${message}`));
}

0 comments on commit 0dbee32

Please sign in to comment.