UNPKG

md-curcuma

Version:

A Typescript library for transporting and converting markdown and other data.

241 lines (240 loc) 8.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Filesystem = void 0; const fs = require("fs"); const path = require("path"); const fsextra = require("fs-extra"); const XLSX = require("xlsx"); class Filesystem { static copy_file(source, target, simulate = false) { if (!simulate) { if (Filesystem.is_file_exist(source)) { if (Filesystem.is_file_exist(target)) { if (Filesystem.is_file_modified(source, target)) { fsextra.copySync(source, target); console.log(`copy_file from ${source} to ${target}`); } else { console.log(`copyjob: source file not modified '${source}'`); } } else { fsextra.copySync(source, target); console.log(`copy_file from ${source} to ${target}`); } } else { console.log(`copyjob: source file doesnt exist '${source}'`); } } else { console.log(`copy_file (simulated) from ${source} to ${target}`); if (Filesystem.is_file_exist(source)) { console.log(`source exist: ${source}.`); } else { console.log(`source exist not: ${source}.`); } if (Filesystem.is_file_exist(target)) { console.log(`target exist: ${target}.`); } else { console.log(`target exist not: ${target}.`); } if (Filesystem.is_file_modified(source, target)) { console.log(`source is modified: ${target}.`); } else { console.log(`source not modified: ${target}.`); } } } static isFolder(my_path) { try { var stat = fs.lstatSync(my_path); return stat.isDirectory(); } catch (e) { return false; } } static isFile(my_path) { try { var stat = fs.lstatSync(my_path); return stat.isFile(); } catch (e) { return false; } } static is_file_exist(my_path) { if (fs.existsSync(my_path)) { return true; } else { return false; } } static is_file_modified(file_source, file_target) { const stats_source = fs.statSync(file_source); const mtime_source = stats_source.mtime; const ctime_source = stats_source.ctime; const stats_target = fs.statSync(file_target); const mtime_target = stats_target.mtime; const ctime_target = stats_target.ctime; console.log(`File source: ${file_source}, target: ${file_target}`); console.log(`File data last modified, source: ${mtime_source}, target: ${mtime_target}`); console.log(`File data last created, source: ${ctime_source}, target: ${ctime_target}`); return mtime_source.getTime() !== ctime_source.getTime(); } static get_file_info(file) { return fs.statSync(file); } static get_filename_base_from(my_path_filename) { return path.basename(my_path_filename); } static get_filename_ext_from(my_path_filename) { return path.extname(my_path_filename); } static get_filename_name_from(my_path_filename) { return path.parse(my_path_filename).name; } static get_path_from(my_path_filename) { return path.parse(my_path_filename).dir; } static concat_path_filename(my_path, my_filename) { if (my_path.endsWith(path.sep)) { return my_path + my_filename; } else { return my_path + path.sep + my_filename; } } static ensure_path(my_path, simulate = false) { if (!simulate) { fsextra.ensureDirSync(my_path); console.log(`ensure_path exist: '${my_path}'`); } else { const folder_arr = Filesystem.get_path_parts(my_path); console.log(`ensure_path (simulated): '${my_path}'`); console.log("ensure_path (simulated):", folder_arr); var part = ""; for (const folder of folder_arr) { part = (part === "" ? "" : part + path.sep) + folder; if (folder !== "") { if (!fs.existsSync(part)) { console.log(`ensure_path (simulated): missing: '${part}'`); } else { console.log(`ensure_path (simulated): exists: '${part}'`); } } } } } static get_path_parts(my_path) { return my_path.split(path.sep); } static get_path_depth(my_path) { return Filesystem.get_path_parts(my_path).length; } static get_files_list(dir, files = []) { dir = dir.endsWith(path.sep) ? dir : dir + path.sep; const fileList = fs.readdirSync(dir, { withFileTypes: true }); for (const file of fileList) { const name = `${dir}${file.name}`; if (fs.statSync(name).isDirectory()) { this.get_files_list(name, files); } else { files.push(name); } } return files; } static read_file_txt(file) { try { var md_array = fs.readFileSync(file).toString(); } catch (err) { throw err; } return md_array; } static read_file_xlsx(file) { let wb = null; try { wb = XLSX.read(fs.readFileSync(file, "binary"), { type: "binary", }); } catch (err) { throw err; } return wb; } static read_file_buffer(file) { try { var md_buffer = fs.readFileSync(file); } catch (err) { throw err; } return md_buffer; } static write_file_xlsx(writePath, content) { try { console.log(`Write XLSX File ${writePath}`); XLSX.writeFile(content, writePath, { compression: true }); } catch (error) { console.log(`An error has occurred, writing ${writePath}`, error); } } static write_file_txt(writePath, content) { try { console.log(`Write File ${writePath}`); fs.writeFileSync(writePath, content, "utf8"); } catch (error) { console.log(`An error has occurred, writing ${writePath}`, error); } } static read_file_json(file) { return JSON.parse(fs.readFileSync(file, "utf8")); } static write_file_json(file, json_object) { fs.writeFileSync(file, JSON.stringify(json_object, null, 4)); } static write_my_file(source_file, job_parameter, workbook, do_not_write_file, doWriting) { const filename = Filesystem.get_filename_base_from(source_file); const path_target_filename = Filesystem.concat_path_filename(job_parameter.path, filename); Filesystem.ensure_path(job_parameter.path, job_parameter.simulate); if (!job_parameter.simulate) { if (!do_not_write_file) { if (Filesystem.is_file_exist(path_target_filename)) { if (Filesystem.is_file_modified(source_file, path_target_filename)) { console.log("file does exist, and is modified (compared by modified-date): Write it."); doWriting(path_target_filename, workbook); } else { console.log("file does exist, but is not modified: Skip writing."); } } else { console.log("file does not exist: Write it."); doWriting(path_target_filename, workbook); } } } else { console.log("###########################"); console.log(source_file); console.log(path_target_filename); console.log(`modified: ${Filesystem.is_file_modified(source_file, path_target_filename)}`); console.log("###########################"); } } } exports.Filesystem = Filesystem;