gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
108 lines (94 loc) • 3.6 kB
JavaScript
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const ncp = require('ncp').ncp;
const copydir = require('copy-dir');
var configFilepath = process.argv[2];
var configFile = fs.readFileSync(configFilepath);
var config = JSON.parse(configFile);
// Part 1: Copy goeditor_boilerplate
var appPath = "../" + config.fileName;
console.log("Copying /goeditor_boilerplate contents to " + appPath + "...");
copydir.sync("./", appPath, function(stat, filepath, filename) {
if (filename === "node_modules" || filename.indexOf(".json") !== -1 || filename.indexOf(".bat") !== -1) {
return false;
}
return true;
}, function (err) {
console.error(err);
});
// Part 2: Inject the code in initFiles at the insertion point in init(), and the code in postInitFiles after init()
console.log("Injecting custom code into application...");
var indexPath = appPath + "/" + config.fileName + ".html";
var app = fs.writeFileSync(indexPath, "");
var lines = fs.readFileSync(appPath + "/goeditor.html", 'utf-8').split('\n');
for (let i in lines) {
var line = lines[i];
// this is the insertion point for init files
if (line.indexOf("12DY@9dkd)dk") !== -1) {
console.log("Injecting initFiles into application...");
var initFiles = config.initFiles;
for (let j in initFiles) {
var initFile = initFiles[j];
var initLines = fs.readFileSync(initFile, 'utf-8').split('\n');
for (let k in initLines) {
var initLine = initLines[k];
maybeAppendLine(initLine, indexPath, false);
}
}
}
// this is the insertion point for post-init files
if (line.indexOf("ijiwd8up@90n") !== -1) {
console.log("Injecting postInitFiles into application...");
var postInitFiles = config.postInitFiles;
for (let j in postInitFiles) {
var piFile = postInitFiles[j];
var piLines = fs.readFileSync(piFile, 'utf-8').split('\n');
for (let k in piLines) {
var piLine = piLines[k];
maybeAppendLine(piLine, indexPath, false);
}
}
}
// replace all instances of "GoEditor" with the proper application name
if (line.indexOf("GoEditor") !== -1) {
line = line.replace("GoEditor", config.name);
}
// replace ###version### with the correct app version number
if (line.indexOf("##version###") !== -1) {
line = line.replace("###version###", "v"+config.version);
}
// replace description text
if (line.indexOf("Insert description here.") !== -1) {
line = line.replace("Insert description here.", config.description);
}
maybeAppendLine(line, indexPath, true)
}
var doNotAppend = false; var isMultilineComment = false;
function maybeAppendLine(line, path, removeComments) {
var tline = line.trim();
// do not bloat file with comments
if (removeComments) {
if (tline.substr(0,2) === "//" || tline.substr(0,2) === "<!") doNotAppend = true;
if (tline.indexOf("/*") !== -1 && tline.indexOf("*/") !== -1) {
}
else if (tline.indexOf("/*") !== -1) isMultilineComment = true;
else if (tline.indexOf("*/") !== -1) {
isMultilineComment = false;
doNotAppend = true;
}
if (isMultilineComment) doNotAppend = true;
}
// do not bloat file with empty lines
if (tline === "") doNotAppend = true;
if (!doNotAppend) {
fs.appendFileSync(path, line+"\n");
}
doNotAppend = false;
}
// Part 3: Cleanup
console.log("Cleaning up...");
// remove the now useless goeditor.html file
fs.unlinkSync(appPath + "/goeditor.html");
fs.unlinkSync(appPath + "/readme.html");
fs.unlinkSync(appPath + "/generateEditor.js");