Skip to content

Commit fae2305

Browse files
arunodankzawa
authored andcommitted
Add correct sync version of error handling with existSync. (#769)
* Add correct sync version of error handling with existSync. * Update utils.js
1 parent cebed46 commit fae2305

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

bin/next-build

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env node
22

33
import { resolve, join } from 'path'
4-
import { exists } from 'mz/fs'
4+
import { existsSync } from 'fs'
55
import parseArgs from 'minimist'
66
import build from '../server/build'
7+
import { printAndExit } from '../lib/utils'
78

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

3132
// Check if pages dir exists and warn if not
32-
exists(dir)
33-
.then(async () => {
34-
if (!(await exists(join(dir, 'pages')))) {
35-
if (await exists(join(dir, '..', 'pages'))) {
36-
console.error('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
37-
} else {
38-
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
39-
}
40-
process.exit(1)
33+
if (!existsSync(dir)) {
34+
printAndExit(`> No such directory exists as the project root: ${dir}`)
35+
}
36+
37+
if (!existsSync(join(dir, 'pages'))) {
38+
if (existsSync(join(dir, '..', 'pages'))) {
39+
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
4140
}
42-
})
43-
.catch((err) => {
44-
console.error(err)
45-
process.exit(1)
46-
})
41+
42+
printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
43+
}
4744

4845
build(dir)
4946
.catch((err) => {

bin/next-dev

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import 'source-map-support/register'
33
import { resolve, join } from 'path'
44
import parseArgs from 'minimist'
5-
import { exists } from 'mz/fs'
5+
import { existsSync } from 'fs'
66
import Server from '../server'
7+
import { printAndExit } from '../lib/utils'
78

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

4041
// Check if pages dir exists and warn if not
41-
exists(dir)
42-
.then(async () => {
43-
if (!(await exists(join(dir, 'pages')))) {
44-
if (await exists(join(dir, '..', 'pages'))) {
45-
console.error('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
46-
} else {
47-
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root')
48-
}
49-
process.exit(1)
42+
if (!existsSync(dir)) {
43+
printAndExit(`> No such directory exists as the project root: ${dir}`)
44+
}
45+
46+
if (!existsSync(join(dir, 'pages'))) {
47+
if (existsSync(join(dir, '..', 'pages'))) {
48+
printAndExit('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?')
5049
}
51-
})
52-
.catch((err) => {
53-
console.error(err)
54-
process.exit(1)
55-
})
50+
51+
printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
52+
}
5653

5754
const srv = new Server({ dir, dev: true })
5855
srv.start(argv.port)

lib/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ export function deprecated (fn, message) {
2121

2222
return newFn
2323
}
24+
25+
export function printAndExit (message, code = 1) {
26+
if (code === 0) {
27+
console.log(message)
28+
} else {
29+
console.error(message)
30+
}
31+
32+
process.exit(code)
33+
}

0 commit comments

Comments
 (0)