md-curcuma
Version:
A Typescript library for transporting and converting markdown and other data.
100 lines (99 loc) • 3.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Json_IO = exports.Json_IO_Writer = exports.Json_IO_Reader = void 0;
const filesystem_1 = require("../core/filesystem");
const observer_1 = require("../core/observer");
const types_1 = require("./types");
const types_2 = require("./types");
class Json_IO_Reader {
constructor(props) {
this.observer_subject = new observer_1.Observer_Subject();
this.props = null;
this.props = props;
}
add_observer(observer, id) {
this.observer_subject.add_observer(observer, id);
}
notify_all(props) {
this.observer_subject.notify_all(props);
}
notify(props) {
this.observer_subject.notify(props);
}
read() {
var file_list = [];
if (filesystem_1.Filesystem.isFolder(this.props.path)) {
file_list = filesystem_1.Filesystem.get_files_list(this.props.path);
}
else if (filesystem_1.Filesystem.isFile(this.props.path)) {
file_list.push(this.props.path);
}
else {
console.log(`not supported: '${this.props.path}'`);
}
file_list.forEach((file) => {
let o_props = new types_1.IO_Observer_Props();
o_props.from = "json-io";
o_props.to = "runner";
o_props.command = "perform-tasks";
let dao_string = filesystem_1.Filesystem.read_file_json(file);
let io_meta = new types_2.IO_Meta();
io_meta.file_list_reader = file_list;
io_meta.file_name_reader = file;
let the_dao = {
data: dao_string,
io_meta: io_meta,
};
o_props.dao = the_dao;
console.log("markdown-io.do_command: perform tasks for", file);
this.notify_all(o_props);
});
let m_props = new types_1.IO_Observer_Props();
m_props.from = "markdown-io";
m_props.to = "runner";
m_props.command = "tasks-finished";
this.notify_all(m_props);
}
}
exports.Json_IO_Reader = Json_IO_Reader;
class Json_IO_Writer {
constructor(props) {
this.observer_subject = new observer_1.Observer_Subject();
this.props = null;
this.collected_data = [];
this.props = props;
}
add_observer(observer, id) {
this.observer_subject.add_observer(observer, id);
}
notify_all(props) {
this.observer_subject.notify_all(props);
}
notify(props) {
this.observer_subject.notify(props);
}
write(dao) {
console.log("### Json_IO.write: ", dao);
if (this.props.append) {
this.collected_data.push(dao.data);
filesystem_1.Filesystem.write_file_json(this.props.path, this.collected_data);
}
else {
filesystem_1.Filesystem.write_file_json(this.props.path, dao.data);
}
}
}
exports.Json_IO_Writer = Json_IO_Writer;
class Json_IO {
constructor(read_props, write_props) {
this.reader = new Json_IO_Reader(read_props);
this.writer = new Json_IO_Writer(write_props);
}
read() {
this.reader.read();
}
write(dao) {
this.writer.write(dao);
}
}
exports.Json_IO = Json_IO;