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": "pnpm -F './packages/**' build",
|
||||||
"build:site": "pnpm --filter site build",
|
"build:site": "pnpm --filter site build",
|
||||||
"build:site:preview": "pnpm --filter site build:preview",
|
"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",
|
"create": "node ./packages/create-mcfly",
|
||||||
"cli": "node ./packages/core/cli/index.js",
|
"cli": "node ./packages/core/cli/index.js",
|
||||||
"test": "vitest .",
|
"test": "vitest .",
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
"./package.json": "./package.json"
|
"./package.json": "./package.json"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc --erasableSyntaxOnly",
|
||||||
"version": "npm version",
|
"version": "npm version",
|
||||||
"publish": "npm publish",
|
"publish": "npm publish",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import { defineCommand } from 'citty'
|
import { defineCommand, type ParsedArgs } from 'citty'
|
||||||
import { dirname, resolve } from 'pathe'
|
import { dirname, resolve } from 'pathe'
|
||||||
import {
|
import {
|
||||||
build,
|
build,
|
||||||
|
@ -13,10 +13,12 @@ import {
|
||||||
import { fileURLToPath } from 'node:url'
|
import { fileURLToPath } from 'node:url'
|
||||||
import { getMcFlyConfig, getNitroConfig } from '../../get-nitro-config.js'
|
import { getMcFlyConfig, getNitroConfig } from '../../get-nitro-config.js'
|
||||||
|
|
||||||
async function _build(args) {
|
async function _build(args: ParsedArgs) {
|
||||||
consola.start('Building project...')
|
consola.start('Building project...')
|
||||||
try {
|
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 [mcflyConfig, appConfigFile] = await getMcFlyConfig()
|
||||||
const nitroConfig = await getNitroConfig(mcflyConfig)
|
const nitroConfig = await getNitroConfig(mcflyConfig)
|
|
@ -2,10 +2,11 @@
|
||||||
|
|
||||||
import { execSync } from 'node:child_process'
|
import { execSync } from 'node:child_process'
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import { defineCommand } from 'citty'
|
import { defineCommand, type ParsedArgs } from 'citty'
|
||||||
|
|
||||||
function createNew(args) {
|
function createNew(args: ParsedArgs) {
|
||||||
const directory = args.dir || args._dir
|
// TODO: check for dir type (should be string)
|
||||||
|
const directory = args?.dir || args?._dir
|
||||||
const command = directory
|
const command = directory
|
||||||
? `npm create mcfly@latest ${directory}`
|
? `npm create mcfly@latest ${directory}`
|
||||||
: 'npm create mcfly@latest'
|
: 'npm create mcfly@latest'
|
|
@ -1,18 +1,20 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import { defineCommand } from 'citty'
|
import { defineCommand, type ParsedArgs } from 'citty'
|
||||||
import { resolve } from 'pathe'
|
import { resolve } from 'pathe'
|
||||||
import { createNitro, writeTypes } from 'nitropack'
|
import { createNitro, writeTypes } from 'nitropack'
|
||||||
import { getMcFlyConfig, getNitroConfig } from '../../get-nitro-config.js'
|
import { getMcFlyConfig, getNitroConfig } from '../../get-nitro-config.js'
|
||||||
|
|
||||||
async function prepare(args) {
|
async function prepare(args: ParsedArgs) {
|
||||||
consola.start('Preparing McFly workspace...')
|
consola.start('Preparing McFly workspace...')
|
||||||
|
|
||||||
let err
|
let err
|
||||||
|
|
||||||
try {
|
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 [mcflyConfig] = await getMcFlyConfig()
|
||||||
const nitroConfig = await getNitroConfig(mcflyConfig)
|
const nitroConfig = await getNitroConfig(mcflyConfig)
|
||||||
const nitro = await createNitro({ rootDir, ...nitroConfig })
|
const nitro = await createNitro({ rootDir, ...nitroConfig })
|
|
@ -2,9 +2,10 @@
|
||||||
|
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import { colorize } from 'consola/utils'
|
import { colorize } from 'consola/utils'
|
||||||
import { defineCommand } from 'citty'
|
import { defineCommand, type ParsedArgs } from 'citty'
|
||||||
import { createRequire } from 'node:module'
|
import { createRequire } from 'node:module'
|
||||||
import {
|
import {
|
||||||
|
type Nitro,
|
||||||
build,
|
build,
|
||||||
createDevServer,
|
createDevServer,
|
||||||
createNitro,
|
createNitro,
|
||||||
|
@ -35,18 +36,13 @@ async function printInfo() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function serve(args) {
|
async function serve(args: ParsedArgs) {
|
||||||
try {
|
try {
|
||||||
/**
|
// TODO: check for dir type (should be string)
|
||||||
* @type {string}
|
const dir = args.dir?.toString() || args._dir?.toString()
|
||||||
*/
|
const rootDir: string = resolve(dir || '.')
|
||||||
const rootDir = resolve(args.dir || args._dir || '.')
|
|
||||||
|
|
||||||
/**
|
let nitro: Nitro
|
||||||
* @typedef {import('nitropack').Nitro} Nitro
|
|
||||||
* @type {Nitro}
|
|
||||||
*/
|
|
||||||
let nitro
|
|
||||||
const reload = async () => {
|
const reload = async () => {
|
||||||
// close existing nitro
|
// close existing nitro
|
||||||
if (nitro) {
|
if (nitro) {
|
||||||
|
@ -72,7 +68,7 @@ async function serve(args) {
|
||||||
{
|
{
|
||||||
watch: true,
|
watch: true,
|
||||||
c12: {
|
c12: {
|
||||||
async onUpdate({ getDiff, newConfig }) {
|
async onUpdate({ getDiff, newConfig }: unknown) {
|
||||||
const diff = getDiff()
|
const diff = getDiff()
|
||||||
|
|
||||||
if (diff.length === 0) {
|
if (diff.length === 0) {
|
||||||
|
@ -81,10 +77,14 @@ async function serve(args) {
|
||||||
|
|
||||||
consola.info(
|
consola.info(
|
||||||
'Nitro config updated:\n' +
|
'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
|
? nitro.updateConfig(newConfig.config || {}) // Hot reload
|
||||||
: reload()) // Full 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