From aac8164d829c3fbf782031ac3d515669ea1f2f05 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Mon, 10 Jul 2017 16:50:43 -0400 Subject: [PATCH] fix(back): chdir.back returns a promise, close #3 --- index.js | 47 +++++++++++++++++++------------------- package.json | 12 ++++++++-- test/chdir-promise-spec.js | 9 ++++++++ 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index beb2c61..f999b25 100644 --- a/index.js +++ b/index.js @@ -1,41 +1,40 @@ -var la = require('lazy-ass'); -var check = require('check-more-types'); +var la = require('lazy-ass') +var check = require('check-more-types') -var q = require('q'); -var fs = require('fs'); -var S = require('spots'); -var debug = require('debug')('chdir-promise'); +var q = require('q') +var fs = require('fs') +var S = require('spots') +var debug = require('debug')('chdir-promise') // stack -var folders = []; +var folders = [] -function _to(folderName) { - la(check.unemptyString(folderName), 'missing git repo folder'); - la(fs.existsSync(folderName), 'cannot find folder', folderName); +function _to (folderName) { + la(check.unemptyString(folderName), 'missing git repo folder') + la(fs.existsSync(folderName), 'cannot find folder', folderName) - var current = process.cwd(); - la(check.unemptyString(folderName), 'missing folder'); - process.chdir(folderName); - debug('chdir to folder', process.cwd()); + var current = process.cwd() + la(check.unemptyString(folderName), 'missing folder') + process.chdir(folderName) + debug('chdir to folder', process.cwd()) - folders.push(current); + folders.push(current) - return current; + return current } -function comeBack() { +function comeBack () { if (!folders.length) { - return; + return Promise.resolve() } - var folder = folders.pop(); - process.chdir(folder); - debug('restored folder', folder); - return folder; + var folder = folders.pop() + process.chdir(folder) + debug('restored folder', folder) + return Promise.resolve(folder) } module.exports = { to: S(q.try, _to, S), back: comeBack, from: comeBack -}; - +} diff --git a/package.json b/package.json index e5a92a3..af8c337 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,12 @@ "test": "node test/chdir-promise-spec.js", "semantic-release": "semantic-release pre && npm publish && semantic-release post", "commit": "commit-wizard", - "size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";" + "size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";", + "pretty": "prettier-standard *.js src/*.js", + "prelint": "npm run pretty", + "lint": "standard --verbose --fix *.js src/*.js", + "pretest": "npm run lint", + "issues": "git-issues" }, "repository": { "type": "git", @@ -41,9 +46,12 @@ "spots": "0.5.0" }, "devDependencies": { + "git-issues": "^1.3.1", "github-post-release": "^1.7.1", "pre-git": "3.15.0", - "semantic-release": "^6.3.6" + "prettier-standard": "^6.0.0", + "semantic-release": "^6.3.6", + "standard": "^10.0.2" }, "config": { "pre-git": { diff --git a/test/chdir-promise-spec.js b/test/chdir-promise-spec.js index 44a3998..59b1239 100644 --- a/test/chdir-promise-spec.js +++ b/test/chdir-promise-spec.js @@ -1,6 +1,8 @@ var la = require('lazy-ass'); var check = require('check-more-types'); var folders = require('..'); +var join = require('path').join; +var parentFolder = join(process.cwd(), '..') la(check.object(folders), 'expected folders to be an object', folders); la(check.fn(folders.to), 'expected folders.to to be a function', folders); @@ -18,3 +20,10 @@ folders.to(__dirname) la(result === 'foo', 'incorrect result', result); }) .done(); + +// .back() returns a promise +folders.to(parentFolder) + .then(function () { + return folders.back() + .then(function () {}) + }).done();