Skip to content

Commit

Permalink
Move schemes into separate JSON files (#13)
Browse files Browse the repository at this point in the history
This has the benefits of separating data from code and of having a
language-agnostic specification of schemes (for implementations of
Sanscript in other programming languages).

This also adds a build step integrating the scheme data into the JS file
-- the file that now contains only the code has been moved to a
subdirectory and the build step (npm run dist) outputs to sanscript.js
(so this causes no breaking changes).
  • Loading branch information
psvenk authored Aug 5, 2020
1 parent db26779 commit 04c968d
Show file tree
Hide file tree
Showing 81 changed files with 1,336 additions and 1,397 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.*~
node_modules
/sanscript.js
54 changes: 54 additions & 0 deletions dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fsp = require("fs").promises;
const path = require("path");

async function main() {
// Get lists of Brahmic and roman schemes
// Each of bschemes and rschemes is an array of which each element is of
// the form [filename, filepath]
const [bschemes, rschemes] = await Promise.all(
["brahmic", "roman"].map(async x => {
const dirpath = path.join(__dirname, "src", "schemes", x)
let paths = [];
for (const filename of await fsp.readdir(dirpath)) {
paths.push([filename, path.join(dirpath, filename)]);
}
return paths;
})
);

// Get arrays of Brahmic and roman scheme file contents
// Each of bfiles and rfiles is an array of which each element is of the
// form [schemeName, fileContents]
const [bfiles, rfiles] = await Promise.all([bschemes, rschemes].map(x =>
Promise.all(x.map(async ([filename, filepath]) => {
let schemeName = filename.split(".");
schemeName.pop();
const fileContents = await fsp.readFile(filepath);
return [schemeName, fileContents];
}))
));

// Get file handle for sanscript.js
let out;
try {
out = await fsp.open(
path.join(__dirname, "sanscript.js"), "w"
);
out.write("var schemes = {};\n");
for (const [scheme, contents] of bfiles) {
out.write(`schemes.${scheme} = ${contents};\n`);
}
for (const [scheme, contents] of rfiles) {
out.write(`schemes.${scheme} = ${contents};\n`);
}
// Write the code to the output file
out.write(await fsp.readFile(
path.join(__dirname, "src", "sanscript.js")
));
} finally {
if (out !== undefined)
await out.close();
}
}

main().catch(console.error);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"test": "test"
},
"scripts": {
"test": "qunit"
"test": "qunit",
"dist": "node dist.js"
},
"repository": {
"type": "git",
Expand Down
1,396 changes: 0 additions & 1,396 deletions sanscript.js

This file was deleted.

Loading

0 comments on commit 04c968d

Please sign in to comment.