From 469fba8b96e00fb9f362432b379196864d3a4595 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Sat, 14 Jan 2017 23:09:12 +0530 Subject: [PATCH 1/2] Add correct sync version of error handling with existSync. --- bin/next-build | 27 ++++++++++++--------------- bin/next-dev | 27 ++++++++++++--------------- lib/utils.js | 10 ++++++++++ 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/bin/next-build b/bin/next-build index e52d0ce35e82a..553c312edcf98 100755 --- a/bin/next-build +++ b/bin/next-build @@ -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: { @@ -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) => { diff --git a/bin/next-dev b/bin/next-dev index 4c86f50206f87..5cef200bbb8b5 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -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: { @@ -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) diff --git a/lib/utils.js b/lib/utils.js index 567314eb9cab8..59553c9e6b3db 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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) +} From 9ecca4070b66e3c7f16dbd6817a1878a49778ca0 Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Sun, 15 Jan 2017 13:48:38 +0530 Subject: [PATCH 2/2] Update utils.js --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 59553c9e6b3db..f9116cb952103 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -22,7 +22,7 @@ export function deprecated (fn, message) { return newFn } -export function printAndExit (message, code = -1) { +export function printAndExit (message, code = 1) { if (code === 0) { console.log(message) } else {