Skip to content

Instantly share code, notes, and snippets.

@bryangingechen
Last active Aug 22, 2020
Embed
What would you like to do?
const fs = require('fs');
const [fileName, outputFile] = process.argv.slice(2);
if (fileName && outputFile) {
const nodes = JSON.parse(fs.readFileSync(fileName, 'utf8'));
if (!Array.isArray(nodes))
return console.log(`${fileName} could not be parsed into an Array`);
if (nodes.some(d => Object.keys(d).some(e => !['id', 'value', 'pinned'].includes(e))))
return console.log(`the objects in ${fileName} may only have 'id', 'version' and 'pinned' properties`);
if (nodes.some(d => d.hasOwnProperty('value') && typeof d.value !== 'string') ||
nodes.some(d => d.hasOwnProperty('pinned') && typeof d.pinned !== 'boolean'))
return console.log(`the 'value' and 'pinnned' values in ${fileName}, if present, must be Strings and Booleans, respectively`);
const newNodes = nodes.reduce((acc, node) => {
// mutates the elements of nodes, so don't use nodes again after this!
node.id = acc.length;
acc.push(node);
return acc;
}, []);
console.log(`${newNodes.length} cells, can be reuploaded as version ${newNodes.length+1}`);
console.log(`writing to ${outputFile}`);
fs.writeFileSync(outputFile, JSON.stringify(newNodes), {flag:'wx'});
} else {
console.log(`run with:
node reindex-nbjson.js inputFile outputFile`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment