Skip to content

Commit

Permalink
feat: initial version of nu cli (nuxt#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 9, 2021
1 parent b0a8ec6 commit a030c62
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 55 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"prepack": "unbuild"
},
"devDependencies": {
"unbuild": "^0.1.11"
"unbuild": "^0.1.12"
},
"dependencies": {
"@vueuse/head": "^0.5.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/bin/nuxt.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

process._startTime = Date.now()
require('../dist').main()
1 change: 1 addition & 0 deletions packages/cli/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BuildConfig } from 'unbuild'

export default <BuildConfig>{
declaration: false,
inlineDependencies: true,
entries: [
'src/index'
],
Expand Down
13 changes: 9 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 15 additions & 0 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const commands = {
dev: () => import('./dev'),
build: () => import('./build'),
usage: () => import('./usage')
}
15 changes: 15 additions & 0 deletions packages/cli/src/commands/usage.ts
Original file line number Diff line number Diff line change
@@ -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'
}
59 changes: 33 additions & 26 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -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]')
process.exit(1)
}
console.log(green(`Nuxt CLI v${version}`))

const isDev = cmd === 'dev'
const rootDir = resolve(process.cwd(), args[1] || '.')

const pkg = 'nuxt3'
const { loadNuxt, build } = require(pkg)
if (!(command in commands)) {
console.log('\n' + red('Invalid command ' + command))
command = 'usage'
}

const nuxt = await loadNuxt({
for: isDev ? 'dev' : 'build',
rootDir
})
if (command === 'usage') {
console.log(`\nUsage: ${cyan(`nu ${Object.keys(commands).join('|')} [args]`)}\n`)
process.exit(1)
}

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 changes: 16 additions & 0 deletions packages/cli/src/utils/help.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions packages/cli/src/utils/nuxt.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions packages/cli/src/utils/server.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepack": "unbuild"
},
"devDependencies": {
"unbuild": "^0.1.11"
"unbuild": "^0.1.12"
},
"dependencies": {
"consola": "^2.15.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 2 additions & 1 deletion packages/nitro/src/server/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>...')
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"prepack": "unbuild"
},
"devDependencies": {
"unbuild": "^0.1.11"
"unbuild": "^0.1.12"
},
"dependencies": {
"@nuxt/kit": "^0.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineNuxtConfig } from '@nuxt/kit'

export default defineNuxtConfig({
vite: false
vite: true
})
2 changes: 2 additions & 0 deletions scripts/nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../packages/cli/bin/nuxt')
Loading

0 comments on commit a030c62

Please sign in to comment.