modules/shared/path.mjs

// module: TRUE

export { JSPath };

/* Used to locate files relative to the script or template being run */

/* Filename of the parent script file (the script actually run in PRIMER/POST) */
// @ts-ignore
const __PARENT_FILENAME__ = arguments[0];

class JSPath {
    /** PRIMER script
     * @type {string} */
    static get PRE() {
        return "Pre";
    }

    /** POST script
     * @type {string} */
    static get POST() {
        return "Post";
    }

    /** REPORTER template
     * @type {string} */
    static get TEMPLATE() {
        return "Template";
    }

    /** Sets the path to the workflow definitions directory
     * @param {string} location Where this is being called from
     * @param {string} [template_dir] REPORTER template directory (only needed when what == JSPath.Template)
     * @example
     * JSPath.SetWorkflowsDirectory(JSPath.POST);
     */
    static SetWorkflowsDirectory(location, template_dir = "") {
        let parent_path = __PARENT_FILENAME__.substring(
            0,
            Math.max(__PARENT_FILENAME__.lastIndexOf("/"), __PARENT_FILENAME__.lastIndexOf("\\"))
        );

        switch (location) {
            case JSPath.PRE:
                /* Assume that PRIMER scripts are at:
                 *     ${workflows_dir}/scripts/automotive_assessments/pre
                 */
                this._workflows_dir = `${parent_path}/../../../`;
                break;
            case JSPath.POST:
                /* Assume that POST scripts are at:
                 *     ${workflows_dir}/scripts/automotive_assessments/post/t-his
                 * or:
                 *     ${workflows_dir}/scripts/automotive_assessments/post/d3plot
                 */
                this._workflows_dir = `${parent_path}/../../../../`;
                break;
            case JSPath.TEMPLATE:
                if (template_dir == "") {
                    ErrorMessage("Argument <template_dir> not defined in JSPath.WorkflowsDirectory");
                    this._workflows_dir = null;
                    break;
                }
                /* Assume that REPORTER templates are at:
                 *     ${workflows_dir}/templates/automotive_assessments
                 */
                this._workflows_dir = `${template_dir}/../../`;
                break;
            default:
                ErrorMessage("Unexpected <location> in JSPath.WorkflowsDirectory");
                this._workflows_dir = null;
                break;
        }
    }

    /** Returns the path to the workflow definitions directory
     * @returns {string}
     * @example
     * const workflows_dir = JSPath.GetWorkflowsDirectory();
     */
    static GetWorkflowsDirectory() {
        if (!this._workflows_dir) {
            ErrorMessage(
                "Workflow directory needs to be set with JSPath.SetWorkflowsDirectory() before it can be retrieved."
            );
        }
        return this._workflows_dir;
    }

    /** Returns the path to the datums definitions directory
     * Assumes that the path is relative to workflow directory
     * Done like this so that we can change it in one place
     * @returns {string}
     * @example
     * const datums_dir = JSPath.GetDatumsDirectory();
     */
    static GetDatumsDirectory() {
        return `${this.GetWorkflowsDirectory()}/scripts/automotive_assessments/datums`;
    }

    /** Returns the path to the occupants definitions directory
     * Assumes that the path is relative to workflow directory
     * Done like this so that we can change it in one place
     * @returns {string}
     * @example
     * const occupants_dir = JSPath.GetOccupantsDirectory();
     */
    static GetOccupantsDirectory() {
        return `${this.GetWorkflowsDirectory()}/scripts/automotive_assessments/occupants`;
    }

    /** Returns the path to the protocols definitions directory
     * Assumes that the path is relative to workflow directory
     * Done like this so that we can change it in one place
     * @returns {string}
     * @example
     * const protocols_dir = JSPath.GetProtocolsDirectory();
     */
    static GetProtocolsDirectory() {
        return `${this.GetWorkflowsDirectory()}/scripts/automotive_assessments/protocols`;
    }
}