mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Add correct sync version of error handling with existSync. (#769)
* Add correct sync version of error handling with existSync. * Update utils.js
This commit is contained in:
parent
cebed46014
commit
fae2305f46
|
@ -1,9 +1,10 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import { resolve, join } from 'path'
|
import { resolve, join } from 'path'
|
||||||
import { exists } from 'mz/fs'
|
import { existsSync } from 'fs'
|
||||||
import parseArgs from 'minimist'
|
import parseArgs from 'minimist'
|
||||||
import build from '../server/build'
|
import build from '../server/build'
|
||||||
|
import { printAndExit } from '../lib/utils'
|
||||||
|
|
||||||
const argv = parseArgs(process.argv.slice(2), {
|
const argv = parseArgs(process.argv.slice(2), {
|
||||||
alias: {
|
alias: {
|
||||||
|
@ -29,21 +30,17 @@ if (argv.help) {
|
||||||
const dir = resolve(argv._[0] || '.')
|
const dir = resolve(argv._[0] || '.')
|
||||||
|
|
||||||
// Check if pages dir exists and warn if not
|
// Check if pages dir exists and warn if not
|
||||||
exists(dir)
|
if (!existsSync(dir)) {
|
||||||
.then(async () => {
|
printAndExit(`> No such directory exists as the project root: ${dir}`)
|
||||||
if (!(await exists(join(dir, 'pages')))) {
|
}
|
||||||
if (await exists(join(dir, '..', 'pages'))) {
|
|
||||||
console.error('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
|
if (!existsSync(join(dir, 'pages'))) {
|
||||||
} else {
|
if (existsSync(join(dir, '..', 'pages'))) {
|
||||||
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
|
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
|
||||||
}
|
|
||||||
process.exit(1)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch((err) => {
|
printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
|
||||||
console.error(err)
|
}
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
build(dir)
|
build(dir)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
|
27
bin/next-dev
27
bin/next-dev
|
@ -2,8 +2,9 @@
|
||||||
import 'source-map-support/register'
|
import 'source-map-support/register'
|
||||||
import { resolve, join } from 'path'
|
import { resolve, join } from 'path'
|
||||||
import parseArgs from 'minimist'
|
import parseArgs from 'minimist'
|
||||||
import { exists } from 'mz/fs'
|
import { existsSync } from 'fs'
|
||||||
import Server from '../server'
|
import Server from '../server'
|
||||||
|
import { printAndExit } from '../lib/utils'
|
||||||
|
|
||||||
const argv = parseArgs(process.argv.slice(2), {
|
const argv = parseArgs(process.argv.slice(2), {
|
||||||
alias: {
|
alias: {
|
||||||
|
@ -38,21 +39,17 @@ if (argv.help) {
|
||||||
const dir = resolve(argv._[0] || '.')
|
const dir = resolve(argv._[0] || '.')
|
||||||
|
|
||||||
// Check if pages dir exists and warn if not
|
// Check if pages dir exists and warn if not
|
||||||
exists(dir)
|
if (!existsSync(dir)) {
|
||||||
.then(async () => {
|
printAndExit(`> No such directory exists as the project root: ${dir}`)
|
||||||
if (!(await exists(join(dir, 'pages')))) {
|
}
|
||||||
if (await exists(join(dir, '..', 'pages'))) {
|
|
||||||
console.error('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
|
if (!existsSync(join(dir, 'pages'))) {
|
||||||
} else {
|
if (existsSync(join(dir, '..', 'pages'))) {
|
||||||
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
|
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
|
||||||
}
|
|
||||||
process.exit(1)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch((err) => {
|
printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
|
||||||
console.error(err)
|
}
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
const srv = new Server({ dir, dev: true })
|
const srv = new Server({ dir, dev: true })
|
||||||
srv.start(argv.port)
|
srv.start(argv.port)
|
||||||
|
|
10
lib/utils.js
10
lib/utils.js
|
@ -21,3 +21,13 @@ export function deprecated (fn, message) {
|
||||||
|
|
||||||
return newFn
|
return newFn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function printAndExit (message, code = 1) {
|
||||||
|
if (code === 0) {
|
||||||
|
console.log(message)
|
||||||
|
} else {
|
||||||
|
console.error(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
process.exit(code)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue