Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: creating a directory tree #1367

Open
wants to merge 3 commits into
base: horizon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/js/trove/filelib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
({
requires: [],
requires: [
{ "import-type": "builtin", name: "string-dict" },
],
provides: {
values: {
"open-input-file": "tany",
Expand All @@ -13,11 +15,12 @@
"close-output-file": "tany",
"close-input-file": "tany",
"create-dir": "tany",
"create-dir-tree": "tany",
"list-files": "tany"
}
},
nativeRequires: ["fs"],
theModule: function(RUNTIME, NAMESPACE, uri, fs) {
theModule: function(RUNTIME, NAMESPACE, uri, SD, fs) {
function InputFile(name) {
this.name = name;
this.fd = fs.openSync(name, "r");
Expand All @@ -27,6 +30,17 @@
this.name = name;
this.fd = fs.openSync(name, (append ? "a" : "w"));
}
var checkISD = RUNTIME.getField(SD, "internal").checkISD;
var isISD = RUNTIME.getField(SD, "internal").isISD;
function makeDirs(baseDir, tree) {
if (!fs.existsSync(baseDir)) fs.mkdirSync(baseDir);
if (isISD(tree)) {
RUNTIME.ffi.toArray(RUNTIME.getField(tree, "keys-list").app()).forEach(function(sub) {
var kid = RUNTIME.getColonField(tree, "get-value").full_meth(tree, sub);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth a comment that this won't stack capture because of what we know about immutable-string-dict's get-value method being flat? Like if this was changed to a generic .get-value dictionary interface, this wouldn't be safe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, possibly

makeDirs(baseDir + "/" + sub, kid);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be more judicious about using path.join or whatever the blessed path library thing is? I know we have other windows issues but that's probably the right thing to do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't tell, tbh: https://nodejs.org/docs/latest/api/fs.html#fsmkdirpath-options-callback gives an example of calling mkdir on "/" even on Windows...

});
}
}

var vals = {
"open-input-file": RUNTIME.makeFunction(function(filename) {
Expand Down Expand Up @@ -169,6 +183,13 @@
fs.mkdirSync(directory);
return true;
}, "create-dir"),
"create-dir-tree": RUNTIME.makeFunction(function(baseDir, subtree) {
RUNTIME.ffi.checkArity(2, arguments, "create-dir-tree", false);
RUNTIME.checkString(baseDir);
checkISD(subtree);
makeDirs(baseDir, subtree);
return true;
}, "create-dir-tree"),
"list-files": RUNTIME.makeFunction(function(directory) {
RUNTIME.ffi.checkArity(1, arguments, "list-files", false);
RUNTIME.checkString(directory);
Expand Down
4 changes: 3 additions & 1 deletion src/js/trove/string-dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,9 @@
};
var internal = {
checkISD: jsCheckISD,
checkMSD: jsCheckMSD
checkMSD: jsCheckMSD,
isISD: isImmutableStringDict,
isMSD: isMutableStringDict
};
return runtime.makeModuleReturn(vals, types, internal);
}
Expand Down