Skip to content

Commit

Permalink
Added flow5 command-line app.
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed May 2, 2024
1 parent 66f0ce4 commit ca410be
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
144 changes: 144 additions & 0 deletions bin/flow5
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#! /usr/bin/env node

require('total5');

const ARGS = process.argv; // TEST: ['-', '-', 'CMD']
const DIR = process.cwd();
const Paths = {};
const KeyToPath = {};

function done(err) {
err && console.error('ERROR:', err);
console.timeEnd('[Done]');
}

function load(args) {

var paths = [];
var port = 8000;

for (let i = 2; i < args.length; i++) {
var arg = args[i];
if ((/^\d+$/).test(arg))
port = +arg;
else
paths.push(arg.trim().replace(/"/g, ''));
}

console.log('[Total.js v5 Flow Engine]');

if (paths.length)
FUNC.server(paths, port);
else
FUNC.help();
}

setTimeout(load, 10, ARGS);

FUNC.help = function() {
console.log('filename|URL_address [PORT_NUMBER] : it starts Flow with the web server on port "8000"');
done();
};

FUNC.server = function(paths, port) {

F.config.$imprint = false;
F.config.$dirpublic = DIR;
F.config.$sourcemap = false;
F.config.$minifyjs = false;
F.config.$minifycss = false;
F.config.$minifyhtml = false;

var notify = $ => Flow.notify($, $.params.id);
var count = 0;

ROUTE('GET /notify/{id}/', notify);
ROUTE('POST /notify/{id}/ <1MB', notify);

ROUTE('SOCKET /flowstreams/{id}/ <8MB', function($) {
$.autodestroy();
Flow.socket($.params.id, $);
});

Flow.on('save', function(schema) {

// var cache = Paths[schema.id];
var path = KeyToPath[schema.id];
var cache = Paths[path];

// Check HTTPS/HTTP source
if (!cache)
return;

if (cache.components && cache.design) {
// single file
Total.Fs.writeFile(path, JSON.stringify(schema, null, '\t'), NOOP);
} else {
// Multi-flow
if (cache[schema.id]) {
cache[schema.id] = schema;
Total.Fs.writeFile(path, JSON.stringify(cache, null, '\t'), NOOP);
}
}

});

console.error('|--- http://127.0.0.1:' + port);

paths.wait(async function(path, next) {

var meta = null;

if ((/^http(s):\/\//).test(path)) {
meta = await RESTBuilder.GET(path).promise();
} else {
meta = await F.readfile(path, 'utf8');
meta = meta.parseJSON(true);
Paths[path] = meta;
}

if (!meta) {
console.error('ERROR:', 'invalid meta -', path);
next();
return;
}

if (meta.components && meta.design) {
var tmp = {};
tmp[meta.id] = meta;
meta = tmp;
}

Object.keys(meta).wait(function(key, resume, index) {

if (key === 'variables') {
resume();
return;
}

KeyToPath[key] = path;

var flowstream = meta[key];

if (!flowstream.id)
flowstream.id = path.makeid() + (index ? index : '');

Flow.load(flowstream, function(err) {

if (err)
console.error('ERROR:', err);
else {
console.log('|--- {0}:'.format(flowstream.name || flowstream.id), 'https://flow.totaljs.com/?socket=' + encodeURIComponent('http://127.0.0.1:{0}/flowstreams/{1}/'.format(port, flowstream.id)));
count++;
}

resume();
});

}, next);

}, function() {
count && F.http({ load: 'none', port: port });
});

};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"test": "test"
},
"bin": {
"total5": "./bin/total5"
"total5": "./bin/total5",
"flow5": "./bin/flow"
},
"scripts": {
"test": "cd tests && node run.js"
Expand Down

0 comments on commit ca410be

Please sign in to comment.