Recently, I created a project and published it on npm
. It needs a JSON configuration file to work properly, which I want to generate as an sample file after npm install
(postinstall script) on the user's project, and then, when a specific command is executed, i want to request that same configuration file from the user's project with the necessary changes made by him.
I'm having issues to get the root folder on the parent project which calls mine with npm install
. I tried the following, but the process.mainModule.filename
is attached to my package, not to the parent.
//Tries to create json file on the user project's root folder, but it creates on my package's one.
fs.writeFile(path.resolve(path.parse(process.mainModule.filename).dir, "sp-publish-config.json"), JSON.stringify(jsonExample, null, 2), function(err) {
if (err) {
return console.log(err);
}
console.log("sp-publish-config.json created.");
});
I also tried to use __dirname
, and all sort of file utils, but no success. It always create on my project.
Is it possible to create a file on the root of the parent project?