I want to know how to take the input and output file path via CLI and use those paths in the npm package.
I want to make a command something like this.
npx convert INPUT_FILE_PATH -o OUTPUT_FILE_PATH
and then I want to use these paths in my package to access files at these paths so that I can read the file and save the output to the output file that I received from the cli.
Same answer as before, you access the instructions passed to the script via process.argv.
Tbh, I would just use a library for writing this as your needs become more complex, it’s much easier (eg commander or vorpal, and there are a load more)
npx convert input.json -o output.yml
From the above command, I should access the arguments like this process.argv[2] and process.argv[4]
and then pass these values to my npm package but the issue how to access these files path just by knowing a file name.
My doubt is how should I access inputpath and outputpath so that I can read it from my package using fs.
I am very new to this.
If there is any tutorial to understand this concept, please share with me
UPDATE:
when I do console.log(inputpath, outputpath), it returns this
/Users/username/Documents/postman-to-openapi-cli/input.json /Users/username/Documents/postman-to-openapi-cli/output.yml
postman-to-openapi-cli is my package folder name, it’s searching for the file in my package directory, but my file is located somewhere else.
So that input/outputFilename, that can be any path. __dirname is a convenience variable Node gives you that is the directory the script is running in, so the filename can be any path from there – npx convert path/to/the/input.json -o path/to/the/output.yml
Note that one of the benefits of using a CLI library like the ones mentioned is that they normally provide path autocompletion in the terminal when you’re using a program built with them
Let say someone run the command from this folder /Users/username/documents
and the input and output file is stored in the downloads like this /Users/username/downloads/input.json /Users/username/downloads/output.yaml
then user should run this command npx convert /Users/username/downloads/input.json -o /Users/username/downloads/output.yaml to convert the input file to output file.
I mean they need to give an absolute path where is the file saved or they need to give the path of the input and output file from the current directory?
I don’t know why I am not able to understand how to access those input/output files.
Can you please give me an example?
Let say you have an input.json file in the Downloads folder (/Users/username/downloads/input/input.json) and you need to put the output after conversion in the output.json file that is located in /Users/username/downloads/output/output.json.
And you open a terminal from the documents folder (/Users/username/documents)
How should you pass the both file path as an argument in the command(npx convert INPUT_FILE_PATH -o OUTPUT_FILE_PATH).
This is my code for cli.js file that runs when someone run the package from the cli.
When I run this command npx convert downloads/Webhook.postman_collection.json -o downloads/output.json from the /Users/username then it return this output
why this append /node_modules/postman-to-openapi-cli in my path, it should be this Input file path:/Users/username/downloads/Webhook.postman_collection.json
Yes you’re not doing anything except printing out the paths. You’ve written a program that takes two paths to files and returns the fully resolved paths. What did you think this was going to do?
Hello there,
Now, Everything works fine
and I am using a commander library,
I just want to know am I doing it correctly?
const program = require('commander');
const ver = require('./package.json')
program
.version(ver.version, '-v, --version')
.option('-s, --source <source>', 'Relative path of the Postman Collection v2.0/v2.1')
.option('-o, --output <output>', 'Relative path of the OpenAPI')
.parse(process.argv);
console.log(`Input: ${process.argv[3]} \nOutput: ${process.argv[5]}`)