I have a .log
file as input and I want to covert this log file to .json
format and then write to an output file.
For doing this operation I have used yargs
package.
Here is the code I am trying:
import yargs from 'yargs'
import fs from 'fs'
// Check if the input file exist!
const checkFileExists = async(filepath: fs.PathLike) => {
return new Promise((resolve, reject) => {
fs.access(filepath, fs.constants.F_OK, (error) => {
resolve(!error)
})
})
}
// Read and Write operation
const filterLogs = (inputPath: any, outputPath: any) => {
// Read the input file
// Make filterations
// convert to json
// create outfile if not EXIST!
// write to outputfile
}
yargs
.options({
input: {
alias: 'i',
describe: 'input file path',
demandOption: true,
type: 'string',
},
output: {
alias: 'o',
describe: 'output file path',
demandOption: true,
type: 'string',
},
})
.coerce('input', async function(arg) {
return await checkFileExists(arg)
})
.help().argv
console.log(yargs.argv)
Running as:
tsc && node parser.js --input="./apps.log" --output=./error.json
How would I be able to run my functions to check if file exists then make other filterations and write to a output file?