mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat: initial version of nu cli (#54)
This commit is contained in:
parent
b0a8ec6368
commit
a030c62d29
@ -26,8 +26,8 @@
|
||||
"jest": "^26.6.3",
|
||||
"jiti": "^1.9.1",
|
||||
"lerna": "^4.0.0",
|
||||
"mkdist": "^0.1.5",
|
||||
"ts-jest": "^26.5.4",
|
||||
"typescript": "^4.2.4"
|
||||
"typescript": "^4.2.4",
|
||||
"unbuild": "^0.1.12"
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
"unbuild": "^0.1.12"
|
||||
},
|
||||
"dependencies": {
|
||||
"@vueuse/head": "^0.5.1",
|
||||
|
@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process._startTime = Date.now()
|
||||
require('../dist').main()
|
||||
|
@ -2,6 +2,7 @@ import type { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: false,
|
||||
inlineDependencies: true,
|
||||
entries: [
|
||||
'src/index'
|
||||
],
|
||||
|
@ -12,11 +12,16 @@
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
},
|
||||
"files": [
|
||||
"bin",
|
||||
"dist"
|
||||
]
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/mri": "^1.1.0",
|
||||
"colorette": "^1.2.2",
|
||||
"listhen": "^0.2.3",
|
||||
"mri": "^1.1.6",
|
||||
"unbuild": "^0.1.12",
|
||||
"v8-compile-cache": "^2.3.0"
|
||||
}
|
||||
}
|
||||
|
15
packages/cli/src/commands/build.ts
Normal file
15
packages/cli/src/commands/build.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { buildNuxt, loadNuxt } from '../utils/nuxt'
|
||||
|
||||
export async function invoke (args) {
|
||||
const nuxt = await loadNuxt({
|
||||
rootDir: args._[0],
|
||||
for: 'build'
|
||||
})
|
||||
|
||||
await buildNuxt(nuxt)
|
||||
}
|
||||
|
||||
export const meta = {
|
||||
usage: 'nu build [rootDir]',
|
||||
description: 'Build nuxt for production deployment'
|
||||
}
|
23
packages/cli/src/commands/dev.ts
Normal file
23
packages/cli/src/commands/dev.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { createServer } from '../utils/server'
|
||||
import { buildNuxt, loadNuxt } from '../utils/nuxt'
|
||||
|
||||
export async function invoke (args) {
|
||||
const server = createServer()
|
||||
const listenPromise = server.listen({ clipboard: true })
|
||||
|
||||
const nuxt = await loadNuxt({
|
||||
rootDir: args._[0],
|
||||
for: 'dev'
|
||||
})
|
||||
|
||||
server.setApp(nuxt.server.app)
|
||||
|
||||
await buildNuxt(nuxt)
|
||||
|
||||
await listenPromise
|
||||
}
|
||||
|
||||
export const meta = {
|
||||
usage: 'nu dev [rootDir]',
|
||||
description: 'Run nuxt development server'
|
||||
}
|
5
packages/cli/src/commands/index.ts
Normal file
5
packages/cli/src/commands/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export const commands = {
|
||||
dev: () => import('./dev'),
|
||||
build: () => import('./build'),
|
||||
usage: () => import('./usage')
|
||||
}
|
15
packages/cli/src/commands/usage.ts
Normal file
15
packages/cli/src/commands/usage.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { cyan } from 'colorette'
|
||||
import { commands } from './index'
|
||||
|
||||
export function invoke (_args) {
|
||||
const sections: string[] = []
|
||||
|
||||
sections.push(`Usage: ${cyan(`nu ${Object.keys(commands).join('|')} [args]`)}`)
|
||||
|
||||
console.log(sections.join('\n\n') + '\n')
|
||||
}
|
||||
|
||||
export const meta = {
|
||||
usage: 'nu help',
|
||||
description: 'Show help'
|
||||
}
|
@ -1,38 +1,45 @@
|
||||
import { resolve } from 'path'
|
||||
import 'v8-compile-cache'
|
||||
import mri from 'mri'
|
||||
import { red, cyan, green } from 'colorette'
|
||||
import { version } from '../package.json'
|
||||
import { commands } from './commands'
|
||||
import { showHelp } from './utils/help'
|
||||
|
||||
async function _main () {
|
||||
const args = process.argv.splice(2)
|
||||
const cmd = args[0]
|
||||
const _argv = process.argv.slice(2)
|
||||
const args = mri(_argv)
|
||||
// @ts-ignore
|
||||
let command = args._.shift() || 'usage'
|
||||
|
||||
if (!['dev', 'build'].includes(cmd)) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Usage nuxt dev|build [rootDir]')
|
||||
console.log(green(`Nuxt CLI v${version}`))
|
||||
|
||||
if (!(command in commands)) {
|
||||
console.log('\n' + red('Invalid command ' + command))
|
||||
command = 'usage'
|
||||
}
|
||||
|
||||
if (command === 'usage') {
|
||||
console.log(`\nUsage: ${cyan(`nu ${Object.keys(commands).join('|')} [args]`)}\n`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const isDev = cmd === 'dev'
|
||||
const rootDir = resolve(process.cwd(), args[1] || '.')
|
||||
|
||||
const pkg = 'nuxt3'
|
||||
const { loadNuxt, build } = require(pkg)
|
||||
|
||||
const nuxt = await loadNuxt({
|
||||
for: isDev ? 'dev' : 'build',
|
||||
rootDir
|
||||
})
|
||||
|
||||
if (isDev) {
|
||||
// https://github.com/nuxt-contrib/listhen
|
||||
await nuxt.server.listen(3000, { name: 'Nuxt' })
|
||||
try {
|
||||
const cmd = await commands[command]()
|
||||
if (args.h || args.help) {
|
||||
showHelp(cmd.meta)
|
||||
} else {
|
||||
await cmd.invoke(args)
|
||||
}
|
||||
} catch (err) {
|
||||
onFatalError(err)
|
||||
}
|
||||
}
|
||||
|
||||
await build(nuxt)
|
||||
function onFatalError (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
export function main () {
|
||||
_main()
|
||||
.catch((error) => {
|
||||
require('consola').fatal(error)
|
||||
require('exit')(2)
|
||||
})
|
||||
_main().catch(onFatalError)
|
||||
}
|
||||
|
16
packages/cli/src/utils/help.ts
Normal file
16
packages/cli/src/utils/help.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { cyan, magenta } from 'colorette'
|
||||
export function showHelp (meta?) {
|
||||
const sections: string[] = []
|
||||
|
||||
if (meta.usage) {
|
||||
sections.push(magenta('> ') + 'Usage: ' + cyan(meta.usage))
|
||||
}
|
||||
|
||||
if (meta.description) {
|
||||
sections.push(magenta('⋮ ') + meta.description)
|
||||
}
|
||||
|
||||
sections.push(`Use ${cyan('nu [command] --help')} to see help for each command`)
|
||||
|
||||
console.log(sections.join('\n\n') + '\n')
|
||||
}
|
13
packages/cli/src/utils/nuxt.ts
Normal file
13
packages/cli/src/utils/nuxt.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export function getNuxtPkg () {
|
||||
return Promise.resolve(require('nuxt3'))
|
||||
}
|
||||
|
||||
export async function loadNuxt (opts) {
|
||||
const { loadNuxt } = await getNuxtPkg()
|
||||
return loadNuxt(opts)
|
||||
}
|
||||
|
||||
export async function buildNuxt (nuxt) {
|
||||
const { build } = await getNuxtPkg()
|
||||
return build(nuxt)
|
||||
}
|
26
packages/cli/src/utils/server.ts
Normal file
26
packages/cli/src/utils/server.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import type { RequestListener } from 'http'
|
||||
|
||||
export function createServer () {
|
||||
const listener = createDynamicFunction <RequestListener>((_req, res) => {
|
||||
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
|
||||
res.end('<!DOCTYPE html><html><head><meta http-equiv="refresh" content="1"><head><body>...')
|
||||
})
|
||||
|
||||
async function listen (opts) {
|
||||
const { listen } = await import('listhen')
|
||||
return listen(listener.call, opts)
|
||||
}
|
||||
|
||||
return {
|
||||
setApp: (app: RequestListener) => listener.set(app),
|
||||
listen
|
||||
}
|
||||
}
|
||||
|
||||
function createDynamicFunction<T extends (...args) => any>(initialValue: T) {
|
||||
let fn: T = initialValue
|
||||
return {
|
||||
set: (newFn: T) => { fn = newFn },
|
||||
call: ((...args) => fn(...args)) as T
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
"unbuild": "^0.1.12"
|
||||
},
|
||||
"dependencies": {
|
||||
"consola": "^2.15.3",
|
||||
|
@ -73,6 +73,6 @@
|
||||
"@types/http-proxy": "^1.17.5",
|
||||
"@types/node-fetch": "^2.5.10",
|
||||
"@types/serve-static": "^1.13.9",
|
||||
"unbuild": "^0.1.11"
|
||||
"unbuild": "^0.1.12"
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,8 @@ export function createDevServer (nitroContext: NitroContext) {
|
||||
// console.error('[proxy]', err)
|
||||
})
|
||||
} else {
|
||||
res.end('Worker not ready!')
|
||||
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
|
||||
res.end('<!DOCTYPE html><html><head><meta http-equiv="refresh" content="1"><head><body>...')
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -34,6 +34,6 @@
|
||||
"@types/fs-extra": "^9.0.10",
|
||||
"@types/hash-sum": "^1.0.0",
|
||||
"@types/lodash": "^4.14.168",
|
||||
"unbuild": "^0.1.11"
|
||||
"unbuild": "^0.1.12"
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
"unbuild": "^0.1.12"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nuxt/kit": "^0.2.0",
|
||||
|
@ -50,6 +50,6 @@
|
||||
"@types/webpack-bundle-analyzer": "^3.9.2",
|
||||
"@types/webpack-dev-middleware": "^4.1.2",
|
||||
"@types/webpack-hot-middleware": "^2.25.4",
|
||||
"unbuild": "^0.1.11"
|
||||
"unbuild": "^0.1.12"
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { defineNuxtConfig } from '@nuxt/kit'
|
||||
|
||||
export default defineNuxtConfig({
|
||||
vite: false
|
||||
vite: true
|
||||
})
|
||||
|
2
scripts/nu
Executable file
2
scripts/nu
Executable file
@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env node
|
||||
require('../packages/cli/bin/nuxt')
|
40
yarn.lock
40
yarn.lock
@ -1594,7 +1594,7 @@ __metadata:
|
||||
"@vueuse/head": ^0.5.1
|
||||
hookable: ^4.4.1
|
||||
ohmyfetch: ^0.2.0
|
||||
unbuild: ^0.1.11
|
||||
unbuild: ^0.1.12
|
||||
vue: ^3.0.11
|
||||
vue-router: ^4.0.6
|
||||
vuex5: ^0.5.0-testing.3
|
||||
@ -1638,7 +1638,7 @@ __metadata:
|
||||
scule: ^0.1.1
|
||||
std-env: ^2.3.0
|
||||
ufo: ^0.6.11
|
||||
unbuild: ^0.1.11
|
||||
unbuild: ^0.1.12
|
||||
unctx: ^0.0.3
|
||||
untyped: ^0.2.5
|
||||
upath: ^2.0.1
|
||||
@ -1705,7 +1705,7 @@ __metadata:
|
||||
std-env: ^2.3.0
|
||||
table: ^6.0.9
|
||||
ufo: ^0.6.11
|
||||
unbuild: ^0.1.11
|
||||
unbuild: ^0.1.12
|
||||
upath: ^2.0.1
|
||||
vue: 3.0.11
|
||||
vue-bundle-renderer: ^0.2.3
|
||||
@ -1739,7 +1739,7 @@ __metadata:
|
||||
"@vue/compiler-sfc": ^3.0.11
|
||||
consola: ^2.15.3
|
||||
fs-extra: ^9.1.0
|
||||
unbuild: ^0.1.11
|
||||
unbuild: ^0.1.12
|
||||
vite: ^2.1.5
|
||||
vue: 3.0.11
|
||||
languageName: unknown
|
||||
@ -1777,7 +1777,7 @@ __metadata:
|
||||
style-resources-loader: ^1.4.1
|
||||
time-fix-plugin: ^2.0.7
|
||||
ufo: ^0.6.11
|
||||
unbuild: ^0.1.11
|
||||
unbuild: ^0.1.12
|
||||
url-loader: ^4.1.1
|
||||
vue: 3.0.11
|
||||
vue-loader: ^16.2.0
|
||||
@ -2346,6 +2346,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/mri@npm:^1.1.0":
|
||||
version: 1.1.0
|
||||
resolution: "@types/mri@npm:1.1.0"
|
||||
checksum: 6e7ef55196c46115bb9b31a86751a9f14056459364540328419fcecb35f5379ec47ad35e73c957b7cd2fd7b087edd383b58a6734dce7a9d2b3296b03aa971a53
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/node-fetch@npm:^2.5.10":
|
||||
version: 2.5.10
|
||||
resolution: "@types/node-fetch@npm:2.5.10"
|
||||
@ -9509,7 +9516,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"mkdist@npm:^0.1.5, mkdist@npm:^0.1.6":
|
||||
"mkdist@npm:^0.1.6":
|
||||
version: 0.1.6
|
||||
resolution: "mkdist@npm:0.1.6"
|
||||
dependencies:
|
||||
@ -10029,7 +10036,12 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "nuxt-cli@workspace:packages/cli"
|
||||
dependencies:
|
||||
unbuild: ^0.1.11
|
||||
"@types/mri": ^1.1.0
|
||||
colorette: ^1.2.2
|
||||
listhen: ^0.2.3
|
||||
mri: ^1.1.6
|
||||
unbuild: ^0.1.12
|
||||
v8-compile-cache: ^2.3.0
|
||||
bin:
|
||||
nu: ./bin/nuxt.js
|
||||
nuxt: ./bin/nuxt.js
|
||||
@ -10049,9 +10061,9 @@ __metadata:
|
||||
jest: ^26.6.3
|
||||
jiti: ^1.9.1
|
||||
lerna: ^4.0.0
|
||||
mkdist: ^0.1.5
|
||||
ts-jest: ^26.5.4
|
||||
typescript: ^4.2.4
|
||||
unbuild: ^0.1.12
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
@ -10079,7 +10091,7 @@ __metadata:
|
||||
nuxt-cli: ^0.1.1
|
||||
scule: ^0.1.1
|
||||
ufo: ^0.6.11
|
||||
unbuild: ^0.1.11
|
||||
unbuild: ^0.1.12
|
||||
upath: ^2.0.1
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
@ -13605,9 +13617,9 @@ typescript@^4.2.4:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"unbuild@npm:^0.1.11":
|
||||
version: 0.1.11
|
||||
resolution: "unbuild@npm:0.1.11"
|
||||
"unbuild@npm:^0.1.12":
|
||||
version: 0.1.12
|
||||
resolution: "unbuild@npm:0.1.12"
|
||||
dependencies:
|
||||
"@rollup/plugin-alias": ^3.1.2
|
||||
"@rollup/plugin-commonjs": ^18.0.0
|
||||
@ -13631,7 +13643,7 @@ typescript@^4.2.4:
|
||||
upath: ^2.0.1
|
||||
bin:
|
||||
unbuild: bin/unbuild.js
|
||||
checksum: a50be051d7a61a48844e90d9eb2dfdfaf8f7cc53dc9e3c4aa587374a47ee33de93fa5a6afc827afaa994fac9ae5c7dbea8cd39bd2f6f29d81a33d453e0767a67
|
||||
checksum: 4da242f54e5a778a20aad6a3e4151b41accdd0d2296216b9d5da8c347e79e372bca7894f15300db46da6ac49ff49216ef31cb97da5cddd30cee14eb7c7bf9c79
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@ -13852,7 +13864,7 @@ typescript@^4.2.4:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"v8-compile-cache@npm:^2.0.3":
|
||||
"v8-compile-cache@npm:^2.0.3, v8-compile-cache@npm:^2.3.0":
|
||||
version: 2.3.0
|
||||
resolution: "v8-compile-cache@npm:2.3.0"
|
||||
checksum: b56f83d9ff14187562badc4955dadeef53ff3abde478ce60759539dd8d5472a91fce9db6083fc2450e54cef6f2110c1a28d8c12162dbf575a6cfcb846986904b
|
||||
|
Loading…
Reference in New Issue
Block a user