chore(core): rewrite cli to ts
This commit is contained in:
parent
f3ee5eebb5
commit
45e8b657f9
9 changed files with 53 additions and 48 deletions
|
@ -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 .",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
|
@ -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'
|
|
@ -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 })
|
|
@ -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
|
||||
},
|
|
@ -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,
|
||||
}
|
23
packages/core/src/cli/index.ts
Executable file
23
packages/core/src/cli/index.ts
Executable file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env node
|
||||
import { defineCommand, runMain, type ArgsDef, type CommandDef } from 'citty'
|
||||
|
||||
const main: CommandDef<ArgsDef> = 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
|
Loading…
Reference in a new issue