-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Provide utils to save in-memory BCD to files #3617
Comments
Here's a save.js that almost works: 'use strict';
const fs = require('fs');
const path = require('path');
function isDirectory(fp) {
try {
return fs.statSync(fp).isDirectory();
} catch (e) {
return false;
}
}
function save(bcd, dirname) {
function processObject(object, keypath) {
for (const [key, value] of Object.entries(object)) {
const candidate = path.join(dirname, ...keypath, key);
if (isDirectory(candidate)) {
// If the path is a directory, recurse.
processObject(value, keypath.concat(key));
} else {
// Otherwise, write data to file.
const filepath = `${candidate}.json`;
// Add wrapping objects with keys as in keypath.
let wrappedValue = value;
const keys = keypath.concat(key).reverse();
for (const key of keys) {
const wrapper = {};
wrapper[key] = wrappedValue;
wrappedValue = wrapper;
}
const json = JSON.stringify(wrappedValue, null, ' ') + '\n';
fs.writeFileSync(filepath, json);
}
}
}
processObject(bcd, []);
}
module.exports = save; Unfortunately, the file structure doesn't exactly line up with expectations. This is what
Still, it already matches api/ exactly, confirmed by changing a single support statement and seeing just that change written back. css/ is also very close to matching. @Elchi3 @jpmedley do you think aligning the file structure with the data structure until there are no differences is worthwhile? Would you use such a utility to make edits yourself? |
I sent #3618 to resolve the only issue in css/. |
I have no opinion on the first question. On the second question, as soon as I say no, I'll think of something to use it for. |
So far I was worried that aligning the file structure with the data structure will make us uncomfortable when editing or that it will be inflexible for adding more data. For machines editing the data there is obviously a benefit if we're aligned. All this said, I could imagine enforcing these more strict file rules, so that there's an even better programmatic data access (which is the whole purpose of this project anyway). |
If you need the source file for a data point, I don't think you need to restructure. If as BCD is being loaded, the module could attach it to the tree somewhere as a symbol. This would keep it out of the way of iterators, but would still be accessible for the kumascript macro to generate an accurate link. My point is that if there's another way to solve 2985 then it shouldn't be regarded as an argument for restructuring files. I'd also be nervous about changing the file structure at this late date. It seems straight forward, but you'll be stomping out bugs for weeks. |
I don't think we're talking about restructuring, but just aligning the very few non-conforming cases as found by @foolip to a file structure that can already be observed otherwise. |
I've filed #6585 which supersedes this issue. |
The BCD module itself (index.js) reads all of the data files into an in-memory object which consumers can use.
When making scripted changes to the data, one has to load, edit and save some JSON files. If one loads each file individually it can be done, but unfortunately then one can't just
require('.')
and work with the merged data.It would be helpful if the file structure could be written back exactly from the in-memory BCD and there's an in-repo utility for doing this.
The text was updated successfully, but these errors were encountered: