Skip to content

Commit

Permalink
Add correct sync version of error handling with existSync. (#769)
Browse files Browse the repository at this point in the history
* Add correct sync version of error handling with existSync.

* Update utils.js
  • Loading branch information
arunoda authored and nkzawa committed Jan 15, 2017
1 parent cebed46 commit fae2305
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
27 changes: 12 additions & 15 deletions bin/next-build
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node

import { resolve, join } from 'path'
import { exists } from 'mz/fs'
import { existsSync } from 'fs'
import parseArgs from 'minimist'
import build from '../server/build'
import { printAndExit } from '../lib/utils'

const argv = parseArgs(process.argv.slice(2), {
alias: {
Expand All @@ -29,21 +30,17 @@ if (argv.help) {
const dir = resolve(argv._[0] || '.')

// Check if pages dir exists and warn if not
exists(dir)
.then(async () => {
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?')
} else {
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
}
process.exit(1)
if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}

if (!existsSync(join(dir, 'pages'))) {
if (existsSync(join(dir, '..', 'pages'))) {
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
}
})
.catch((err) => {
console.error(err)
process.exit(1)
})

printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
}

build(dir)
.catch((err) => {
Expand Down
27 changes: 12 additions & 15 deletions bin/next-dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import 'source-map-support/register'
import { resolve, join } from 'path'
import parseArgs from 'minimist'
import { exists } from 'mz/fs'
import { existsSync } from 'fs'
import Server from '../server'
import { printAndExit } from '../lib/utils'

const argv = parseArgs(process.argv.slice(2), {
alias: {
Expand Down Expand Up @@ -38,21 +39,17 @@ if (argv.help) {
const dir = resolve(argv._[0] || '.')

// Check if pages dir exists and warn if not
exists(dir)
.then(async () => {
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?')
} else {
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
}
process.exit(1)
if (!existsSync(dir)) {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}

if (!existsSync(join(dir, 'pages'))) {
if (existsSync(join(dir, '..', 'pages'))) {
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
}
})
.catch((err) => {
console.error(err)
process.exit(1)
})

printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
}

const srv = new Server({ dir, dev: true })
srv.start(argv.port)
Expand Down
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ export function deprecated (fn, message) {

return newFn
}

export function printAndExit (message, code = 1) {
if (code === 0) {
console.log(message)
} else {
console.error(message)
}

process.exit(code)
}

0 comments on commit fae2305

Please sign in to comment.