run-npm-script.js

31 lines | 809 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 (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);
        });
    });

}