Skip to content

Commit

Permalink
xs pathlib: statSync, readlinesSync
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Nov 25, 2019
1 parent 258b178 commit 7fa0a8d
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions xs-platform/pathlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,39 @@ const harden = x => Object.freeze(x, true);
export function makePath(filename, { File, Iterator }) {
const mk = there => makePath(there, { File, Iterator });

function readFileSync() {
let file;
try {
file = new File(filename);
} catch(oops) {
// not sure why xs loses error messages, but without logging, we just get:
// xs-platform/pathlib.js:21: exception: throw!
console.log({ filename, message: oops.message });
throw(new Error(`${filename}: ${oops.message}`));
}
const contents = file.read(String);
file.close();
return contents;
}

function readdirSync(options) {
const dirIter = new Iterator(filename);
let item;
const items = [];
while ((item = dirIter.next())) {
const f = typeof item.length === 'number';
items.push(harden({
name: item.name,
isFile: () => f,
}));
}
return items;
}

return harden({
toString() {
return filename;
},
resolve(...others) {
// ISSUE: support ../?
// TODO: chop off filename at last /
Expand All @@ -13,19 +45,14 @@ export function makePath(filename, { File, Iterator }) {
// ISSUE: support ../?
return mk([filename, ...others].join('/'));
},
readFileSync() {
let file;
try {
file = new File(filename);
} catch(oops) {
// not sure why xs loses error messages, but without logging, we just get:
// xs-platform/pathlib.js:21: exception: throw!
console.log({ filename, message: oops.message });
throw(new Error(`${filename}: ${oops.message}`));
}
const contents = file.read(String);
file.close();
return contents;
statSync() {
new File(filename);
return harden({});
},
readFileSync,
readlinesSync() {
return readFileSync().replace(/\n$/, '').split('\n');
},
readdirSync,
});
}

0 comments on commit 7fa0a8d

Please sign in to comment.