type2/source/type2.ts

202 lines
4.1 KiB
TypeScript
Raw Normal View History

/*
This file is part of »type2«.
2016-12-26 17:56:02 +01:00
Copyright 2016-2018 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
<info@greenscale.de>
»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 <http://www.gnu.org/licenses/>.
*/
const _fs = require("fs");
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
type type_parameters = {path ?: string; input ?: Array<char>;};
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
type type_foo_1 = {parameters : type_parameters; reader : type_myreader;};
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
type type_foo_2 = lib_lang.type_tree<lib_lang.type_terminal_default>;
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
type type_myreader = lib_lang.class_reader<char, lib_lang.type_terminal_default, string, type_foo_2>;
/**
* @author fenris
*/
function generate_reader(
path : string
) : lib_call.type_promise<type_myreader, Error>
{
return (
lib_call.promise_resolve<void, Error>(undefined)
.then<string/*, Error*/>(
() => lib_file.read_promise(path)
)
.then<any/*, Error*/>(
(content) => lib_call.promise_resolve<any, Error>(
JSON.parse(content)
2016-12-26 17:56:02 +01:00
)
)
.then<type_myreader/*, Error*/>(
(data) => lib_call.promise_resolve<type_myreader, Error>(
lib_lang.class_reader.default_raw(data)
2016-12-26 17:56:02 +01:00
)
)
);
}
/**
* @author fenris
*/
function transform_arguments(
args : Array<string>
) : 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<char>;
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<string>
) : lib_call.type_promise<void, Error>
{
return (
lib_call.promise_resolve<void, Error>(undefined)
.then<string/*, Error*/>(
() => lib_call.promise_make<string, Error>(
(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;
2016-12-26 17:56:02 +01:00
}
}
);
process.stdin.on(
"end",
() => {
resolve(input_raw);
}
);
}
2016-12-26 17:56:02 +01:00
)
)
.then<type_parameters/*, Error*/>(
(input_raw) => lib_call.promise_resolve<type_parameters, Error>(
transform_arguments([args[0], input_raw])
)
)
.then<type_foo_1/*, Error*/>(
(parameters) => (
generate_reader(parameters.path)
.then<type_foo_1/*, Error*/>(
(reader) => lib_call.promise_resolve<type_foo_1, Error>(
{
"parameters": parameters,
"reader": reader,
}
2016-12-26 17:56:02 +01:00
)
)
2016-12-26 17:56:02 +01:00
)
)
.then<type_foo_2/*, Error*/>(
(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<type_foo_2, Error>(new Error("didn't work"));
}
else {
return lib_call.promise_resolve<type_foo_2, Error>(result);
}
}
)
.then<void/*, Error*/>(
(result) => {
console.info(JSON.stringify(result, undefined, "\t"));
return lib_call.promise_resolve<void, Error>(undefined);
}
)
);
}
2016-12-26 17:56:02 +01:00
/**
* @author fenris
*/
main(process.argv.slice(2))
.then(
(output) => {
// console.info("--", "done:", output);
},
(error) => {
console.error("--", "failed:", error);
}
);
2016-12-26 17:56:02 +01:00