From 72e77f0b59bc541eb16716645c90518b599dab2d Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Fri, 16 Sep 2022 16:52:32 -0400 Subject: [PATCH 1/4] feat: stub `build-module` --- packages/nuxi/src/commands/build-module.ts | 29 +++++++++++++ packages/nuxi/src/commands/index.ts | 1 + packages/nuxi/src/commands/info.ts | 37 +--------------- packages/nuxi/src/utils/packageManagers.ts | 50 +++++++++++++++++++++- 4 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 packages/nuxi/src/commands/build-module.ts diff --git a/packages/nuxi/src/commands/build-module.ts b/packages/nuxi/src/commands/build-module.ts new file mode 100644 index 00000000000..87d385792d6 --- /dev/null +++ b/packages/nuxi/src/commands/build-module.ts @@ -0,0 +1,29 @@ +import { execa } from 'execa' +import consola from 'consola' +import { resolve } from 'pathe' +import { getPkg } from '../utils/packageManagers' +import { defineNuxtCommand } from './index' + +// Stub `build-module` until we have proper support for it +// @see https://github.com/nuxt/module-builder/issues/32 +export default defineNuxtCommand({ + meta: { + name: 'build-module', + usage: 'npx nuxi build-module [--stub] [rootDir]', + description: 'Helper command for using `@nuxt/module-builder`' + }, + async invoke (args) { + // Execute `@nuxt/module-builder` locally if possible + const rootDir = resolve(args._[0] || '.') + const hasLocal = getPkg('@nuxt/module-builder', rootDir) + const execArgs = Object.entries({ + '--stub': args.stub + }).filter(([, value]) => value).map(([key]) => key) + + if (!hasLocal) { + return consola.error('Missing `@nuxt/module-builder` dependency, please install it first to use this command') + } + + await execa('nuxt-module-build', execArgs, { preferLocal: true, stdio: 'inherit', cwd: rootDir }) + } +}) diff --git a/packages/nuxi/src/commands/index.ts b/packages/nuxi/src/commands/index.ts index 4771e7c4da0..1efc57bced5 100644 --- a/packages/nuxi/src/commands/index.ts +++ b/packages/nuxi/src/commands/index.ts @@ -5,6 +5,7 @@ const _rDefault = (r: any) => r.default || r export const commands = { dev: () => import('./dev').then(_rDefault), build: () => import('./build').then(_rDefault), + 'build-module': () => import('./build-module').then(_rDefault), cleanup: () => import('./cleanup').then(_rDefault), clean: () => import('./cleanup').then(_rDefault), preview: () => import('./preview').then(_rDefault), diff --git a/packages/nuxi/src/commands/info.ts b/packages/nuxi/src/commands/info.ts index 8dac49d71b0..c307de85934 100644 --- a/packages/nuxi/src/commands/info.ts +++ b/packages/nuxi/src/commands/info.ts @@ -1,14 +1,10 @@ import os from 'node:os' -import { existsSync, readFileSync } from 'node:fs' -import { createRequire } from 'node:module' import { resolve } from 'pathe' import jiti from 'jiti' -import destr from 'destr' import { splitByCase } from 'scule' import clipboardy from 'clipboardy' import { NuxtModule } from '@nuxt/schema' -import { getPackageManager, getPackageManagerVersion } from '../utils/packageManagers' -import { findup } from '../utils/fs' +import { getPackageManager, getPackageManagerVersion, findPackage, getPkg } from '../utils/packageManagers' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ @@ -125,34 +121,3 @@ function getNuxtConfig (rootDir: string) { return {} } } - -function getPkg (name: string, rootDir: string) { - // Assume it is in {rootDir}/node_modules/${name}/package.json - let pkgPath = resolve(rootDir, 'node_modules', name, 'package.json') - - // Try to resolve for more accuracy - const _require = createRequire(rootDir) - try { pkgPath = _require.resolve(name + '/package.json') } catch (_err) { - // console.log('not found:', name) - } - - return readJSONSync(pkgPath) -} - -function findPackage (rootDir: string) { - return findup(rootDir, (dir) => { - const p = resolve(dir, 'package.json') - if (existsSync(p)) { - return readJSONSync(p) - } - }) || {} -} - -function readJSONSync (filePath: string) { - try { - return destr(readFileSync(filePath, 'utf-8')) - } catch (err) { - // TODO: Warn error - return null - } -} diff --git a/packages/nuxi/src/utils/packageManagers.ts b/packages/nuxi/src/utils/packageManagers.ts index ba43eaeb921..7afeae45625 100644 --- a/packages/nuxi/src/utils/packageManagers.ts +++ b/packages/nuxi/src/utils/packageManagers.ts @@ -1,5 +1,7 @@ import { execSync } from 'node:child_process' -import { existsSync } from 'node:fs' +import { existsSync, readFileSync } from 'node:fs' +import { createRequire } from 'node:module' +import destr from 'destr' import { resolve } from 'pathe' import { findup } from './fs' @@ -25,3 +27,49 @@ export function getPackageManager (rootDir: string) { export function getPackageManagerVersion (name: string) { return execSync(`${name} --version`).toString('utf8').trim() } + +/** + * Find package.json and read it + * + * @example + * ```ts + * findPackage('nuxt', '/path/to/project') + * ``` + * @param name - package name + * @param rootDir - root directory + */ +export function getPkg (name: string, rootDir: string) { + // Assume it is in {rootDir}/node_modules/${name}/package.json + let pkgPath = resolve(rootDir, 'node_modules', name, 'package.json') + + // Try to resolve for more accuracy + const _require = createRequire(rootDir) + try { pkgPath = _require.resolve(name + '/package.json') } catch (_err) { + // console.log('not found:', name) + } + + return readJSONSync(pkgPath) +} + +/** + * findPackage - Read package.json file + * + * @param rootDir - file path + */ +export function findPackage (rootDir: string) { + return findup(rootDir, (dir) => { + const p = resolve(dir, 'package.json') + if (existsSync(p)) { + return readJSONSync(p) + } + }) || {} +} + +export function readJSONSync (filePath: string) { + try { + return destr(readFileSync(filePath, 'utf-8')) + } catch (err) { + // TODO: Warn error + return null + } +} From cf1f073d96aca012cf9604004f1f581af2e61a57 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Fri, 16 Sep 2022 17:51:07 -0400 Subject: [PATCH 2/4] fix: tryResolve & undo refactor --- docs/content/3.api/5.commands/build-module.md | 12 +++++ packages/nuxi/src/commands/build-module.ts | 5 +- packages/nuxi/src/commands/info.ts | 37 +++++++++++++- packages/nuxi/src/utils/packageManagers.ts | 50 +------------------ 4 files changed, 52 insertions(+), 52 deletions(-) create mode 100644 docs/content/3.api/5.commands/build-module.md diff --git a/docs/content/3.api/5.commands/build-module.md b/docs/content/3.api/5.commands/build-module.md new file mode 100644 index 00000000000..20ae8d407a7 --- /dev/null +++ b/docs/content/3.api/5.commands/build-module.md @@ -0,0 +1,12 @@ +# `nuxi build-module` + +```{bash} +npx nuxi build-module [--stub] [rootDir] +``` + +The `build-module` command creates a `dist` directory within your `rootDir` that contains the full build for your **nuxt-module**. (**note**: This command is only available when you are using `@nuxt/module-builder` to build your module. Please see [this readme](https://github.com/nuxt/module-builder#-nuxt-module-builder) for more information.) + +Option | Default | Description +-------------------------|-----------------|------------------ +`rootDir` | `.` | The root directory of the module to bundle. +`--stub` | `false` | Stub out your module for development using [jiti](https://github.com/unjs/jiti#jiti). (**note:** This is mainly for development purposes.) diff --git a/packages/nuxi/src/commands/build-module.ts b/packages/nuxi/src/commands/build-module.ts index 87d385792d6..ef60e61d40d 100644 --- a/packages/nuxi/src/commands/build-module.ts +++ b/packages/nuxi/src/commands/build-module.ts @@ -1,7 +1,7 @@ import { execa } from 'execa' import consola from 'consola' import { resolve } from 'pathe' -import { getPkg } from '../utils/packageManagers' +import { tryResolveModule } from '../utils/cjs' import { defineNuxtCommand } from './index' // Stub `build-module` until we have proper support for it @@ -15,7 +15,8 @@ export default defineNuxtCommand({ async invoke (args) { // Execute `@nuxt/module-builder` locally if possible const rootDir = resolve(args._[0] || '.') - const hasLocal = getPkg('@nuxt/module-builder', rootDir) + const hasLocal = tryResolveModule('@nuxt/module-builder/package.json', rootDir) + const execArgs = Object.entries({ '--stub': args.stub }).filter(([, value]) => value).map(([key]) => key) diff --git a/packages/nuxi/src/commands/info.ts b/packages/nuxi/src/commands/info.ts index c307de85934..8dac49d71b0 100644 --- a/packages/nuxi/src/commands/info.ts +++ b/packages/nuxi/src/commands/info.ts @@ -1,10 +1,14 @@ import os from 'node:os' +import { existsSync, readFileSync } from 'node:fs' +import { createRequire } from 'node:module' import { resolve } from 'pathe' import jiti from 'jiti' +import destr from 'destr' import { splitByCase } from 'scule' import clipboardy from 'clipboardy' import { NuxtModule } from '@nuxt/schema' -import { getPackageManager, getPackageManagerVersion, findPackage, getPkg } from '../utils/packageManagers' +import { getPackageManager, getPackageManagerVersion } from '../utils/packageManagers' +import { findup } from '../utils/fs' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ @@ -121,3 +125,34 @@ function getNuxtConfig (rootDir: string) { return {} } } + +function getPkg (name: string, rootDir: string) { + // Assume it is in {rootDir}/node_modules/${name}/package.json + let pkgPath = resolve(rootDir, 'node_modules', name, 'package.json') + + // Try to resolve for more accuracy + const _require = createRequire(rootDir) + try { pkgPath = _require.resolve(name + '/package.json') } catch (_err) { + // console.log('not found:', name) + } + + return readJSONSync(pkgPath) +} + +function findPackage (rootDir: string) { + return findup(rootDir, (dir) => { + const p = resolve(dir, 'package.json') + if (existsSync(p)) { + return readJSONSync(p) + } + }) || {} +} + +function readJSONSync (filePath: string) { + try { + return destr(readFileSync(filePath, 'utf-8')) + } catch (err) { + // TODO: Warn error + return null + } +} diff --git a/packages/nuxi/src/utils/packageManagers.ts b/packages/nuxi/src/utils/packageManagers.ts index 7afeae45625..ba43eaeb921 100644 --- a/packages/nuxi/src/utils/packageManagers.ts +++ b/packages/nuxi/src/utils/packageManagers.ts @@ -1,7 +1,5 @@ import { execSync } from 'node:child_process' -import { existsSync, readFileSync } from 'node:fs' -import { createRequire } from 'node:module' -import destr from 'destr' +import { existsSync } from 'node:fs' import { resolve } from 'pathe' import { findup } from './fs' @@ -27,49 +25,3 @@ export function getPackageManager (rootDir: string) { export function getPackageManagerVersion (name: string) { return execSync(`${name} --version`).toString('utf8').trim() } - -/** - * Find package.json and read it - * - * @example - * ```ts - * findPackage('nuxt', '/path/to/project') - * ``` - * @param name - package name - * @param rootDir - root directory - */ -export function getPkg (name: string, rootDir: string) { - // Assume it is in {rootDir}/node_modules/${name}/package.json - let pkgPath = resolve(rootDir, 'node_modules', name, 'package.json') - - // Try to resolve for more accuracy - const _require = createRequire(rootDir) - try { pkgPath = _require.resolve(name + '/package.json') } catch (_err) { - // console.log('not found:', name) - } - - return readJSONSync(pkgPath) -} - -/** - * findPackage - Read package.json file - * - * @param rootDir - file path - */ -export function findPackage (rootDir: string) { - return findup(rootDir, (dir) => { - const p = resolve(dir, 'package.json') - if (existsSync(p)) { - return readJSONSync(p) - } - }) || {} -} - -export function readJSONSync (filePath: string) { - try { - return destr(readFileSync(filePath, 'utf-8')) - } catch (err) { - // TODO: Warn error - return null - } -} From 42cab6fff56a1837e757c5c0aa0ecaf8c71ca599 Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Mon, 19 Sep 2022 16:41:44 -0400 Subject: [PATCH 3/4] chore: update docs with alert. --- docs/content/3.api/5.commands/build-module.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/content/3.api/5.commands/build-module.md b/docs/content/3.api/5.commands/build-module.md index 20ae8d407a7..1445a52c49c 100644 --- a/docs/content/3.api/5.commands/build-module.md +++ b/docs/content/3.api/5.commands/build-module.md @@ -4,9 +4,13 @@ npx nuxi build-module [--stub] [rootDir] ``` -The `build-module` command creates a `dist` directory within your `rootDir` that contains the full build for your **nuxt-module**. (**note**: This command is only available when you are using `@nuxt/module-builder` to build your module. Please see [this readme](https://github.com/nuxt/module-builder#-nuxt-module-builder) for more information.) +The `build-module` command runs `@nuxt/module-builder` to generate `dist` directory within your `rootDir` that contains the full build for your **nuxt-module**. Option | Default | Description -------------------------|-----------------|------------------ `rootDir` | `.` | The root directory of the module to bundle. `--stub` | `false` | Stub out your module for development using [jiti](https://github.com/unjs/jiti#jiti). (**note:** This is mainly for development purposes.) + +::alert +This command is only available when you are using `@nuxt/module-builder` to build your module. Please see [this readme](https://github.com/nuxt/module-builder#-nuxt-module-builder) for more information. +:: From 4489c355e313988b67ce9eaab707a4f61f7aa1ab Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 16 Oct 2022 11:38:13 +0200 Subject: [PATCH 4/4] fallback to npx if not locally found --- packages/nuxi/src/commands/build-module.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/nuxi/src/commands/build-module.ts b/packages/nuxi/src/commands/build-module.ts index ef60e61d40d..43932c88015 100644 --- a/packages/nuxi/src/commands/build-module.ts +++ b/packages/nuxi/src/commands/build-module.ts @@ -4,27 +4,30 @@ import { resolve } from 'pathe' import { tryResolveModule } from '../utils/cjs' import { defineNuxtCommand } from './index' -// Stub `build-module` until we have proper support for it -// @see https://github.com/nuxt/module-builder/issues/32 +const MODULE_BUILDER_PKG = '@nuxt/module-builder' + export default defineNuxtCommand({ meta: { name: 'build-module', usage: 'npx nuxi build-module [--stub] [rootDir]', - description: 'Helper command for using `@nuxt/module-builder`' + description: `Helper command for using ${MODULE_BUILDER_PKG}` }, async invoke (args) { - // Execute `@nuxt/module-builder` locally if possible + // Find local installed version const rootDir = resolve(args._[0] || '.') - const hasLocal = tryResolveModule('@nuxt/module-builder/package.json', rootDir) + const hasLocal = tryResolveModule(`${MODULE_BUILDER_PKG}/package.json`, rootDir) const execArgs = Object.entries({ '--stub': args.stub }).filter(([, value]) => value).map(([key]) => key) + let cmd = 'nuxt-module-build' if (!hasLocal) { - return consola.error('Missing `@nuxt/module-builder` dependency, please install it first to use this command') + consola.warn(`Cannot find locally installed version of \`${MODULE_BUILDER_PKG}\` (>=0.2.0). Falling back to \`npx ${MODULE_BUILDER_PKG}\``) + cmd = 'npx' + execArgs.unshift(MODULE_BUILDER_PKG) } - await execa('nuxt-module-build', execArgs, { preferLocal: true, stdio: 'inherit', cwd: rootDir }) + await execa(cmd, execArgs, { preferLocal: true, stdio: 'inherit', cwd: rootDir }) } })