gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
137 lines (119 loc) • 4.97 kB
JavaScript
/*
* This script is used to generate a goeditor project
* It can take several arguments; i.e.
* node generateEditor <editor name> <number of diagrams> <number of palettes>
* Editor name is the name of the editor to generate -- your app will be <editor name>.html. It will live in directory ../<editor name>
* Number of diagrams is how many diagrams to generate for your application
* Number of palettes is how many palettes to generate for your application
* Example:
* node generateEditor "testEditor" 2 2
* This generates an editor application at path ../testEditor
* In that directory will be all files needed for a goeditor app with 2 diagrams and 2 palettes
* The main app would be at ../testEditor/testEditor.html
*/
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const ncp = require('ncp').ncp;
const copydir = require('copy-dir');
const rimraf = require('rimraf');
var editorName = process.argv[2];
//console.log(editorName);
// Part 1: Copy ./goeditor.html to ../<editorName>.html
var appPath = "../" + editorName;
console.log("Copying ./goeditor.html contents to " + appPath + "...");
copydir.sync("./", appPath, function(stat, filepath, filename) {
return true;
}, function (err) {
console.error(err);
});
// iterate over ../<editorName> contents and delete irrelevant files and directories
fs.readdir(appPath, function(err, list){
if (err) return;
list.forEach(function(file){
if (file.indexOf(".json") != -1 || file == "generateEditor.js") {
fs.unlink(appPath + "/" + file, (err) => {
if (err) throw err;
console.log(file + ' was deleted');
});
}
if (file === "node_modules") {
rimraf.sync(appPath+"/"+file);
}
});
});
// Part 2: Build the call to setupEditor() in init() of ../<editorName>.html with proper arguments
// arg1: diagramsCount
var diagramsCount = process.argv[3];
if (diagramsCount == undefined) diagramsCount = 1;
// inspector?
// Palettes
var palettesCount = process.argv[4];
if (palettesCount == undefined) palettesCount = 0;
// Overviews
var setupCall = "\t\t// Do not remove this function call! This sets up your editor \n\t\tsetupEditorApplication("+diagramsCount+", " + palettesCount + ");";
// write the setupCall to the proper insertion point in <editorName>.html
var indexPath = appPath + "/" + editorName + ".html";
var app = fs.writeFileSync(indexPath, "");
var lines = fs.readFileSync("./goeditor_minimal.html", 'utf-8').split('\n');
for (let i in lines) {
var line = lines[i];
// this is the insertion point for setup call
if (line.indexOf("ge-setup-call-insertion-point") !== -1) {
console.log("Injecting setup call into application...");
maybeAppendLine(setupCall, indexPath);
}
// inject diagrams
else if (line.indexOf("ge-diagrams-container") != -1) {
console.log("Injecting diagram divs");
line = '<div id="ge-diagrams-container" style="display: flex;">';
if (diagramsCount > 0) line += "\n";
for (var j = 0; j < diagramsCount; j++) {
// TODO allow dev to supply each diagram height / width / background color / stroke???
var w = (1/diagramsCount)*100;
line += '\t<div id="ge-diagram-'+j+'" style="height: 800px; width: ' + w + '%; background: white; border: 1px solid black; "></div>\n';
}
line += '</div>';
maybeAppendLine(line, indexPath);
}
// inject palettes
else if (line.indexOf('id="ge-palettes-container">') != -1) {
console.log("Injecting palettes divs");
line = '<div id="ge-palettes-container">';
if (palettesCount > 0) line += "\n";
for (var j = 0; j < palettesCount; j++) {
// TODO allow dev to supply palette params?
line += '\t<h3> Palette ' + j + '</h3><div><div id="ge-palette-'+j+'" style="height: 500px; background: white; border: 1px solid black; "></div></div>\n';
}
line += '</div>';
maybeAppendLine(line, indexPath);
}
// inject overviews -- default 1 overview / diagram
else if (line.indexOf('id="ge-overviews-container">') != -1) {
line = '<div id="ge-overviews-container">';
if (diagramsCount == 0) {
line = ""; // don't have any overviews if there are no diagrams
}
else {
line += "\n";
console.log("Injecting overviews divs");
for (var j = 0; j < diagramsCount; j++) {
line += '\t<h3> Overview ' + j + '</h3><div><div id="ge-overview-'+j+'" style="height: 200px; background: white; border: 1px solid black; "></div></div>\n';
}
line += '</div>';
}
maybeAppendLine(line, indexPath);
}
else {
// replace all instances of "GoEditor" with the proper application name
if (line.indexOf("GoEditor") !== -1) {
line = line.replace("GoEditor", editorName);
}
maybeAppendLine(line, indexPath);
}
}
function maybeAppendLine(line, path) {
var tline = line.trim();
fs.appendFileSync(path, line+"\n");
}
fs.unlink(appPath + "/goeditor_minimal.html");