mountAction: function()

in src/kinds/nodejs/nodejs.js [56:87]


    mountAction: function(invoker) {
        // bridge that mounts local source path

        if (fs.statSync(invoker.sourcePath).isDirectory()) {
            throw new Error(`[source-path] or --build-path must point to a source file, it cannot be a folder: '${invoker.sourcePath}'`);
        }

        // test if code uses commonjs require()
        const isCommonJS = /(\s|=)require\(\s*['"`]/.test(fs.readFileSync(invoker.sourcePath));

        // is it a require() based action or a plain JS one?
        const bridgeSource = isCommonJS ? "mount-require.js" : "mount-plain.js";

        let code = fs.readFileSync(`${__dirname}/${bridgeSource}`, {encoding: 'utf8'});
        let sourceFile = invoker.sourceFile.toString();

        // On Windows, the path set on the cli would typically be in windows format,
        // but the nodejs container is Unix and requires Unix paths
        if (path.sep !== path.posix.sep) {
            sourceFile = sourceFile.split(path.sep).join(path.posix.sep);
        }

        code = code.replace("$$main$$",        invoker.main || "main");
        code = code.replace("$$sourcePath$$", `${CODE_MOUNT}/${sourceFile}`);
        code = code.replace("$$sourceFile$$",  sourceFile);

        return {
            binary: false,
            main:   "main",
            code:   code,
        };
    }