run-npm-script.js

34 lines | 894 B Blame History Raw Download
const npm = require("npm");

module.exports = function(scriptName, onSuccess, onError) {

    npm.load(function(err) {
        console.log('Running npm script: "' + scriptName + '"');
        npm.commands.run([scriptName], function(error, data) {

            if (error) {
                console.error('Npm script: "' + scriptName + '" error', error);
                if (data) {
                    console.log(data);
                }

                if (onError) {
                    onError(error);
                }
            }
            else {
                console.log('Npm script: "' + scriptName + '" ran successfully');

                if (onSuccess) {
                    onSuccess(data);
                }
            }

        });

        npm.on('log', function(message) {
            console.log('Npm script: "' + scriptName + '" log', message);
        });
    });

}