diff --git a/server/validate.js b/server/validate.js index 07ffceff..8a091605 100644 --- a/server/validate.js +++ b/server/validate.js @@ -1,5 +1,7 @@ 'use strict'; +const tryCatch = require('try-catch'); + const config = require('./config'); const exit = require('./exit'); const columns = require('./columns'); @@ -14,15 +16,14 @@ module.exports.root = (dir, fn) => { if (config('dropbox')) return; - const fs = require('fs'); + const {statSync} = require('fs'); + const [error] = tryCatch(statSync, dir); - fs.stat(dir, (error) => { - if (error) - return exit('cloudcmd --root: %s', error.message); - - if (typeof fn === 'function') - fn('root:', dir); - }); + if (error) + return exit('cloudcmd --root: %s', error.message); + + if (typeof fn === 'function') + fn('root:', dir); }; module.exports.editor = (name) => { diff --git a/test/server/validate.js b/test/server/validate.js index 5938689e..73a4e728 100644 --- a/test/server/validate.js +++ b/test/server/validate.js @@ -50,10 +50,12 @@ test('validate: root: /home', (t) => { test('validate: root: stat', (t) => { const fn = sinon.stub(); - const {stat} = fs; + const {statSync} = fs; const error = 'ENOENT'; - fs.stat = (dir, fn) => fn(Error(error)); + fs.statSync = () => { + throw Error(error); + }; mockRequire(exitPath, fn); @@ -62,7 +64,7 @@ test('validate: root: stat', (t) => { root('hello', fn); const msg = 'cloudcmd --root: %s'; - fs.stat = stat; + fs.statSync = statSync; mockRequire.stop(exitPath); t.ok(fn.calledWith(msg, error), 'should call fn');