74 lines
1.6 KiB
JavaScript
Executable File
74 lines
1.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const outputFilename = path.resolve(`data_model_${process.argv[2]}.json`);
|
|
process.chdir(path.resolve(path.dirname(fs.realpathSync(__filename)), ".."));
|
|
|
|
const db = require("../lib/db");
|
|
|
|
const dataModel = {};
|
|
|
|
|
|
function recursive(object, path) {
|
|
if (!path)
|
|
path = "";
|
|
|
|
for (let key in object) {
|
|
if (key[0] === "_" || !object[key])
|
|
continue;
|
|
|
|
let isObject = object[key]["_object"];
|
|
let isValue = "_value" in object[key];
|
|
|
|
if ((isObject && isValue) || (!isObject && !isValue))
|
|
throw new Error("Unexpected object");
|
|
|
|
if (isValue) {
|
|
let value = object[key]["_value"];
|
|
if (typeof value === "boolean")
|
|
value = +value;
|
|
|
|
let values = [object[key]["_writable"] || false, value.toString()];
|
|
if (object[key]["_type"])
|
|
values.push(object[key]["_type"]);
|
|
|
|
dataModel[`${path}${key}`] = values;
|
|
}
|
|
|
|
if (isObject) {
|
|
let values = [object[key]["_writable"] || false];
|
|
dataModel[`${path}${key}.`] = values;
|
|
recursive(object[key], `${path}${key}.`);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
db.connect(function(err) {
|
|
if (err)
|
|
throw err;
|
|
|
|
return db.devicesCollection.findOne({
|
|
_id: process.argv[2]
|
|
}, {}, function(err, device) {
|
|
if (err)
|
|
throw err;
|
|
|
|
if (!device)
|
|
throw new Error("Device not found");
|
|
|
|
recursive(device);
|
|
|
|
fs.writeFile(outputFilename, JSON.stringify(dataModel, null, 2), function() {
|
|
if (err)
|
|
throw err;
|
|
|
|
console.log(`${outputFilename} saved`);
|
|
process.exit(0);
|
|
});
|
|
});
|
|
});
|