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