/* This file is part of »type2«. Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' »type2« is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. »type2« is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with »type2«. If not, see . */ const _fs = require("fs"); /** * @author fenris */ type type_parameters = {path ?: string; input ?: Array;}; /** * @author fenris */ type type_foo_1 = {parameters : type_parameters; reader : type_myreader;}; /** * @author fenris */ type type_foo_2 = lib_lang.type_tree; /** * @author fenris */ type type_myreader = lib_lang.class_reader; /** * @author fenris */ function generate_reader( path : string ) : lib_call.type_promise { return ( lib_call.promise_resolve(undefined) .then( () => lib_file.read_promise(path) ) .then( (content) => lib_call.promise_resolve( JSON.parse(content) ) ) .then( (data) => lib_call.promise_resolve( lib_lang.class_reader.default_raw(data) ) ) ); } /** * @author fenris */ function transform_arguments( args : Array ) : type_parameters { let parameters : type_parameters = {}; // path { const path : string = ( (args.length >= 1) ? args.shift() : null ); if (path == null) { throw (new Error("no path specified")) } else { parameters["path"] = path; } } // input { let input : Array; const input_raw : string = args.join(" "); if (input_raw == "") { throw (new Error("no input given")); } else { input = input_raw.split(""); parameters["input"] = input; } } return parameters; } /** * @author fenris */ function main( args : Array ) : lib_call.type_promise { return ( lib_call.promise_resolve(undefined) .then( () => lib_call.promise_make( (resolve, reject) => { // TODO: outsource reading from stdin to a plankton module let input_raw : string = ""; process.stdin.setEncoding( "utf8" ); process.stdin.on( "readable", () => { let chunk : string; while ((chunk = process.stdin.read()) !== null) { input_raw += chunk; } } ); process.stdin.on( "end", () => { resolve(input_raw); } ); } ) ) .then( (input_raw) => lib_call.promise_resolve( transform_arguments([args[0], input_raw]) ) ) .then( (parameters) => ( generate_reader(parameters.path) .then( (reader) => lib_call.promise_resolve( { "parameters": parameters, "reader": reader, } ) ) ) ) .then( (vars) => { lib_log.level_push(1); const result : type_foo_2 = vars.reader.run(vars.parameters.input); lib_log.level_pop(); if (result == null) { return lib_call.promise_reject(new Error("didn't work")); } else { return lib_call.promise_resolve(result); } } ) .then( (result) => { console.info(JSON.stringify(result, undefined, "\t")); return lib_call.promise_resolve(undefined); } ) ); } /** * @author fenris */ main(process.argv.slice(2)) .then( (output) => { // console.info("--", "done:", output); }, (error) => { console.error("--", "failed:", error); } );