|
| 1 | +const{ |
| 2 | +promises: { readFile, readdir } |
| 3 | +}=require("fs"); |
| 4 | +const{ join }=require("path"); |
| 5 | + |
| 6 | +constgetPackageJson=async(dir=process.cwd())=>{ |
| 7 | +// load package.json file |
| 8 | +constpathToPackageJson=join(dir,"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; |
| 24 | + |
| 25 | +constversionMatch=(current,expected)=>{ |
| 26 | +letcurrentSemver=current; |
| 27 | +if(["~","^"].includes(current[0])){ |
| 28 | +currentSemver=current.substring(1); |
| 29 | +} |
| 30 | +returncurrentSemver===expected; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * isModuleInstalled |
| 35 | + * @param { name, type } params |
| 36 | + * "name" - the name of the dependency |
| 37 | + * "type" - "dependency", "devDependency", "peerDependency" |
| 38 | + * @returns boolean |
| 39 | + */ |
| 40 | +constisModuleInstalled=async({ name, type, version })=>{ |
| 41 | +// 1. load package.json file |
| 42 | +constjson=awaitgetPackageJson(); |
| 43 | + |
| 44 | +// 2. verify package lists dependency by type |
| 45 | +letinstallCommand; |
| 46 | +lethasDependency; |
| 47 | +letcurrentVersion; |
| 48 | + |
| 49 | +switch(type){ |
| 50 | +case"dependency": |
| 51 | +installCommand="--save"; |
| 52 | +hasDependency=!!json.dependencies&&json.dependencies[name]; |
| 53 | +currentVersion=json.dependencies[name]; |
| 54 | +break; |
| 55 | +case"devDependency": |
| 56 | +installCommand="--save-dev"; |
| 57 | +hasDependency=!!json.devDependencies&&json.devDependencies[name]; |
| 58 | +currentVersion=json.devDependencies[name]; |
| 59 | +break; |
| 60 | +case"peerDependency": |
| 61 | +thrownewError("Peer dependencies unsupported"); |
| 62 | +default: |
| 63 | +thrownewError("Unsupported packaged type"); |
| 64 | +} |
| 65 | + |
| 66 | +if(!hasDependency){ |
| 67 | +thrownewError(`Run "npm install ${installCommand}${name}"`); |
| 68 | +} |
| 69 | + |
| 70 | +// 3. if version, check dependency version |
| 71 | +if(version&&!versionMatch(currentVersion,version)){ |
| 72 | +thrownewError( |
| 73 | +`Dependency ${name} version ${currentVersion} does not match expected ${version}` |
| 74 | +); |
| 75 | +} |
| 76 | + |
| 77 | +// 4. verify node_module installed |
| 78 | +constpathToNodeModule=join(process.cwd(),"node_modules",name); |
| 79 | +consthasNodeModules=awaitreaddir(pathToNodeModule).catch(()=>{ |
| 80 | +thrownewError('Missing node_modules. Run "npm install"'); |
| 81 | +}); |
| 82 | +if(!hasNodeModules){ |
| 83 | +thrownewError('Missing node_modules. Run "npm install"'); |
| 84 | +} |
| 85 | + |
| 86 | +// 5. if version, has installed node_module version |
| 87 | +if(version){ |
| 88 | +constnodeModulePackageJson=awaitgetPackageJson(pathToNodeModule); |
| 89 | +if(!versionMatch(nodeModulePackageJson.version,version)){ |
| 90 | +thrownewError( |
| 91 | +`Dependency ${name} version ${version} is not yet installed. Run "npm install"` |
| 92 | +); |
| 93 | +} |
| 94 | +} |
| 95 | + |
| 96 | +returntrue; |
| 97 | +}; |
| 98 | + |
| 99 | +exports.isModuleInstalled=isModuleInstalled; |
| 100 | + |
| 101 | +// created because assert.doesNotThrow not working predictably with async fns |
| 102 | +constdoesNotThrow=asyncfn=>{ |
| 103 | +letresult=true; |
| 104 | +try{ |
| 105 | +awaitfn(); |
| 106 | +}catch(error){ |
| 107 | +console.error(error); |
| 108 | +result=false; |
| 109 | +} |
| 110 | +returnresult; |
| 111 | +}; |
| 112 | + |
| 113 | +exports.doesNotThrow=doesNotThrow; |
0 commit comments