feat: initial version of nu cli (#54)

This commit is contained in:
pooya parsa 2021-04-09 17:52:45 +02:00 committed by GitHub
parent b0a8ec6368
commit a030c62d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 196 additions and 55 deletions

View File

@ -26,8 +26,8 @@
"jest": "^26.6.3", "jest": "^26.6.3",
"jiti": "^1.9.1", "jiti": "^1.9.1",
"lerna": "^4.0.0", "lerna": "^4.0.0",
"mkdist": "^0.1.5",
"ts-jest": "^26.5.4", "ts-jest": "^26.5.4",
"typescript": "^4.2.4" "typescript": "^4.2.4",
"unbuild": "^0.1.12"
} }
} }

View File

@ -14,7 +14,7 @@
"prepack": "unbuild" "prepack": "unbuild"
}, },
"devDependencies": { "devDependencies": {
"unbuild": "^0.1.11" "unbuild": "^0.1.12"
}, },
"dependencies": { "dependencies": {
"@vueuse/head": "^0.5.1", "@vueuse/head": "^0.5.1",

View File

@ -1,3 +1,3 @@
#!/usr/bin/env node #!/usr/bin/env node
process._startTime = Date.now()
require('../dist').main() require('../dist').main()

View File

@ -2,6 +2,7 @@ import type { BuildConfig } from 'unbuild'
export default <BuildConfig>{ export default <BuildConfig>{
declaration: false, declaration: false,
inlineDependencies: true,
entries: [ entries: [
'src/index' 'src/index'
], ],

View File

@ -12,11 +12,16 @@
"scripts": { "scripts": {
"prepack": "unbuild" "prepack": "unbuild"
}, },
"devDependencies": {
"unbuild": "^0.1.11"
},
"files": [ "files": [
"bin", "bin",
"dist" "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"
}
} }

View 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'
}

View 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'
}

View File

@ -0,0 +1,5 @@
export const commands = {
dev: () => import('./dev'),
build: () => import('./build'),
usage: () => import('./usage')
}

View 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'
}

View File

@ -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 () { async function _main () {
const args = process.argv.splice(2) const _argv = process.argv.slice(2)
const cmd = args[0] const args = mri(_argv)
// @ts-ignore
let command = args._.shift() || 'usage'
if (!['dev', 'build'].includes(cmd)) { console.log(green(`Nuxt CLI v${version}`))
// eslint-disable-next-line no-console
console.error('Usage nuxt dev|build [rootDir]') 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) process.exit(1)
} }
const isDev = cmd === 'dev' try {
const rootDir = resolve(process.cwd(), args[1] || '.') const cmd = await commands[command]()
if (args.h || args.help) {
const pkg = 'nuxt3' showHelp(cmd.meta)
const { loadNuxt, build } = require(pkg) } else {
await cmd.invoke(args)
const nuxt = await loadNuxt({ }
for: isDev ? 'dev' : 'build', } catch (err) {
rootDir onFatalError(err)
})
if (isDev) {
// https://github.com/nuxt-contrib/listhen
await nuxt.server.listen(3000, { name: 'Nuxt' })
} }
}
await build(nuxt) function onFatalError (err) {
console.error(err)
process.exit(1)
} }
export function main () { export function main () {
_main() _main().catch(onFatalError)
.catch((error) => {
require('consola').fatal(error)
require('exit')(2)
})
} }

View 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')
}

View 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)
}

View 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
}
}

View File

@ -12,7 +12,7 @@
"prepack": "unbuild" "prepack": "unbuild"
}, },
"devDependencies": { "devDependencies": {
"unbuild": "^0.1.11" "unbuild": "^0.1.12"
}, },
"dependencies": { "dependencies": {
"consola": "^2.15.3", "consola": "^2.15.3",

View File

@ -73,6 +73,6 @@
"@types/http-proxy": "^1.17.5", "@types/http-proxy": "^1.17.5",
"@types/node-fetch": "^2.5.10", "@types/node-fetch": "^2.5.10",
"@types/serve-static": "^1.13.9", "@types/serve-static": "^1.13.9",
"unbuild": "^0.1.11" "unbuild": "^0.1.12"
} }
} }

View File

