Skip to content

Commit

Permalink
xs platform: flesh out pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Nov 27, 2019
1 parent 014f758 commit 1ea6a96
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions xs-platform/pathlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ export function makePath(filename, { File, Iterator }) {
return contents;
}

function withWriting(suffix, thunk) {
const tmpName = filename + suffix;
const file = new File(tmpName, true);
const fp = harden({
writeSync(value) {
file.write(value);
}
});
try {
thunk(fp);
} finally {
file.close();
}
File.rename(tmpName, filename);
}

function atomicReplace(contents) {
return new Promise(
(resolve, reject) => {
try {
const tmp = mk(filename + '.tmp');
const tmpF = new File(tmp, true);
tmpF.write(contents);
tmpF.close();
File.rename(tmp, filename);
} catch(oops) {
reject(oops);
return;
}
resolve();
});
}

function readdirSync(options) {
const dirIter = new Iterator(filename);
let item;
Expand All @@ -32,6 +65,17 @@ export function makePath(filename, { File, Iterator }) {
return items;
}

function readdir() {
return new Promise((resolve, reject) => {
try {
const names = readdirSync({}).map(item => item.name);
resolve(names);
} catch (oops) {
reject(oops);
}
});
}

function butLast(p) {
const pos = p.lastIndexOf('/');
return pos >= 0 ? p.slice(0, pos + 1) : p;
Expand Down Expand Up @@ -76,6 +120,9 @@ export function makePath(filename, { File, Iterator }) {
return readFileSync().replace(/\n$/, '').split('\n');
},
readdirSync,
readdir,
bundleSource,
atomicReplace,
withWriting,
});
}

0 comments on commit 1ea6a96

Please sign in to comment.