From 016bab788b4bc1cb859cee15f92ded287cbdf661 Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Mon, 18 Oct 2021 05:31:45 -0600 Subject: [PATCH] fix(gatsby-dev-cli): resolve correct versions of packages with unpkg (#33551) (#33570) * fix(gatsby-dev-cli): resolve correct versions of packages with unpkg * a bit more details in case of errors * defaults for deps (cherry picked from commit 31d5a5e4d9e508eeeb41b630c3cd7d6f77333856) Co-authored-by: Vladimir Razuvaev --- .../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` +}