Skip to content

Commit

Permalink
chore: add readFileJson, rename internal URL#asDir to URL#dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Aug 11, 2021
1 parent 6accb7b commit e9055c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions .task/internal/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,32 @@ export const copydir = async function copydir(src, dst) {

for (const dirent of await readdir(src, { withFileTypes: true })) {
await (dirent.isDirectory() ? copydir : copyFile)(
src.asDir.to(dirent.name),
dst.asDir.to(dirent.name)
src.dir.to(dirent.name),
dst.dir.to(dirent.name)
)
}
}

copydir(URL.from(src), URL.from(dst))
}

/** Asynchronously reads a file as parsed JSON. */
export const readFileJson = {
async readFileJson(/** @type {import('fs').PathLike | fs.FileHandle} */ path) {
const json = await fs.readFile(path, 'utf8')

/** @type {JSONValue} */
const data = JSON.parse(json)

return data
}
}.readFileJson

fs.copydir = copydir
fs.readFileJson = readFileJson

/** @typedef { string | number | boolean | null | JSONArray | JSONObject } JSONValue */
/** @typedef { JSONValue[] } JSONArray */
/** @typedef {{ [k: string]: JSONValue }} JSONObject */

export default fs
2 changes: 1 addition & 1 deletion .task/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class URL extends globalThis.URL {
return this.href.endsWith(searchString, length)
}

get asDir() {
get dir() {
return new URL(this.href + '/')
}

Expand Down

0 comments on commit e9055c6

Please sign in to comment.