@optimajet/workflow-designer
Version:
Designer for Workflow Engine
69 lines (55 loc) • 2.16 kB
JavaScript
import {exec} from 'node:child_process';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import os from 'node:os';
import fs, {promises as fsPromises} from 'node:fs';
import util from 'node:util';
import crypto from 'node:crypto';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function runCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
}
async function passSettings(file, url, schemeCode) {
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const data = await readFile(file);
const result = data.toString()
.replace(/URL_PLACEHOLDER/g, url)
.replace(/SCHEME_CODE_PLACEHOLDER/g, schemeCode);
await writeFile(file, result);
}
async function createTempFolder(sourcePath) {
const args = process.argv.slice(2).join('|');
const digest = crypto.createHash('sha256')
.update(args)
.digest('hex');
const tempPath = path.join(sourcePath, '..', `workflow-designer-${digest}`);
await fsPromises.mkdir(tempPath, {recursive: true});
await fsPromises.copyFile(path.resolve(sourcePath, 'index.js'), path.resolve(tempPath, 'index.js'));
await fsPromises.copyFile(path.resolve(sourcePath, 'index.html'), path.resolve(tempPath, 'index.html'));
return tempPath
}
async function main() {
const args = process.argv.slice(2);
const templateDirectory = path.resolve(__dirname, '..', 'bin', 'template');
const tempFolderName = await createTempFolder(templateDirectory);
const jsFile = path.resolve(tempFolderName, 'index.js');
const htmlFile = path.resolve(tempFolderName, 'index.html');
const designerUrl = args[0] ?? 'https://demo.workflowengine.io/Designer/API';
const schemeCode = args[1] ?? 'SimpleWF';
await passSettings(jsFile, designerUrl, schemeCode);
const platform = os.platform();
const command = platform === 'win32' ? `start ${htmlFile}` : `open ${htmlFile}`;
await runCommand(command);
}
main().catch(console.error);