forked from nuxt/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial version of nu cli (nuxt#54)
- Loading branch information
Showing
22 changed files
with
196 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('../packages/cli/bin/nuxt') |
Oops, something went wrong.