file_helper.mjs

export { find_all_json_files, find_lsdyna_files, WriteJSON, ReadJSON };

/**
 * returns an array of JSON files in and below root directory path
 * @param {string} root path to start recursive seach for files
 * @param {string[]} [file_list=[]] the list of files found so far (iterative call)
 * @returns {string[]}
 */
function find_all_json_files(root, file_list = []) {
    if (Unix()) var slash = "/";
    else var slash = "\\";

    //let file_list = [];

    for (let file_OR_dir of File.FindFiles(root, File.DIRECTORY | File.FILE)) {
        let path = `${root}${slash}${file_OR_dir}`;
        if (File.IsFile(path)) {
            //Message(path);
            if (/\.json$/i.test(file_OR_dir)) file_list.push(path);
        } else if (File.IsDirectory(path)) file_list.concat(find_all_json_files(path, file_list));
    }

    return file_list;
}

/**
 * Searches a directory for filenames matching either Arup or LSTC filename conventions.
 * Searches for files in directory <dir> of type <file_type>, possibly containing
   <job_name>, and returns the first match in the priority lists below.
   @param {string} dir Directory to search
   @param {string} job_name Root filename to search for
   @param {string} file_type File type to search for (can be "PRIMER", "D3PLOT", "T/HIS", "OTF")
   @returns {?string}
   @example
   let absolute_filename = find_lsdyna_files("C:/my/results/directory", "job_001", "D3PLOT");
 */
function find_lsdyna_files(dir, job_name, file_type) {
    let filters = [];
    let filename;

    switch (file_type) {
        case "PRIMER":
            filters = [
                new RegExp("^" + job_name + ".key$"),
                new RegExp("^" + job_name + ".k$"),
                new RegExp("^" + job_name + ".*.key$"),
                new RegExp("^" + job_name + ".*.k$"),
                new RegExp(".*.key$"),
                new RegExp(".*.k$")
            ];

            break;

        case "D3PLOT":
            filters = [
                new RegExp("^" + job_name + ".ptf$"),
                new RegExp("^" + job_name + ".d3plot$"),
                new RegExp("^d3plot$"),
                new RegExp("^" + job_name + ".*.ptf$"),
                new RegExp("^" + job_name + ".*.d3plot$"),
                new RegExp(".*.ptf$"),
                new RegExp(".*.d3plot$")
            ];
            break;

        case "T/HIS":
            filters = [
                new RegExp("^" + job_name + ".thf$"),
                new RegExp("^" + job_name + ".d3thdt$"),
                new RegExp("^d3thdt$"),
                new RegExp("^" + job_name + ".*.thf$"),
                new RegExp("^" + job_name + ".*.d3thdt$"),
                new RegExp(".*.thf$"),
                new RegExp(".*.d3thdt$"),
                new RegExp("^" + job_name + ".binout.*$"),
                new RegExp("^binout.*$"),
                new RegExp("^" + job_name + ".*.binout.*$"),
                new RegExp(".*.binout.*$")
            ];
            break;

        case "OTF":
            filters = [
                new RegExp("^" + job_name + ".otf$"),
                new RegExp("^" + job_name + ".d3hsp$"),
                new RegExp("^d3hsp$"),
                new RegExp("^" + job_name + ".*.otf$"),
                new RegExp("^" + job_name + ".*.d3hsp$"),
                new RegExp(".*.otf$"),
                new RegExp(".*.d3hsp$")
            ];
            break;

        default:
            ErrorMessage(`Unexpected <file_type = "${file_type}" in function find_lsdyna_files.`);
            Exit();
            break;
    }

    let filestore = [];
    for (let filter of filters) {
        let filelist = File.FindFiles(dir);
        for (let file of filelist) {
            if (filter.test(file) == true) {
                filestore.push(file);
            }
        }
        if (filestore.length > 0) {
            filestore.sort();
            /* Pick first matching file and strip off rest of path */
            filename = filestore[0].substring(
                Math.max(filestore[0].lastIndexOf("/"), filestore[0].lastIndexOf("\\")) + 1
            );
            break;
        }
    }

    if (filestore.length == 0) return null;

    let absolute_filename = dir + "/" + filename;

    return absolute_filename;
}

/**
 * write json string representation (with 4 space indent)
 * @param {string} filename
 * @param {any} object
 */
function WriteJSON(filename, object) {
    /* Write the job data as a JSON file that will be picked up by T/HIS */
    let json_str = JSON.stringify(object, null, 4);
    let f_json = new File(filename, File.WRITE);
    f_json.Writeln(json_str);
    f_json.Close();
}

/**
 * read json string representation (with 4 space indent)
 * @param {string} filename
 * @return {object}
 */
function ReadJSON(filename) {
    /* Write the job data as a JSON file that will be picked up by T/HIS */
    let f_json = new File(filename, File.READ);
    let json = JSON.parse(f_json.ReadAll());
    f_json.Close();
    return json;
}