Friday, 26 May 2017

Run node script from commandline

First things first, Trello API manipulatio. I'm a newbie and this is, obviously, my first time trying to execute this kind of process. I've made some research abou running a script/node in commandline with arguments and calling functions exported from a index.js and pass the parameters. I have key and token, and I'm clueless... Command attempts:

  • node -e 'require("./index.js").listUsersTasksStatus'
  • run-func index.js [param], run-func is a node module
  • node index.js [args]

    var Trello = require("trello");
    var async = require("async");
    var trello = new Trello(
        "MY-KEY",
        "MY-TOKEN");
    
    module.exports.listUsersTasksStatus = (board, lists, cb) => {
        let result;
        let usersData = {};
        let tempData = [];
    
        // Baixa todos os usuários do Board
        trello.getBoardMembers(board).then((data) => {
            // Loop por usuários
            for (let user of data) {
                usersData[user.username] = {
                    id: user.id,
                    name: user.fullName,
                    tag: '@'.concat(user.username),
                    tasks: {}
                }
            }
    
        }).then(() => {
            // Loop Async pelas Listas
            async.each(lists, (list, cb) => {
                // Busca API pelos cards da lista
                trello.getCardsOnList(list).then((cards) => {
                    // Loop Async pelos Cards
                    async.each(cards, (card, cb) => {
                        // Busca API pelos Checklists
                        trello.getChecklistsOnCard(card.id).then((checklists) => {
                            // Loop pelos Checklists
                            for (checklist of checklists) {
                                // Loop por Tasks
                                for (task of checklist.checkItems) {
                                    let taskName = task.name;
                                    let searchRE = /@([0-9a-zA-Z_]{1,15})/ig;
                                    // Busca usuário taggeado
                                    while ((users = searchRE.exec(taskName)) !== null) {
                                        // Adiciona dados das tarefas
                                        usersData[users[1]].tasks[task.id] = {
                                            descricao: taskName.replace(/@([0-9a-zA-Z_]{1,15})/, '').trim(),
                                            feita: (task.state == 'complete')
                                        }
                                    };
                                }
                            }
                            cb(null);
                        });
                    }, (err) => {
                        cb(null);
                    })
                })
            }, (err) => {
                for (key in usersData) {
                    let user = usersData[key];
    
                    let tasksTotal = Object.keys(user.tasks).length;
                    let tasksDone = 0;
                    Object.values(user.tasks).map((task) => {
                        if (task.feita) tasksDone++;
                    })
    
                    // Checa quantidade de Tarefas
                    if (tasksTotal > 0) {
                        console.log(`> ${user.name} completou ${Math.round((tasksDone/tasksTotal)*100,0)}% das tarefas (${tasksDone}/${tasksTotal})`)
                    }
                }
                //cb(null,usersData);
            })
        })
    }
    
    


via Rick Stanley

No comments:

Post a Comment