|
| 1 | +const{ |
| 2 | +promises:{ readFile} |
| 3 | +}=require("fs"); |
| 4 | +const{ join}=require("path"); |
| 5 | + |
| 6 | +constgetPackageJson=async()=>{ |
| 7 | +// load package.json file |
| 8 | +constpathToPackageJson=join(process.cwd(),"package.json"); |
| 9 | +constpackageJson=awaitreadFile(pathToPackageJson,"utf8").catch( |
| 10 | +console.error |
| 11 | +); |
| 12 | +if(!packageJson){ |
| 13 | +thrownewError("Missing root package.json"); |
| 14 | +} |
| 15 | +// parse as JSON |
| 16 | +constjson=JSON.parse(packageJson); |
| 17 | +if(!json){ |
| 18 | +thrownewError("The package.json content looks invalid"); |
| 19 | +} |
| 20 | +returnjson; |
| 21 | +}; |
| 22 | + |
| 23 | +exports.getPackageJson=getPackageJson; |