Convert backend code to ES modules

Bundling code now with Rollup for its flat bundling and tree shaking
support. Front end code still goes through Webpack for browser support.
This commit is contained in:
Zaid Abdulla committed 2019-02-04 23:47:54 -08:00
1 parent 824639976f
commit 1e41c72d60
62 files changed
+718 -898

No files matched your search

+5 -5
View File
@@ -1,8 +1,8 @@
#!/usr/bin/env node
#!/usr/bin/env -S node -r esm
"use strict";
const mongodb = require("mongodb");
const config = require("../lib/config");
import { MongoClient } from "mongodb";
import { get } from "../lib/config";
const configData = {
"auth.simple.users.admin.password": "'admin'",
@@ -158,8 +158,8 @@ const configData = {
};
async function run() {
const client = await mongodb.MongoClient.connect(
config.get("MONGODB_CONNECTION_URL"),
const client = await MongoClient.connect(
get("MONGODB_CONNECTION_URL"),
{ useNewUrlParser: true }
);
const collection = client.db().collection("config");
+35 -42
View File
@@ -1,4 +1,4 @@
#!/usr/bin/env node
#!/usr/bin/env -S node -r esm
/**
* Copyright 2013-2017 Zaid Abdulla
*
@@ -19,73 +19,66 @@
*/
"use strict";
const fs = require("fs");
const path = require("path");
import fs from "fs";
import path from "path";
import { connect, devicesCollection } from "../lib/db";
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, p) {
if (!p) p = "";
function recursive(object, path) {
if (!path)
path = "";
for (const key in object) {
if (key[0] === "_" || !object[key]) continue;
for (let key in object) {
if (key[0] === "_" || !object[key])
continue;
let isObject = object[key]["_object"];
let isValue = "_value" in object[key];
const isObject = object[key]["_object"];
const 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;
if (typeof value === "boolean") value = +value;
let values = [object[key]["_writable"] || false, value.toString()];
if (object[key]["_type"])
values.push(object[key]["_type"]);
const values = [object[key]["_writable"] || false, value.toString()];
if (object[key]["_type"]) values.push(object[key]["_type"]);
dataModel[`${path}${key}`] = values;
dataModel[`${p}${key}`] = values;
}
if (isObject) {
let values = [object[key]["_writable"] || false];
dataModel[`${path}${key}.`] = values;
recursive(object[key], `${path}${key}.`);
const values = [object[key]["_writable"] || false];
dataModel[`${p}${key}.`] = values;
recursive(object[key], `${p}${key}.`);
}
}
}
connect(err => {
if (err) throw err;
db.connect(function(err) {
if (err)
throw err;
return devicesCollection.findOne(
{
_id: process.argv[2]
},
{},
(err, device) => {
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");
if (!device)
throw new Error("Device not found");
recursive(device);
recursive(device);
fs.writeFile(outputFilename, JSON.stringify(dataModel, null, 2), () => {
if (err) throw err;
fs.writeFile(outputFilename, JSON.stringify(dataModel, null, 2), function() {
if (err)
throw err;
console.log(`${outputFilename} saved`);
process.exit(0);
});
});
process.stdout.write(`${outputFilename} saved\n`);
process.exit(0);
});
}
);
});