Skip to content

Commit

Permalink
feat(root-link-target): drop Node.js support and use async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Nov 17, 2020
1 parent c9001c3 commit ce3290e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-dancers-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"root-link-target": major
---

Drop Node.js 8 support and use async/await syntax.
28 changes: 11 additions & 17 deletions root-link-target/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@ const path = require('path')
const pathTemp = require('path-temp')
const nextPath = require('next-path')

module.exports = (filePath) => {
module.exports = async (filePath) => {
filePath = path.resolve(filePath)
const end = path.dirname(filePath)
let dir = path.parse(end).root

return new Promise((resolve, reject) => {
(function can () {
canLink(filePath, pathTemp(dir))
.then((result) => {
if (result) {
resolve(dir)
} else if (dir === end) {
reject(new Error(`${filePath} cannot be linked to anywhere`))
} else {
dir = nextPath(dir, end)
can()
}
})
.catch(reject)
}())
})
while (true) {
const result = await canLink(filePath, pathTemp(dir))
if (result) {
return dir
} else if (dir === end) {
throw new Error(`${filePath} cannot be linked to anywhere`)
} else {
dir = nextPath(dir, end)
}
}
}

module.exports.sync = (filePath) => {
Expand Down
2 changes: 1 addition & 1 deletion root-link-target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"fs"
],
"engines": {
"node": ">=8.15"
"node": ">=10"
},
"author": "Zoltan Kochan <z@kochan.io> (https://www.kochan.io/)",
"license": "MIT",
Expand Down
11 changes: 4 additions & 7 deletions root-link-target/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ test('rootLinkTarget.sync()', t => {
t.end()
})

test('rootLinkTarget.sync()', t => {
rootLinkTarget('package.json')
.then(root => {
t.equal(typeof root, 'string')
t.end()
})
.catch(t.end)
test('rootLinkTarget.sync()', async t => {
const root = await rootLinkTarget('package.json')
t.equal(typeof root, 'string')
t.end()
})

0 comments on commit ce3290e

Please sign in to comment.