@ -74,7 +74,8 @@ export function createDevServer (nitroContext: NitroContext) {
// console.error('[proxy]', err) // console.error('[proxy]', err)
}) })
} else { } 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>...')
} }
}) })

View File

@ -34,6 +34,6 @@
"@types/fs-extra": "^9.0.10", "@types/fs-extra": "^9.0.10",
"@types/hash-sum": "^1.0.0", "@types/hash-sum": "^1.0.0",
"@types/lodash": "^4.14.168", "@types/lodash": "^4.14.168",
"unbuild": "^0.1.11" "unbuild": "^0.1.12"
} }
} }

View File

@ -11,7 +11,7 @@
"prepack": "unbuild" "prepack": "unbuild"
}, },
"devDependencies": { "devDependencies": {
"unbuild": "^0.1.11" "unbuild": "^0.1.12"
}, },
"dependencies": { "dependencies": {
"@nuxt/kit": "^0.2.0", "@nuxt/kit": "^0.2.0",

View File

@ -50,6 +50,6 @@
"@types/webpack-bundle-analyzer": "^3.9.2", "@types/webpack-bundle-analyzer": "^3.9.2",
"@types/webpack-dev-middleware": "^4.1.2", "@types/webpack-dev-middleware": "^4.1.2",
"@types/webpack-hot-middleware": "^2.25.4", "@types/webpack-hot-middleware": "^2.25.4",
"unbuild": "^0.1.11" "unbuild": "^0.1.12"
} }
} }

View File

@ -1,5 +1,5 @@
import { defineNuxtConfig } from '@nuxt/kit' import { defineNuxtConfig } from '@nuxt/kit'
export default defineNuxtConfig({ export default defineNuxtConfig({
vite: false vite: true
}) })

2
scripts/nu Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../packages/cli/bin/nuxt')

View File

