NodeJs Ejemplo de scripts de llamadas de línea de comandos de Linux

Esto muestra un enfoque para usar NodeJs para crear scripts de llamadas de línea de comandos de Linux. El objetivo es tratar de utilizar un enfoque de programación más funcional. Se usa una cadena de promesas. Cada llamada a la línea de comandos de Linux es asíncrona, por lo que las promesas de JavaScript proporcionan una forma limpia de gestionar esto. La biblioteca JavaScript lodash se usa para proporcionar funcionalidad. ` 'use strict'; module.exports.fileProcessor = (() => { const _ = require('lodash'); const util = require('util') const exec = require('child_process').exec; var child;

this._e = (cmd) => { return new Promise((resolve, reject) => { child = exec(cmd, (error, stdout, stderr) =>{ resolve(stdout); if (error !== null || stderr != null) { console.log("---errors: ") console.log("error: " + error); console.log("stderr: " + stderr) reject(stderr); } }); }); }

const helpers = require ('../helpers.js');

this.run = (filePath, newFileExtension) => {

const thenFn = (fileNames) => {

    var zipFileNames = _.map(fileNames, (fileName) => {
        var newFileName = fileName;
        if (newFileExtension != undefined) {
            var name = fileName.split(".")[0];
            newFileName = name + newFileExtension;
        }
        return newFileName;
    });

    const copyPromises = _.map(_.zip(fileNames, zipFileNames), (pair) =>{
        return new Promise((resolve, reject) => {
            const fileName = pair[0];
            const newFileName = pair[1];
            const cmd = "cp '" + fileName + "' '" + newFileName + "'";
            this._e(cmd)
            .then((e)=> {
                resolve((e)=> console.log("copied " + e));
            }); 
        });
    });

    var lastPromise = _.last(copyPromises);

    const mkDirPromises = _.map(fileNames, (fileName) =>{
        return new Promise((resolve) => {
            var name = fileName.split(".")[0];
            const cmd = "mkdir '" + name + "'";
            this._e(cmd)
            .then((e)=> {
                resolve((e)=> console.log("mkdir " + e));
            });
        });
    });

    const moveFilePromises = _.map(_.zip(fileNames, zipFileNames), (pair) =>{
        return new Promise((resolve, reject) => {
            const dirName = pair[0].split(".")[0];
            const zipFileName = pair[1];
            const cmd = "mv '" + zipFileName + "' '" + dirName + "'";
            console.log(cmd);
            this._e(cmd)
            .then((e)=> {
                resolve((e)=> console.log("moved " + e));
            }); 
        });
    });

    const extractFilePromises = _.map(fileNames, (fileName) =>{
        return new Promise((resolve, reject) => {
            const dirName = fileName.split(".")[0];
            const baseFileName = _.last(dirName.split("/"));
            const zipFileName = baseFileName + ".zip";
            const cmd = "cd '" + dirName + "'; " + " tar -xvf '" + zipFileName + "'";
            console.log(cmd);
            this._e(cmd)
            .then((e)=> {
                resolve((e)=> console.log("extracted " + e));
            }); 
        });
    });

    lastPromise.then(
     (someVar) => {
        const lastmkDirPromise = _.last(mkDirPromises);
        lastmkDirPromise.then((output) => {
            const lastmoveFilePromise = _.last(moveFilePromises);
            lastmoveFilePromise.then((output) => {
                const lastextractFilePromise = _.last(extractFilePromises);
                lastextractFilePromise.then();
            });
        });
     }   
    );      

};   

helpers.getFiles(filePath)(thenFn);

} })(); La clase helpers.js: module.exports.helpers = (() => { this.getFiles = (path) => { const _ = require('lodash'); const ls = require('list-directory-contents');

return (thenFn) => {
    return new Promise((resolve, reject)  => {
        ls(path, (err, tree) => {
            var files = [];
            _.forEach(tree, (value) => {
                files.push(value);
            });
            resolve(files);
        });

    }).then(thenFn);
}

}; })(); ` Ejemplo de uso de la línea de comandos REPL de NodeJs: var fp1 = require('./fileProcessor.js'); fp1.run("/some/directory/path",".zip");