Search code examples
node.jspm2yargs

Trying to setup a service built with YARGS using PM2


So, i have a script to define a service where I can use change streams on mongo. I made it with Yargs so i can pass all the arguments to the application. But when i try yo daemon it with pm2 I´m having hard time passing the command with all the args I wrote. Here is the service yargs setup:

const yargs = require('yargs');
const mongo = require("mongodb").MongoClient;
const fs = require('fs');
const BSON = require('bson');

const argv = yargs
.command(
    'listen', 'Listen to a collection for changes and save it to another',{
        collection :{
            description : 'the collection you want to listen to',
            alias : 'c',
            type: 'string',
        },
        database:{
            description: 'the database you want to listen to',
            alias : 'db',
            type: 'string',
        },
        uri:{
            description: 'the connection string for your mongodb instance',
            alias : 'uri',
            type: 'string',
        },
        logbase:{
            description: 'the database where the changes will be kept',
            alias: 'logdb',
            type: 'string',
        },
        log_collection:{
            description: 'the collection where the changes will be kept',
            alias: 'logc',
            type: 'string',
        }
    })
    .option('verbose', {
        alias : 'v',
        description: 'Writes on the console the information',
        type: 'boolean',
    })
    .help()
    .alias('help', 'h')
    .argv;

if(argv._.includes('listen')){
    if(!argv.uri || !argv.collection || !argv.db || !argv.log_collection || !argv.logbase){
        console.log("Missing parameters!")
        if(!argv.uri) console.log("--uri is missing!")
        if(!argv.collection) console.log("--collection is missing!")
        if(!argv.db) console.log("--db is missing!")
        if(!argv.log_collection) console.log("--log_collection is missing!")
        if(!argv.logbase) console.log("--log_base is missing!")
    }
    else{
        const uri = argv.uri;
        const coll = argv.collection;
        const database = argv.db;
        const logbase = argv.logbase;
        const log_collection = argv.log_collection;
        const verbose = argv.verbose;
        const token = token_reader(logbase, log_collection, verbose)
        if(token){
            //if there is a resume token
            resume_with_token(token ,uri, database, coll, argv.verbose, logbase, log_collection)
        }else { 
            listen (uri, database, coll, argv.verbose, logbase, log_collection)
        }
    }
} else {
    console.log('Commnad not listed')
}

So basicaly i have the folwing line to run in my console:

node .\generic_collection_listerner.js listen -c collection_name --uri "mongo_connection_url" --db database --logbase logbase --log_collection log_collection --verbose

Thanks in advance!


Solution

  • As Ohad Cohen pointed out, the string is defently diferent from the one I tried. I`am using windows, i don´t know exactly what it changes. But the folowing model worked for me:

    pm2 start node -- .\generic_collection_listerner.js listen  -c collection -uri mogo_url -logbase logDb -log_collection changelog -verbose