From ea82fc5c213c1ca08595d98c9fece1bbd491cb1b Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Mon, 18 Oct 2021 16:37:35 +0700 Subject: [PATCH] fix(gatsby-dev-cli): resolve correct versions of packages with unpkg (#33551) * fix(gatsby-dev-cli): resolve correct versions of packages with unpkg * a bit more details in case of errors * defaults for deps --- .../src/utils/check-deps-changes.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/gatsby-dev-cli/src/utils/check-deps-changes.js b/packages/gatsby-dev-cli/src/utils/check-deps-changes.js index c9d529582fa1e..e6092f652853d 100644 --- a/packages/gatsby-dev-cli/src/utils/check-deps-changes.js +++ b/packages/gatsby-dev-cli/src/utils/check-deps-changes.js @@ -60,16 +60,16 @@ exports.checkDepsChanges = async ({ // this allow us to not publish to local repository // and save some time/work try { - const response = await got( - `https://unpkg.com/${packageName}/package.json` - ) + const version = getPackageVersion(packageName) + const url = `https://unpkg.com/${packageName}@${version}/package.json` + const response = await got(url) if (response?.statusCode !== 200) { - throw new Error(`No response or non 200 code`) + throw new Error(`No response or non 200 code for ${url}`) } localPKGjson = JSON.parse(response.body) - } catch { + } catch (e) { console.log( - `'${packageName}' doesn't seem to be installed and is not published on NPM.` + `'${packageName}' doesn't seem to be installed and is not published on NPM. Error: ${e.message}` ) return { didDepsChanged: true, @@ -182,3 +182,12 @@ exports.checkDepsChanges = async ({ packageNotInstalled, } } + +function getPackageVersion(packageName) { + const projectPackageJson = JSON.parse( + fs.readFileSync(`./package.json`, `utf-8`) + ) + const { dependencies = {}, devDependencies = {} } = projectPackageJson + const version = dependencies[packageName] || devDependencies[packageName] + return version || `latest` +}