From 45e8b657f9cd7236d24192885d30c20cdecc15f5 Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Fri, 28 Feb 2025 23:50:29 +0100 Subject: [PATCH] chore(core): rewrite cli to ts --- package.json | 2 +- packages/core/package.json | 2 +- .../src/cli/commands/{build.mjs => build.ts} | 8 ++++-- .../commands/{generate.mjs => generate.ts} | 0 .../core/src/cli/commands/{new.mjs => new.ts} | 7 +++-- .../cli/commands/{prepare.mjs => prepare.ts} | 8 ++++-- .../src/cli/commands/{serve.mjs => serve.ts} | 28 +++++++++---------- packages/core/src/cli/index.js | 23 --------------- packages/core/src/cli/index.ts | 23 +++++++++++++++ 9 files changed, 53 insertions(+), 48 deletions(-) rename packages/core/src/cli/commands/{build.mjs => build.ts} (89%) rename packages/core/src/cli/commands/{generate.mjs => generate.ts} (100%) rename packages/core/src/cli/commands/{new.mjs => new.ts} (80%) rename packages/core/src/cli/commands/{prepare.mjs => prepare.ts} (78%) rename packages/core/src/cli/commands/{serve.mjs => serve.ts} (81%) delete mode 100755 packages/core/src/cli/index.js create mode 100755 packages/core/src/cli/index.ts diff --git a/package.json b/package.json index cbaa0f6..d9fba1f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "build": "pnpm -F './packages/**' build", "build:site": "pnpm --filter site build", "build:site:preview": "pnpm --filter site build:preview", - "template:basic": "pnpm --filter @templates/basic dev", + "template:basic": "pnpm run build && pnpm --filter @templates/basic start", "create": "node ./packages/create-mcfly", "cli": "node ./packages/core/cli/index.js", "test": "vitest .", diff --git a/packages/core/package.json b/packages/core/package.json index 5306999..3c9b9c8 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -17,7 +17,7 @@ "./package.json": "./package.json" }, "scripts": { - "build": "tsc", + "build": "tsc --erasableSyntaxOnly", "version": "npm version", "publish": "npm publish", "test": "echo \"Error: no test specified\" && exit 1" diff --git a/packages/core/src/cli/commands/build.mjs b/packages/core/src/cli/commands/build.ts similarity index 89% rename from packages/core/src/cli/commands/build.mjs rename to packages/core/src/cli/commands/build.ts index 7198321..5f7d7d8 100644 --- a/packages/core/src/cli/commands/build.mjs +++ b/packages/core/src/cli/commands/build.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import { consola } from 'consola' -import { defineCommand } from 'citty' +import { defineCommand, type ParsedArgs } from 'citty' import { dirname, resolve } from 'pathe' import { build, @@ -13,10 +13,12 @@ import { import { fileURLToPath } from 'node:url' import { getMcFlyConfig, getNitroConfig } from '../../get-nitro-config.js' -async function _build(args) { +async function _build(args: ParsedArgs) { consola.start('Building project...') try { - const rootDir = resolve(args.dir || args._dir || '.') + // TODO: check for dir type (should be string) + const dir: string = args.dir?.toString() || args._dir?.toString() || '.' + const rootDir = resolve(dir) const [mcflyConfig, appConfigFile] = await getMcFlyConfig() const nitroConfig = await getNitroConfig(mcflyConfig) diff --git a/packages/core/src/cli/commands/generate.mjs b/packages/core/src/cli/commands/generate.ts similarity index 100% rename from packages/core/src/cli/commands/generate.mjs rename to packages/core/src/cli/commands/generate.ts diff --git a/packages/core/src/cli/commands/new.mjs b/packages/core/src/cli/commands/new.ts similarity index 80% rename from packages/core/src/cli/commands/new.mjs rename to packages/core/src/cli/commands/new.ts index d76ad61..3d77f2d 100644 --- a/packages/core/src/cli/commands/new.mjs +++ b/packages/core/src/cli/commands/new.ts @@ -2,10 +2,11 @@ import { execSync } from 'node:child_process' import { consola } from 'consola' -import { defineCommand } from 'citty' +import { defineCommand, type ParsedArgs } from 'citty' -function createNew(args) { - const directory = args.dir || args._dir +function createNew(args: ParsedArgs) { + // TODO: check for dir type (should be string) + const directory = args?.dir || args?._dir const command = directory ? `npm create mcfly@latest ${directory}` : 'npm create mcfly@latest' diff --git a/packages/core/src/cli/commands/prepare.mjs b/packages/core/src/cli/commands/prepare.ts similarity index 78% rename from packages/core/src/cli/commands/prepare.mjs rename to packages/core/src/cli/commands/prepare.ts index 15f7563..48dafac 100755 --- a/packages/core/src/cli/commands/prepare.mjs +++ b/packages/core/src/cli/commands/prepare.ts @@ -1,18 +1,20 @@ #!/usr/bin/env node import { consola } from 'consola' -import { defineCommand } from 'citty' +import { defineCommand, type ParsedArgs } from 'citty' import { resolve } from 'pathe' import { createNitro, writeTypes } from 'nitropack' import { getMcFlyConfig, getNitroConfig } from '../../get-nitro-config.js' -async function prepare(args) { +async function prepare(args: ParsedArgs) { consola.start('Preparing McFly workspace...') let err try { - const rootDir = resolve(args.dir || args._dir || '.') + // TODO: check for dir type (should be string) + const dir: string = args.dir?.toString() || args._dir?.toString() || '.' + const rootDir = resolve(dir) const [mcflyConfig] = await getMcFlyConfig() const nitroConfig = await getNitroConfig(mcflyConfig) const nitro = await createNitro({ rootDir, ...nitroConfig }) diff --git a/packages/core/src/cli/commands/serve.mjs b/packages/core/src/cli/commands/serve.ts similarity index 81% rename from packages/core/src/cli/commands/serve.mjs rename to packages/core/src/cli/commands/serve.ts index fc4767f..58d37c8 100644 --- a/packages/core/src/cli/commands/serve.mjs +++ b/packages/core/src/cli/commands/serve.ts @@ -2,9 +2,10 @@ import { consola } from 'consola' import { colorize } from 'consola/utils' -import { defineCommand } from 'citty' +import { defineCommand, type ParsedArgs } from 'citty' import { createRequire } from 'node:module' import { + type Nitro, build, createDevServer, createNitro, @@ -35,18 +36,13 @@ async function printInfo() { } } -async function serve(args) { +async function serve(args: ParsedArgs) { try { - /** - * @type {string} - */ - const rootDir = resolve(args.dir || args._dir || '.') + // TODO: check for dir type (should be string) + const dir = args.dir?.toString() || args._dir?.toString() + const rootDir: string = resolve(dir || '.') - /** - * @typedef {import('nitropack').Nitro} Nitro - * @type {Nitro} - */ - let nitro + let nitro: Nitro const reload = async () => { // close existing nitro if (nitro) { @@ -72,7 +68,7 @@ async function serve(args) { { watch: true, c12: { - async onUpdate({ getDiff, newConfig }) { + async onUpdate({ getDiff, newConfig }: unknown) { const diff = getDiff() if (diff.length === 0) { @@ -81,10 +77,14 @@ async function serve(args) { consola.info( 'Nitro config updated:\n' + - diff.map((entry) => ` ${entry.toString()}`).join('\n') + diff + .map((entry: unknown) => ` ${entry?.toString()}`) + .join('\n') ) - await (diff.every((e) => hmrKeyRe.test(e.key)) + // TODO: get types for c12 config & remove unknown + // @ts-ignore + await (diff.every((e: unknown) => hmrKeyRe.test(e.key)) ? nitro.updateConfig(newConfig.config || {}) // Hot reload : reload()) // Full reload }, diff --git a/packages/core/src/cli/index.js b/packages/core/src/cli/index.js deleted file mode 100755 index 5de7a97..0000000 --- a/packages/core/src/cli/index.js +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env node -import { defineCommand, runMain } from 'citty' - -const main = defineCommand({ - meta: { - name: 'mcfly', - description: 'McFly CLI', - }, - subCommands: { - new: () => import('./commands/new.mjs').then((r) => r.default), - serve: () => import('./commands/serve.mjs').then((r) => r.default), - build: () => import('./commands/build.mjs').then((r) => r.default), - prepare: () => import('./commands/prepare.mjs').then((r) => r.default), - generate: () => import('./commands/generate.mjs').then((r) => r.default), - g: () => import('./commands/generate.mjs').then((r) => r.default), - }, -}) - -runMain(main) - -export const exportedForTest = { - main, -} diff --git a/packages/core/src/cli/index.ts b/packages/core/src/cli/index.ts new file mode 100755 index 0000000..674d278 --- /dev/null +++ b/packages/core/src/cli/index.ts @@ -0,0 +1,23 @@ +#!/usr/bin/env node +import { defineCommand, runMain, type ArgsDef, type CommandDef } from 'citty' + +const main: CommandDef = defineCommand({ + meta: { + name: 'mcfly', + description: 'McFly CLI', + }, + subCommands: { + build: () => import('./commands/build.js').then((r) => r.default), + generate: () => import('./commands/generate.js').then((r) => r.default), + new: () => import('./commands/new.js').then((r) => r.default), + prepare: () => import('./commands/prepare.js').then((r) => r.default), + serve: () => import('./commands/serve.js').then((r) => r.default), + g: () => import('./commands/generate.js').then((r) => r.default), + }, +}) + +runMain(main) + +export const exportedForTest = { + main, +} as const