This is an automated email from the ASF dual-hosted git repository.

lukeroy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-runtime-nodejs.git


The following commit(s) were added to refs/heads/master by this push:
     new baa4185  Add functionality to install modules during init (#233)
baa4185 is described below

commit baa4185e13590261ce01f2584b29ed963101fda6
Author: Luke-Roy-IBM <[email protected]>
AuthorDate: Fri Mar 10 12:13:34 2023 +0100

    Add functionality to install modules during init (#233)
    
    * Add functionality to install modules during init
    
    Install modules specified in package.json during initialisation as a 
alternative to packageing them.
    Detect if the node_modules directory is  missing and installes the node 
modules.
    
    * check if package.json has dependencies and install if required
    
    * remove trailing whitespace
    
    * read package.json correctly as object
---
 core/nodejsActionBase/runner.js | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js
index 46355cc..6b5759c 100644
--- a/core/nodejsActionBase/runner.js
+++ b/core/nodejsActionBase/runner.js
@@ -46,6 +46,17 @@ function initializeActionHandler(message) {
                     return Promise.reject('Zipped actions must contain either 
package.json or index.js at the root.');
                 }
 
+                // 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);

Reply via email to