forked from natcl/electron-node-red
-
Notifications
You must be signed in to change notification settings - Fork 32
/
merger.js
executable file
·85 lines (77 loc) · 2.95 KB
/
merger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
const fs = require("fs");
const path = require('path');
const pkg = require("./package-template.json");
let arg = "./example";
let flowfile = null;
let dn = arg;
let app;
// If extra param specified then is it a directory or a package or flow file ?
if (process.argv.length === 3 ) {
arg = process.argv[2];
let dn = path.dirname(arg);
if (path.extname(arg) === ".json") {
if (path.basename(arg, '.json') !== "package") {
flowfile = path.basename(arg);
app = require(path.join(dn, "package.json"));
}
else {
app = require(arg);
}
}
else {
app = require(path.join(arg, "package.json"));
dn = arg;
}
}
else {
app = require(arg+"/package.json");
}
// Merge electron settings over project settings (project has priority)
const merge = {
...app.dependencies,
...pkg.dependencies
};
pkg.dependencies = merge;
// Try to get flow file name from package.json setiings
if (app.hasOwnProperty("node-red") && app["node-red"].hasOwnProperty("settings") && app["node-red"].settings.hasOwnProperty("flowFile") ) {
pkg.NRelectron.flowFile = app["node-red"].settings.flowFile;
} // or the npm scripts if there is a run command
else if (app.hasOwnProperty("scripts") && app.scripts.hasOwnProperty("start")) {
pkg.NRelectron.flowFile = app.scripts.start.split(' ').pop();
} // or the command line if the user gave us a name - or just guess flow.json.
else {
pkg.NRelectron.flowFile = flowfile || "flow.json";
}
// If dashboard is in package.json assume start with dashboard.
if (merge.hasOwnProperty("node-red-dashboard")) {
pkg.NRelectron.start = "dashboard";
}
// If map is not in package.json then force it to be hidden
if (!merge.hasOwnProperty("node-red-contrib-web-worldmap")) {
pkg.NRelectron.showmap = false;
}
pkg.name = app.name;
pkg.version = app.version;
pkg.description = app.description;
// console.log(pkg);
// Copy over existing flow file and creds file
fs.copyFile(path.join(arg, pkg.NRelectron.flowFile), path.join("./", pkg.NRelectron.flowFile), (err) => {
if (err) { console.log("Failed to copy flows file - "+path.join(arg, pkg.NRelectron.flowFile)); }
else { console.log('Copied flows file - '+pkg.NRelectron.flowFile); }
});
const creds = path.basename(pkg.NRelectron.flowFile,".json")+"_cred.json";
fs.copyFile(path.join(arg, creds), path.join("./", creds), (err) => {
if (err) { console.log("Failed to copy creds file - "+path.join(arg, creds)); }
else { console.log('Copied creds file - '+creds); }
});
// Finally re-write the new package.json
fs.writeFile("./package.json", JSON.stringify(pkg, null, 4), 'utf8', function (err) {
if (err) { console.log("Failed to re-write package.json file."); }
else {
console.log("Merged package.json");
console.log("OK - you can now run yarn");
console.log("and then yarn start to run");
console.log(" or yarn dist to build");
}
});