Yeoman Generator - How to Parse Package.json of Generator Project


Yeoman Generator - How to Parse Package.json of Generator Project



I have the following, standard folder structure within my generator. The task I am current struggling with is I currently have a templated _package.json that I write to disk for the main generation. There is one variable in the written package.json I would like to hydrate which is the actual version of the generator project. Basically, I want to be able to determine via the new generated api project's package.json which version of the generator was used to construct the project, so that I can target older versions and mark them for updates.



I have tried to accomplish this in both various generator steps like prompting.js and writing.js and via the following code:




var fs = require('fs');

let pkgGeneratorAPIVersion = JSON.parse(fs.readFileSync('../../package.json', 'utf8'));



but it isn't working. I keep getting the following error:




Error: ENOENT: no such file or directory, open 'package.json'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at module.exports.prompt.answers



I've tried various relative paths to no avail. Is there a better way to parse the json file? Is my relative path off? Any help is greatly appreciated.



Folder structure




1 Answer
1



I ended up fixing it with the following:




var pkgJSON = require('../../package.json');
this.answers.corePkgGeneratorAPI = JSON.parse(JSON.stringify(pkgJSON)).version;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?