function initializeActionHandler()

in core/nodejsActionBase/runner.js [27:94]


function initializeActionHandler(message) {
    if (message.binary) {
        // The code is a base64-encoded zip file.
        ext = detectFileType(message.code)
        if (ext == 'unsupported'){
            return Promise.reject("There was an error uncompressing the action archive. The file type is unsupported");
        }
        return extractInTmpDir(message.code)
            .then(moduleDir => {
                let parts = splitMainHandler(message.main);
                if (parts === undefined) {
                    // message.main is guaranteed to not be empty but be defensive anyway
                    return Promise.reject('Name of main function is not valid.');
                }

                // If there is only one property in the "main" handler, it is the function name
                // and the module name is specified either from package.json or assumed to be index.js.
                let [index, main] = parts;

                // Set the executable directory to the project dir.
                process.chdir(moduleDir);

                if (index === undefined && !fs.existsSync('package.json') && !fs.existsSync('index.js')) {
                    return Promise.reject('Zipped actions must contain either package.json or index.js at the root.');
                }


                // check environment variable OW_ENABLE_INIT_INSTALL if we should do a 'npm install' to load not yet installed modules.
                let enableInitInstall= !process.env.OW_ENABLE_INIT_INSTALL ? 'true' : process.env.OW_ENABLE_INIT_INSTALL;

                if (enableInitInstall === 'true') {
                    // install npm modules during init if source code zip doesn´t containt them
                    // check if package.json exists and node_modules don`t
                    if (fs.existsSync('package.json') && !fs.existsSync('./node_modules/')) {
                        var package_json = JSON.parse(fs.readFileSync('package.json', 'utf8'));
                        if (package_json.hasOwnProperty('dependencies')) {
                            if (Object.keys(package_json.dependencies).length > 0) {
                                exec("npm install")
                            }
                        }
                    }
                }



                //  The module to require.
                let whatToRequire = index !== undefined ? path.join(moduleDir, index) : moduleDir;
                let handler = eval('require("' + whatToRequire + '").' + main);
                return assertMainIsFunction(handler, message.main);
            })
            .catch(error => Promise.reject(error));
    } else try {
        let handler = eval(
          `(function(){
               ${message.code}
               try {
                 return ${message.main}
               } catch (e) {
                 if (e.name === 'ReferenceError') {
                    return module.exports.${message.main} || exports.${message.main}
                 } else throw e
               }
           })()`);
        return assertMainIsFunction(handler, message.main);
    } catch (e) {
        return Promise.reject(e);
    }
}