Skip to content

Commit

Permalink
feat(cli): add prepare command to stub module types (nuxt#370)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
  • Loading branch information
danielroe and pi0 authored Jul 26, 2021
1 parent 8c09d05 commit 57a2974
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Argv } from 'mri'
export const commands = {
dev: () => import('./dev'),
build: () => import('./build'),
prepare: () => import('./prepare'),
usage: () => import('./usage')
}

Expand Down
57 changes: 57 additions & 0 deletions packages/cli/src/commands/prepare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { promises as fsp } from 'fs'
import { relative, resolve } from 'upath'
import { cyan } from 'colorette'

import { requireModule, getModulePaths, getNearestPackage } from '../utils/cjs'
import { exists } from '../utils/fs'
import { success } from '../utils/log'
import { defineNuxtCommand } from './index'

export default defineNuxtCommand({
meta: {
name: 'prepare',
usage: 'nu prepare',
description: 'Prepare nuxt for development/build'
},
async invoke (args) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.')

const { loadNuxt } = requireModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
const nuxt = await loadNuxt({ rootDir })

const adHocModules = nuxt.options._majorVersion === 3
? ['@nuxt/kit', '@nuxt/app', '@nuxt/nitro']
: ['@nuxt/kit']

const types = [
...adHocModules,
// Modules
...nuxt.options.buildModules,
...nuxt.options.modules,
...nuxt.options._modules
].filter(f => typeof f === 'string')

const modulePaths = getModulePaths(nuxt.options.modulesDir)
const _references = await Promise.all(types.map(async (id) => {
const pkg = getNearestPackage(id, modulePaths)
return pkg ? `/// <reference types="${pkg.name}" />` : await exists(id) && `/// <reference path="${id}" />`
})).then(arr => arr.filter(Boolean))

const references = Array.from(new Set(_references)) as string[]
await nuxt.callHook('prepare:types', { references })

const declarationPath = resolve(`${rootDir}/nuxt.d.ts`)

const declaration = [
'// Declarations auto generated by `nuxt prepare`. Please do not manually modify this file.',
'',
...references,
''
].join('\n')

await fsp.writeFile(declarationPath, declaration)

success('Generated', cyan(relative(process.cwd(), declarationPath)))
}
})
42 changes: 29 additions & 13 deletions packages/cli/src/utils/cjs.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { normalize } from 'upath'
import { normalize, dirname } from 'upath'

export function resolveModule (id: string, paths?: string) {
return normalize(require.resolve(id, {
paths: [].concat(
// @ts-ignore
global.__NUXT_PREPATHS__,
paths,
process.cwd(),
// @ts-ignore
global.__NUXT_PATHS__
).filter(Boolean)
}))
export function getModulePaths (paths?: string | string[]): string[] {
return [].concat(
// @ts-ignore
global.__NUXT_PREPATHS__,
...(Array.isArray(paths) ? paths : [paths]),
process.cwd(),
// @ts-ignore
global.__NUXT_PATHS__
).filter(Boolean)
}

export function requireModule (id: string, paths?: string) {
export function resolveModule (id: string, paths?: string | string[]) {
return normalize(require.resolve(id, { paths: getModulePaths(paths) }))
}

export function tryResolveModule (id: string, paths?: string | string[]) {
try {
return resolveModule(id, paths)
} catch { return null }
}

export function requireModule (id: string, paths?: string | string[]) {
return require(resolveModule(id, paths))
}

export function getNearestPackage (id: string, paths?: string | string[]) {
while (dirname(id) !== id) {
try { return requireModule(id + '/package.json', paths) } catch { }
id = dirname(id)
}
return null
}
11 changes: 11 additions & 0 deletions packages/cli/src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { promises as fsp } from 'fs'

// Check if a file exists
export async function exists (path: string) {
try {
await fsp.access(path)
return true
} catch {
return false
}
}
9 changes: 5 additions & 4 deletions packages/cli/src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { red, yellow, cyan } from 'colorette'
import { green, red, yellow, cyan } from 'colorette'

export const error = (...args) => console.error(red('[error]'), ...args)
export const warn = (...args) => console.warn(yellow('[warn]'), ...args)
export const info = (...args) => console.info(cyan('[info]'), ...args)
export const error = (...args: any[]) => console.error(red('[error]'), ...args)
export const warn = (...args: any[]) => console.warn(yellow('[warn]'), ...args)
export const info = (...args: any[]) => console.info(cyan('[info]'), ...args)
export const success = (...args: any[]) => console.log(green('[success]'), ...args)
3 changes: 3 additions & 0 deletions packages/kit/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export interface NuxtHooks {
'config': (options: NuxtConfig) => HookResult
'run:before': (options: { argv: string[], cmd: { name: string, usage: string, description: string, options: Record<string, any> }, rootDir: string }) => HookResult

// nuxt-cli
'prepare:types': (options: { references: string[] }) => HookResult

// @nuxt/core
'ready': (nuxt: Nuxt) => HookResult
'close': (nuxt: Nuxt) => HookResult
Expand Down
7 changes: 7 additions & 0 deletions playground/nuxt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Declarations auto generated by `nuxt prepare`. Please do not manually modify this file.

/// <reference types="@nuxt/kit" />
/// <reference types="@nuxt/nitro" />
/// <reference types="@nuxt/pages" />
/// <reference types="@nuxt/meta" />
/// <reference types="@nuxt/component-discovery" />

0 comments on commit 57a2974

Please sign in to comment.