code cleanup

This commit is contained in:
Adam Matthiesen 2024-01-28 20:44:16 -08:00
parent 59676111da
commit 58dd1d2bf6
15 changed files with 87 additions and 77 deletions

View File

@ -12,4 +12,4 @@ if (requiredMajorVersion < minimumMajorVersion) {
process.exit(1); process.exit(1);
} }
import('./src/main.js').then(({ main }) => main()); import('./src/index.js').then(({ main }) => main());

View File

@ -2,6 +2,7 @@ export interface Context {
dryRun: boolean; dryRun: boolean;
installDeps: boolean; installDeps: boolean;
initGitRepo: boolean; initGitRepo: boolean;
template: string;
pkgManager: "npm" | "yarn" | "pnpm" | null; pkgManager: "npm" | "yarn" | "pnpm" | null;
args: string[]; args: string[];
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@matthiesenxyz/create-astro-ghostcms", "name": "@matthiesenxyz/create-astro-ghostcms",
"version": "0.0.1", "version": "0.0.2",
"description": "Utility to quickly get started with our Integration and astro", "description": "Utility to quickly get started with our Integration and astro",
"type": "module", "type": "module",
"main": "./create-astro-ghostcms.mjs", "main": "./create-astro-ghostcms.mjs",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

View File

@ -3,9 +3,9 @@ import arg from "arg";
import fse from "fs-extra"; import fse from "fs-extra";
import * as p from "@clack/prompts"; import * as p from "@clack/prompts";
import c from 'picocolors'; import c from 'picocolors';
import { exitPrompt, getModulePaths, isPackageManager } from "./lib/utils.js"; import { exitPrompt, getModulePaths, isPackageManager } from "./utils";
import { createBasic } from "./runners/basic.js"; import { createBasic } from "./scripts/basic.js";
import { createStarterKit } from "./runners/starterkit.js"; //import { createStarterKit } from "./scripts/starterkit.js";
export async function main() { export async function main() {
@ -89,6 +89,7 @@ export async function main() {
dryRun, dryRun,
installDeps, installDeps,
initGitRepo, initGitRepo,
template: template,
pkgManager: isPackageManager(pkgManager) ? pkgManager : null, pkgManager: isPackageManager(pkgManager) ? pkgManager : null,
args, args,
}; };
@ -99,7 +100,7 @@ export async function main() {
await createBasic(ctx).catch(console.error); await createBasic(ctx).catch(console.error);
break; break;
case "starterkit": case "starterkit":
await createStarterKit(ctx).catch(console.error); await createBasic(ctx).catch(console.error);
break; break;
default: default:
throw new Error(c.red(`Unknown template: ${template}`)); throw new Error(c.red(`Unknown template: ${template}`));
@ -122,7 +123,7 @@ function isValidTemplate(template) {
} }
/** /**
* @typedef {import("../types").Template} Template * @typedef {import("../types.js").Template} Template
* @typedef {import("../types").PackageManager} PackageManager * @typedef {import("../types.js").PackageManager} PackageManager
* @typedef {import("../types").Context} Context * @typedef {import("../types.js").Context} Context
*/ */

View File

@ -1,61 +0,0 @@
import path from "node:path";
import os from "node:os";
import * as p from "@clack/prompts";
import c from "picocolors";
import { fileURLToPath } from "node:url";
/**
* @param {string} url
*/
export function getModulePaths(url) {
const pathname = fileURLToPath(url);
const parts = pathname.split("/");
const basename = parts.pop();
const dirname = parts.join(path.sep);
return { pathname, dirname, basename };
}
/**
* @returns {never}
*/
export function exitPrompt() {
p.cancel(c.red("Operation Cancelled"));
process.exit(0);
}
/**
* @param {string} str
*/
export function isPathname(str) {
return str.includes(path.sep);
}
/**
* @param {string} pathname
*/
export function normalizePath(pathname) {
if (os.platform() === "win32") {
return path.win32.normalize(pathname);
}
return path.normalize(pathname);
}
/**
* @param {number} ms
*/
export async function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
*
* @param {string} str
* @returns {str is PackageManager}
*/
export function isPackageManager(str) {
return str === "npm" || str === "yarn" || str === "pnpm";
}
/**
* @typedef {import("../../types").PackageManager} PackageManager
*/

View File

@ -4,13 +4,13 @@ import c from 'picocolors';
import * as p from "@clack/prompts"; import * as p from "@clack/prompts";
import { execa } from "execa"; import { execa } from "execa";
import { exitPrompt, getModulePaths, isPathname, import { exitPrompt, getModulePaths, isPathname,
normalizePath, wait } from "../lib/utils.js"; normalizePath, wait } from "../utils";
const runnerName = "basic"; //const runnerName = "basic";
/** @param {Context} ctx */ /** @param {Context} ctx */
export async function createBasic(ctx) { export async function createBasic(ctx) {
let { args, dryRun, initGitRepo, installDeps } = ctx; let { args, dryRun, initGitRepo, installDeps, template } = ctx;
const s = p.spinner(); const s = p.spinner();
let cwd = process.cwd(); let cwd = process.cwd();
@ -30,7 +30,7 @@ export async function createBasic(ctx) {
if (dryRun) { if (dryRun) {
await wait(2000); await wait(2000);
} else { } else {
await createApp(project.name, project.pathname, { await createApp(project.name, project.pathname, template, {
onError(error) { onError(error) {
s.stop(`${c.red('Failed to create new project')}`); s.stop(`${c.red('Failed to create new project')}`);
p.cancel(); p.cancel();
@ -102,13 +102,14 @@ export async function createBasic(ctx) {
* *
* @param {string} projectName * @param {string} projectName
* @param {string} projectPathname * @param {string} projectPathname
* @param {string} template
* @param {{ onError: (err: unknown) => any }} opts * @param {{ onError: (err: unknown) => any }} opts
*/ */
async function createApp(projectName, projectPathname, { onError }) { async function createApp(projectName, projectPathname, template, { onError }) {
const { pathname } = getModulePaths(import.meta.url); const { pathname } = getModulePaths(import.meta.url);
const templatesDir = path.resolve(pathname, "..", "..", "templates"); const templatesDir = path.resolve(pathname, "..", "..", "templates");
const sharedTemplateDir = path.join(templatesDir, "_shared"); const sharedTemplateDir = path.join(templatesDir, "_shared");
const runnerTemplateDir = path.join(templatesDir, runnerName); const runnerTemplateDir = path.join(templatesDir, template);
await fse.ensureDir(projectPathname); await fse.ensureDir(projectPathname);

View File

@ -4,7 +4,7 @@ import c from 'picocolors';
import * as p from "@clack/prompts"; import * as p from "@clack/prompts";
import { execa } from "execa"; import { execa } from "execa";
import { exitPrompt, getModulePaths, isPathname, import { exitPrompt, getModulePaths, isPathname,
normalizePath, wait } from "../lib/utils.js"; normalizePath, wait } from "../utils";
const runnerName = "starterkit"; const runnerName = "starterkit";

View File

@ -0,0 +1,10 @@
import * as p from "@clack/prompts";
import c from "picocolors";
/**
* @returns {never}
*/
export function exitPrompt() {
p.cancel(c.red("Operation Cancelled"));
process.exit(0);
}

View File

@ -0,0 +1,13 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
/**
* @param {string} url
*/
export function getModulePaths(url) {
const pathname = fileURLToPath(url);
const parts = pathname.split("/");
const basename = parts.pop();
const dirname = parts.join(path.sep);
return { pathname, dirname, basename };
}

View File

@ -0,0 +1,6 @@
export { exitPrompt } from "./exitPrompt";
export { getModulePaths } from "./getModulePaths";
export { isPackageManager } from "./isPackageManager";
export { isPathname } from "./isPathname";
export { normalizePath } from "./normalizePath";
export { wait } from "./wait";

View File

@ -0,0 +1,12 @@
/**
*
* @param {string} str
* @returns {str is PackageManager}
*/
export function isPackageManager(str) {
return str === "npm" || str === "yarn" || str === "pnpm";
}
/**
* @typedef {import("../../types").PackageManager} PackageManager
*/

View File

@ -0,0 +1,8 @@
import path from "node:path";
/**
* @param {string} str
*/
export function isPathname(str) {
return str.includes(path.sep);
}

View File

@ -0,0 +1,12 @@
import path from "node:path";
import os from "node:os";
/**
* @param {string} pathname
*/
export function normalizePath(pathname) {
if (os.platform() === "win32") {
return path.win32.normalize(pathname);
}
return path.normalize(pathname);
}

View File

@ -0,0 +1,7 @@
/**
* @param {number} ms
*/
export async function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}