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(cli): add
prepare
command to stub module types (nuxt#370)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
- Loading branch information
Showing
7 changed files
with
113 additions
and
17 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
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))) | ||
} | ||
}) |
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,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 | ||
} |
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,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 | ||
} | ||
} |
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,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) |
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,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" /> |