Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxi): nuxi build-module #7610

Merged
merged 17 commits into from Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions 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)
cpreston321 marked this conversation as resolved.
Show resolved Hide resolved
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 })
}
})
1 change: 1 addition & 0 deletions packages/nuxi/src/commands/index.ts
Expand Up @@ -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),
Expand Down
37 changes: 1 addition & 36 deletions 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({
Expand Down Expand Up @@ -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
}
}
50 changes: 49 additions & 1 deletion 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'

Expand All @@ -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
}
}