Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import registryUrl from 'registry-url'; import keywords from '../enums/keywords.js'; import { getPackageNameAndScope } from './private/index.js'; import chalk from 'chalk'; import fetch from 'node-fetch'; import getYarnInfo from './getYarnInfo.js'; import NOT_FOUND from '../constants/notFound.js'; async function validateMode(packageName, version) { return validate(packageName, version, keywords.MODE); } async function validateExtension(packageName, version) { return validate(packageName, version, keywords.EXTENSION); } async function validateModeYarnInfo(packageName) { return validateYarnInfo(packageName, keywords.MODE); } async function validateExtensionYarnInfo(packageName) { return validateYarnInfo(packageName, keywords.EXTENSION); } function validateYarnInfo(packageName, keyword) { return new Promise(async (resolve, reject) => { function rejectIfNotFound() { const error = new Error(`${chalk.red.bold('Error')} extension ${packageName} not installed`); reject(error); } const packageInfo = await getYarnInfo(packageName).catch(() => { rejectIfNotFound(); }); if (!packageInfo) { rejectIfNotFound(); return; } const { keywords } = packageInfo; const isValid = keywords && keywords.includes(keyword); if (isValid) { resolve(true); } else { const error = new Error( `${chalk.red.bold('Error')} package ${packageName} is not an ${keyword}` ); reject(error); } }); } function getVersion(json, version) { const versions = Object.keys(json.versions); // if no version is defined get the latest if (version === undefined) { return json['dist-tags'].latest; } // Get and validate version if it is explicitly defined const allowMinorVersionUpgrade = version.startsWith('^'); if (!allowMinorVersionUpgrade) { const isValidVersion = versions.includes(version); if (!isValidVersion) { return; } return version; } // Choose version based on the newer minor/patch versions const [majorVersion] = version .split('^')[1] .split('.') .map(v => parseInt(v)); // Find the version that matches the major version, but is the latest minor version versions .filter(version => parseInt(version.split('.')[0]) === majorVersion) .sort((a, b) => { const [majorA, minorA, patchA] = a.split('.').map(v => parseInt(v)); const [majorB, minorB, patchB] = b.split('.').map(v => parseInt(v)); if (majorA === majorB) { if (minorA === minorB) { return patchB - patchA; } return minorB - minorA; } return majorB - majorA; }); if (versions.length === 0) { return; } return versions[0]; } function validate(packageName, version, keyword) { return new Promise(async (resolve, reject) => { const { scope } = getPackageNameAndScope(packageName); // Gets the registry of the package. Scoped packages may not be using the global default. const registryUrlOfPackage = registryUrl(scope); let options = {}; if (process.env.NPM_TOKEN) { options['headers'] = { Authorization: `Bearer ${process.env.NPM_TOKEN}`, }; } const response = await fetch(`${registryUrlOfPackage}${packageName}`, options); const json = await response.json(); if (json.error && json.error === NOT_FOUND) { const error = new Error(`${chalk.red.bold('Error')} package ${packageName} not found`); reject(error); return; } const packageVersion = getVersion(json, version); if (packageVersion) { const versionedJson = json.versions[packageVersion]; const keywords = versionedJson.keywords; const isValid = keywords && keywords.includes(keyword); if (isValid) { resolve(true); } else { const error = new Error( `${chalk.red.bold('Error')} package ${packageName} is not an ${keyword}` ); reject(error); } } else { // Particular version undefined const error = new Error( `${chalk.red.bold('Error')} version ${packageVersion} of package ${packageName} not found` ); reject(error); } }); } export { validateMode, validateExtension, validateModeYarnInfo, validateExtensionYarnInfo }; |