From a030c62d29ba871f94a7152c7d5fa36d4de1d3b6 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Fri, 9 Apr 2021 17:52:45 +0200 Subject: [PATCH] feat: initial version of nu cli (#54) --- package.json | 4 +- packages/app/package.json | 2 +- packages/cli/bin/nuxt.js | 2 +- packages/cli/build.config.ts | 1 + packages/cli/package.json | 13 +++++-- packages/cli/src/commands/build.ts | 15 ++++++++ packages/cli/src/commands/dev.ts | 23 ++++++++++++ packages/cli/src/commands/index.ts | 5 +++ packages/cli/src/commands/usage.ts | 15 ++++++++ packages/cli/src/index.ts | 59 +++++++++++++++++------------- packages/cli/src/utils/help.ts | 16 ++++++++ packages/cli/src/utils/nuxt.ts | 13 +++++++ packages/cli/src/utils/server.ts | 26 +++++++++++++ packages/kit/package.json | 2 +- packages/nitro/package.json | 2 +- packages/nitro/src/server/dev.ts | 3 +- packages/nuxt3/package.json | 2 +- packages/vite/package.json | 2 +- packages/webpack/package.json | 2 +- playground/nuxt.config.ts | 2 +- scripts/nu | 2 + yarn.lock | 40 +++++++++++++------- 22 files changed, 196 insertions(+), 55 deletions(-) create mode 100644 packages/cli/src/commands/build.ts create mode 100644 packages/cli/src/commands/dev.ts create mode 100644 packages/cli/src/commands/index.ts create mode 100644 packages/cli/src/commands/usage.ts create mode 100644 packages/cli/src/utils/help.ts create mode 100644 packages/cli/src/utils/nuxt.ts create mode 100644 packages/cli/src/utils/server.ts create mode 100755 scripts/nu diff --git a/package.json b/package.json index e23c8e2d27..aeb0a04e84 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/packages/app/package.json b/packages/app/package.json index 3b81f53e02..1033e7048b 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -14,7 +14,7 @@ "prepack": "unbuild" }, "devDependencies": { - "unbuild": "^0.1.11" + "unbuild": "^0.1.12" }, "dependencies": { "@vueuse/head": "^0.5.1", diff --git a/packages/cli/bin/nuxt.js b/packages/cli/bin/nuxt.js index c28a2caa4a..73d8c64931 100755 --- a/packages/cli/bin/nuxt.js +++ b/packages/cli/bin/nuxt.js @@ -1,3 +1,3 @@ #!/usr/bin/env node - +process._startTime = Date.now() require('../dist').main() diff --git a/packages/cli/build.config.ts b/packages/cli/build.config.ts index 76d302985e..0a945810ee 100644 --- a/packages/cli/build.config.ts +++ b/packages/cli/build.config.ts @@ -2,6 +2,7 @@ import type { BuildConfig } from 'unbuild' export default { declaration: false, + inlineDependencies: true, entries: [ 'src/index' ], diff --git a/packages/cli/package.json b/packages/cli/package.json index 9cf28050ce..261c9ae4e4 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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" + } } diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts new file mode 100644 index 0000000000..10c7cf54f0 --- /dev/null +++ b/packages/cli/src/commands/build.ts @@ -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' +} diff --git a/packages/cli/src/commands/dev.ts b/packages/cli/src/commands/dev.ts new file mode 100644 index 0000000000..3ef88898a9 --- /dev/null +++ b/packages/cli/src/commands/dev.ts @@ -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' +} diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts new file mode 100644 index 0000000000..6583694c55 --- /dev/null +++ b/packages/cli/src/commands/index.ts @@ -0,0 +1,5 @@ +export const commands = { + dev: () => import('./dev'), + build: () => import('./build'), + usage: () => import('./usage') +} diff --git a/packages/cli/src/commands/usage.ts b/packages/cli/src/commands/usage.ts new file mode 100644 index 0000000000..0efcaa5940 --- /dev/null +++ b/packages/cli/src/commands/usage.ts @@ -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' +} diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index d02a2c9eb8..c04d1af44d 100755 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -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) } diff --git a/packages/cli/src/utils/help.ts b/packages/cli/src/utils/help.ts new file mode 100644 index 0000000000..7d89f50e09 --- /dev/null +++ b/packages/cli/src/utils/help.ts @@ -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') +} diff --git a/packages/cli/src/utils/nuxt.ts b/packages/cli/src/utils/nuxt.ts new file mode 100644 index 0000000000..1da22da115 --- /dev/null +++ b/packages/cli/src/utils/nuxt.ts @@ -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) +} diff --git a/packages/cli/src/utils/server.ts b/packages/cli/src/utils/server.ts new file mode 100644 index 0000000000..2e23acb640 --- /dev/null +++ b/packages/cli/src/utils/server.ts @@ -0,0 +1,26 @@ +import type { RequestListener } from 'http' + +export function createServer () { + const listener = createDynamicFunction ((_req, res) => { + res.setHeader('Content-Type', 'text/html; charset=UTF-8') + res.end('...') + }) + + async function listen (opts) { + const { listen } = await import('listhen') + return listen(listener.call, opts) + } + + return { + setApp: (app: RequestListener) => listener.set(app), + listen + } +} + +function createDynamicFunction any>(initialValue: T) { + let fn: T = initialValue + return { + set: (newFn: T) => { fn = newFn }, + call: ((...args) => fn(...args)) as T + } +} diff --git a/packages/kit/package.json b/packages/kit/package.json index ce585a2236..a73fa1f1d6 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -12,7 +12,7 @@ "prepack": "unbuild" }, "devDependencies": { - "unbuild": "^0.1.11" + "unbuild": "^0.1.12" }, "dependencies": { "consola": "^2.15.3", diff --git a/packages/nitro/package.json b/packages/nitro/package.json index c7c2eab970..c79f950f91 100644 --- a/packages/nitro/package.json +++ b/packages/nitro/package.json @@ -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" } } diff --git a/packages/nitro/src/server/dev.ts b/packages/nitro/src/server/dev.ts index 3f7a8aeb19..b3412964eb 100644 --- a/packages/nitro/src/server/dev.ts +++ b/packages/nitro/src/server/dev.ts @@ -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('...') } }) diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index 12ae7f76ed..9c058d4f53 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -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" } } diff --git a/packages/vite/package.json b/packages/vite/package.json index 56ddff64e7..6c5744e8f2 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -11,7 +11,7 @@ "prepack": "unbuild" }, "devDependencies": { - "unbuild": "^0.1.11" + "unbuild": "^0.1.12" }, "dependencies": { "@nuxt/kit": "^0.2.0", diff --git a/packages/webpack/package.json b/packages/webpack/package.json index cdf7c35133..c5c24483ce 100644 --- a/packages/webpack/package.json +++ b/packages/webpack/package.json @@ -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" } } diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 15046bd316..70d274d93d 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -1,5 +1,5 @@ import { defineNuxtConfig } from '@nuxt/kit' export default defineNuxtConfig({ - vite: false + vite: true }) diff --git a/scripts/nu b/scripts/nu new file mode 100755 index 0000000000..40017b5bfb --- /dev/null +++ b/scripts/nu @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('../packages/cli/bin/nuxt') diff --git a/yarn.lock b/yarn.lock index 93a0c1f8bd..4076cc27d8 100644 --- a/yarn.lock +++ b/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