Skip to content

Commit

Permalink
chore: lint and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 19, 2023
1 parent 9a6fa0e commit d918d5e
Show file tree
Hide file tree
Showing 33 changed files with 583 additions and 359 deletions.
59 changes: 0 additions & 59 deletions package2.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/cli-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
process._startTime = Date.now()

// @ts-expect-error `default` property is not declared
import('./cli').then(r => (r.default || r).main())
import('./cli').then((r) => (r.default || r).main())
10 changes: 7 additions & 3 deletions src/cli-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (process.argv[2] === 'dev') {
import(cliEntry.href)
}

function startSubprocess () {
function startSubprocess() {
let childProc: ChildProcess | undefined

const onShutdown = () => {
Expand All @@ -32,9 +32,13 @@ function startSubprocess () {

start()

function start () {
function start() {
childProc = fork(fileURLToPath(cliEntry))
childProc.on('close', (code) => { if (code) { process.exit(code) } })
childProc.on('close', (code) => {
if (code) {
process.exit(code)
}
})
childProc.on('message', (message) => {
if ((message as { type: string })?.type === 'nuxt:restart') {
childProc?.kill()
Expand Down
20 changes: 14 additions & 6 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
meta: {
name: 'add',
usage: `npx nuxi add [--cwd] [--force] ${Object.keys(templates).join('|')} <name>`,
description: 'Create a new template file.'
usage: `npx nuxi add [--cwd] [--force] ${Object.keys(templates).join(
'|'
)} <name>`,
description: 'Create a new template file.',
},
async invoke (args) {
async invoke(args) {
const cwd = resolve(args.cwd || '.')

const template = args._[0]
const name = args._[1]

// Validate template name
if (!templates[template]) {
consola.error(`Template ${template} is not supported. Possible values: ${Object.keys(templates).join(', ')}`)
consola.error(
`Template ${template} is not supported. Possible values: ${Object.keys(
templates
).join(', ')}`
)
process.exit(1)
}

Expand All @@ -41,7 +47,9 @@ export default defineNuxtCommand({

// Ensure not overriding user code
if (!args.force && existsSync(path)) {
consola.error(`File exists: ${path} . Use --force to override or use a different name.`)
consola.error(
`File exists: ${path} . Use --force to override or use a different name.`
)
process.exit(1)
}

Expand All @@ -58,5 +66,5 @@ export default defineNuxtCommand({
// Write file
await fsp.writeFile(path, res.contents.trim() + '\n')
consola.info(`🪄 Generated a new ${template} in ${path}`)
}
},
})
47 changes: 30 additions & 17 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default defineNuxtCommand({
meta: {
name: 'analyze',
usage: 'npx nuxi analyze [--log-level] [--name] [--no-serve] [rootDir]',
description: 'Build nuxt and analyze production bundle (experimental)'
description: 'Build nuxt and analyze production bundle (experimental)',
},
async invoke (args, options = {}) {
async invoke(args, options = {}) {
overrideEnv('production')

const name = args.name || 'default'
Expand All @@ -34,17 +34,17 @@ export default defineNuxtCommand({
rootDir,
overrides: defu(options.overrides, {
build: {
analyze: true
analyze: true,
},
analyzeDir,
buildDir,
nitro: {
output: {
dir: outDir
}
dir: outDir,
},
},
logLevel: args['log-level']
})
logLevel: args['log-level'],
}),
})

analyzeDir = nuxt.options.analyzeDir
Expand All @@ -63,28 +63,39 @@ export default defineNuxtCommand({
endTime,
analyzeDir,
buildDir,
outDir
outDir,
}

await nuxt.callHook('build:analyze:done', meta)
await fsp.writeFile(join(analyzeDir, 'meta.json'), JSON.stringify(meta, null, 2), 'utf-8')
await fsp.writeFile(
join(analyzeDir, 'meta.json'),
JSON.stringify(meta, null, 2),
'utf-8'
)

console.info('Analyze results are available at: `' + analyzeDir + '`')
console.warn('Do not deploy analyze results! Use `nuxi build` before deploying.')
console.warn(
'Do not deploy analyze results! Use `nuxi build` before deploying.'
)

if (args.serve !== false && !process.env.CI) {
const app = createApp()

const serveFile = (filePath: string) => lazyEventHandler(async () => {
const contents = await fsp.readFile(filePath, 'utf-8')
return eventHandler((event) => { event.node.res.end(contents) })
})
const serveFile = (filePath: string) =>
lazyEventHandler(async () => {
const contents = await fsp.readFile(filePath, 'utf-8')
return eventHandler((event) => {
event.node.res.end(contents)
})
})

console.info('Starting stats server...')

app.use('/client', serveFile(join(analyzeDir, 'client.html')))
app.use('/nitro', serveFile(join(analyzeDir, 'nitro.html')))
app.use(eventHandler(() => `<!DOCTYPE html>
app.use(
eventHandler(
() => `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
Expand All @@ -100,11 +111,13 @@ export default defineNuxtCommand({
</li>
</ul>
</html>
`))
`
)
)

await listen(toNodeListener(app))

return 'wait' as const
}
}
},
})
27 changes: 19 additions & 8 deletions src/commands/build-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,36 @@ export default defineNuxtCommand({
meta: {
name: 'build-module',
usage: 'npx nuxi build-module [--stub] [rootDir]',
description: `Helper command for using ${MODULE_BUILDER_PKG}`
description: `Helper command for using ${MODULE_BUILDER_PKG}`,
},
async invoke (args) {
async invoke(args) {
// Find local installed version
const rootDir = resolve(args._[0] || '.')
const hasLocal = await tryResolveModule(`${MODULE_BUILDER_PKG}/package.json`, rootDir)
const hasLocal = await tryResolveModule(
`${MODULE_BUILDER_PKG}/package.json`,
rootDir
)

const execArgs = Object.entries({
'--stub': args.stub,
'--prepare': args.prepare
}).filter(([, value]) => value).map(([key]) => key)
'--prepare': args.prepare,
})
.filter(([, value]) => value)
.map(([key]) => key)

let cmd = 'nuxt-module-build'
if (!hasLocal) {
consola.warn(`Cannot find locally installed version of \`${MODULE_BUILDER_PKG}\` (>=0.2.0). Falling back to \`npx ${MODULE_BUILDER_PKG}\``)
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(cmd, execArgs, { preferLocal: true, stdio: 'inherit', cwd: rootDir })
}
await execa(cmd, execArgs, {
preferLocal: true,
stdio: 'inherit',
cwd: rootDir,
})
},
})
20 changes: 12 additions & 8 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default defineNuxtCommand({
meta: {
name: 'build',
usage: 'npx nuxi build [--prerender] [--dotenv] [--log-level] [rootDir]',
description: 'Build nuxt for production deployment'
description: 'Build nuxt for production deployment',
},
async invoke (args, options = {}) {
async invoke(args, options = {}) {
overrideEnv('production')

const rootDir = resolve(args._[0] || '.')
Expand All @@ -25,13 +25,13 @@ export default defineNuxtCommand({
rootDir,
dotenv: {
cwd: rootDir,
fileName: args.dotenv
fileName: args.dotenv,
},
overrides: {
logLevel: args['log-level'],
_generate: args.prerender,
...(options?.overrides || {})
}
...(options?.overrides || {}),
},
})

// Use ? for backward compatibility for Nuxt <= RC.10
Expand All @@ -50,12 +50,16 @@ export default defineNuxtCommand({

if (args.prerender) {
if (!nuxt.options.ssr) {
consola.warn('HTML content not prerendered because `ssr: false` was set. You can read more in `https://nuxt.com/docs/getting-started/deployment#static-hosting`.')
consola.warn(
'HTML content not prerendered because `ssr: false` was set. You can read more in `https://nuxt.com/docs/getting-started/deployment#static-hosting`.'
)
}
// TODO: revisit later if/when nuxt build --prerender will output hybrid
const dir = nitro?.options.output.publicDir
const publicDir = dir ? relative(process.cwd(), dir) : '.output/public'
consola.success(`You can now deploy \`${publicDir}\` to any static hosting!`)
consola.success(
`You can now deploy \`${publicDir}\` to any static hosting!`
)
}
}
},
})
6 changes: 3 additions & 3 deletions src/commands/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default defineNuxtCommand({
meta: {
name: 'cleanup',
usage: 'npx nuxi clean|cleanup',
description: 'Cleanup generated nuxt files and caches'
description: 'Cleanup generated nuxt files and caches',
},
async invoke (args) {
async invoke(args) {
const rootDir = resolve(args._[0] || '.')
await cleanupNuxtDirs(rootDir)
}
},
})
Loading

0 comments on commit d918d5e

Please sign in to comment.