@ -1594,7 +1594,7 @@ __metadata:
"@vueuse/head": ^0.5.1 "@vueuse/head": ^0.5.1
hookable: ^4.4.1 hookable: ^4.4.1
ohmyfetch: ^0.2.0 ohmyfetch: ^0.2.0
unbuild: ^0.1.11 unbuild: ^0.1.12
vue: ^3.0.11 vue: ^3.0.11
vue-router: ^4.0.6 vue-router: ^4.0.6
vuex5: ^0.5.0-testing.3 vuex5: ^0.5.0-testing.3
@ -1638,7 +1638,7 @@ __metadata:
scule: ^0.1.1 scule: ^0.1.1
std-env: ^2.3.0 std-env: ^2.3.0
ufo: ^0.6.11 ufo: ^0.6.11
unbuild: ^0.1.11 unbuild: ^0.1.12
unctx: ^0.0.3 unctx: ^0.0.3
untyped: ^0.2.5 untyped: ^0.2.5
upath: ^2.0.1 upath: ^2.0.1
@ -1705,7 +1705,7 @@ __metadata:
std-env: ^2.3.0 std-env: ^2.3.0
table: ^6.0.9 table: ^6.0.9
ufo: ^0.6.11 ufo: ^0.6.11
unbuild: ^0.1.11 unbuild: ^0.1.12
upath: ^2.0.1 upath: ^2.0.1
vue: 3.0.11 vue: 3.0.11
vue-bundle-renderer: ^0.2.3 vue-bundle-renderer: ^0.2.3
@ -1739,7 +1739,7 @@ __metadata:
"@vue/compiler-sfc": ^3.0.11 "@vue/compiler-sfc": ^3.0.11
consola: ^2.15.3 consola: ^2.15.3
fs-extra: ^9.1.0 fs-extra: ^9.1.0
unbuild: ^0.1.11 unbuild: ^0.1.12
vite: ^2.1.5 vite: ^2.1.5
vue: 3.0.11 vue: 3.0.11
languageName: unknown languageName: unknown
@ -1777,7 +1777,7 @@ __metadata:
style-resources-loader: ^1.4.1 style-resources-loader: ^1.4.1
time-fix-plugin: ^2.0.7 time-fix-plugin: ^2.0.7
ufo: ^0.6.11 ufo: ^0.6.11
unbuild: ^0.1.11 unbuild: ^0.1.12
url-loader: ^4.1.1 url-loader: ^4.1.1
vue: 3.0.11 vue: 3.0.11
vue-loader: ^16.2.0 vue-loader: ^16.2.0
@ -2346,6 +2346,13 @@ __metadata:
languageName: node languageName: node
linkType: hard 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": "@types/node-fetch@npm:^2.5.10":
version: 2.5.10 version: 2.5.10
resolution: "@types/node-fetch@npm:2.5.10" resolution: "@types/node-fetch@npm:2.5.10"
@ -9509,7 +9516,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"mkdist@npm:^0.1.5, mkdist@npm:^0.1.6": "mkdist@npm:^0.1.6":
version: 0.1.6 version: 0.1.6
resolution: "mkdist@npm:0.1.6" resolution: "mkdist@npm:0.1.6"
dependencies: dependencies:
@ -10029,7 +10036,12 @@ __metadata:
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "nuxt-cli@workspace:packages/cli" resolution: "nuxt-cli@workspace:packages/cli"
dependencies: 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: bin:
nu: ./bin/nuxt.js nu: ./bin/nuxt.js
nuxt: ./bin/nuxt.js nuxt: ./bin/nuxt.js
@ -10049,9 +10061,9 @@ __metadata:
jest: ^26.6.3 jest: ^26.6.3
jiti: ^1.9.1 jiti: ^1.9.1
lerna: ^4.0.0 lerna: ^4.0.0
mkdist: ^0.1.5
ts-jest: ^26.5.4 ts-jest: ^26.5.4
typescript: ^4.2.4 typescript: ^4.2.4
unbuild: ^0.1.12
languageName: unknown languageName: unknown
linkType: soft linkType: soft
@ -10079,7 +10091,7 @@ __metadata:
nuxt-cli: ^0.1.1 nuxt-cli: ^0.1.1
scule: ^0.1.1 scule: ^0.1.1
ufo: ^0.6.11 ufo: ^0.6.11
unbuild: ^0.1.11 unbuild: ^0.1.12
upath: ^2.0.1 upath: ^2.0.1
languageName: unknown languageName: unknown
linkType: soft linkType: soft
@ -13605,9 +13617,9 @@ typescript@^4.2.4:
languageName: node languageName: node
linkType: hard linkType: hard
"unbuild@npm:^0.1.11": "unbuild@npm:^0.1.12":
version: 0.1.11 version: 0.1.12
resolution: "unbuild@npm:0.1.11" resolution: "unbuild@npm:0.1.12"
dependencies: dependencies:
"@rollup/plugin-alias": ^3.1.2 "@rollup/plugin-alias": ^3.1.2
"@rollup/plugin-commonjs": ^18.0.0 "@rollup/plugin-commonjs": ^18.0.0
@ -13631,7 +13643,7 @@ typescript@^4.2.4:
upath: ^2.0.1 upath: ^2.0.1
bin: bin:
unbuild: bin/unbuild.js unbuild: bin/unbuild.js
checksum: a50be051d7a61a48844e90d9eb2dfdfaf8f7cc53dc9e3c4aa587374a47ee33de93fa5a6afc827afaa994fac9ae5c7dbea8cd39bd2f6f29d81a33d453e0767a67 checksum: 4da242f54e5a778a20aad6a3e4151b41accdd0d2296216b9d5da8c347e79e372bca7894f15300db46da6ac49ff49216ef31cb97da5cddd30cee14eb7c7bf9c79
languageName: node languageName: node
linkType: hard linkType: hard
@ -13852,7 +13864,7 @@ typescript@^4.2.4:
languageName: node languageName: node
linkType: hard 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 version: 2.3.0
resolution: "v8-compile-cache@npm:2.3.0" resolution: "v8-compile-cache@npm:2.3.0"
checksum: b56f83d9ff14187562badc4955dadeef53ff3abde478ce60759539dd8d5472a91fce9db6083fc2450e54cef6f2110c1a28d8c12162dbf575a6cfcb846986904b checksum: b56f83d9ff14187562badc4955dadeef53ff3abde478ce60759539dd8d5472a91fce9db6083fc2450e54cef6f2110c1a28d8c12162dbf575a6cfcb846986904b