Skip to content

Commit

Permalink
add tinacms
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDevAU committed Oct 22, 2024
1 parent eff078f commit cd80e8b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/cli/src/commands/add/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { type Command, Option } from "commander";
import type { Command } from "commander";
import consola from "consola";
import { addBiomejsAction } from "./biomejs/index.js";
import { addNextAuthAction } from "./next-auth/index.js";
import { addShadcnuiAction } from "./shadcnui/index.js";
import { addTinaCMSAction } from "./tinacms/index.js";
import { addVSCodeAction } from "./vscode/index.js";

export const CommandName = {
shadcnui: "shadcnui",
biomejs: "biomejs",
vscode: "vscode",
nextAuth: "nextAuth",
tinacms: "tinacms",
};

export function addCommand(program: Command) {
Expand All @@ -32,6 +34,9 @@ export function addCommand(program: Command) {
case CommandName.nextAuth:
await addNextAuthAction();
break;
case CommandName.tinacms:
await addTinaCMSAction();
break;
default:
consola.error(`Unknown package: ${input}`);
break;
Expand Down
44 changes: 44 additions & 0 deletions packages/cli/src/commands/add/tinacms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import consola from "consola";
import { execa } from "execa";
import {
AddPackageToConfig,
CheckConfig,
type ConfigOptions,
} from "../../../util/config.js";
import { RemoteInstall, TryRemoveFile } from "../../../util/index.js";
import { CommandName } from "../index.js";

export const description = "Add TinaCMS to your Next.js project";

export const addTinaCMSAction = async () => {
consola.log("Adding TinaCMS to your Next.js project...");
let config: ConfigOptions;
try {
config = await CheckConfig(CommandName.tinacms);
} catch (error) {
if (error instanceof Error) {
consola.error(error.message);
}
return;
}

consola.info("Initializing TinaCMS in your Next.js project...");
try {
const [command, initialArgs] = RemoteInstall(config.packageManager);
await execa(command, [...initialArgs, "@tinacms/cli@latest", "init"], {
stdio: "inherit",
});

//! Tech Debt - Remove the pages/ file created by TinaCMS init command
// Adding file at pages/demo/blog/[filename].tsx... ✅
await TryRemoveFile("pages/demo/blog/[filename].tsx");

//? Should we add an example here? i.e. a posts page and a /posts/[slug] page?
consola.success("TinaCMS has been successfully initialized!");
await AddPackageToConfig(CommandName.tinacms);
} catch (error) {
if (error instanceof Error) {
consola.error("Failed to initialize the TinaCMS:", error.message);
}
}
};
36 changes: 34 additions & 2 deletions packages/cli/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import fs, { rmdir } from "node:fs/promises";
import { unlink } from "node:fs/promises";
import path, { dirname } from "node:path";
import type { Stream } from "node:stream";
import consola from "consola";

Expand Down Expand Up @@ -157,3 +158,34 @@ export const TryWriteFile = async (
return false;
}
};

export const TryRemoveFile = async (filePath: string): Promise<boolean> => {
try {
// Remove the file
await unlink(filePath);
consola.info(`File removed at ${filePath}`);

// Now attempt to remove the directories if they are empty
let dirPath = dirname(filePath);

// Check each directory upwards and remove if empty
while (dirPath !== "/") {
try {
// Try to remove the directory
await rmdir(dirPath);
consola.info(`Empty directory removed at ${dirPath}`);
dirPath = dirname(dirPath); // Move to the parent directory
} catch (error) {
// Stop if the directory isn't empty or any other error occurs
break;
}
}

return true;
} catch (error) {
if (error instanceof Error) {
consola.error(`Failed to remove file at ${filePath}: ${error.message}`);
}
return false;
}
};

0 comments on commit cd80e8b

Please sign in to comment.