From a332b6e87b23e13ba8dafad342b240327fa9e445 Mon Sep 17 00:00:00 2001 From: pieh Date: Tue, 19 Nov 2024 18:01:44 +0100 Subject: [PATCH] fix: add check for existence of runtime package in user project --- src/helpers/getAngularVersion.js | 22 --------------- src/helpers/getPackageVersion.js | 43 ++++++++++++++++++++++++++++++ src/helpers/serverModuleHelpers.js | 15 +++++++++++ src/index.js | 2 +- tests/integration.test.mjs | 2 +- 5 files changed, 60 insertions(+), 24 deletions(-) delete mode 100644 src/helpers/getAngularVersion.js create mode 100644 src/helpers/getPackageVersion.js diff --git a/src/helpers/getAngularVersion.js b/src/helpers/getAngularVersion.js deleted file mode 100644 index ac409549..00000000 --- a/src/helpers/getAngularVersion.js +++ /dev/null @@ -1,22 +0,0 @@ -const { readJSON } = require('fs-extra') - -/** - * Get Angular version from package.json. - * @param {string} root - * @returns {Promise} - */ -const getAngularVersion = async function (root) { - let packagePath - try { - // eslint-disable-next-line n/no-missing-require - packagePath = require.resolve('@angular/core/package.json', { paths: [root] }) - } catch { - // module not found - return - } - - const { version } = await readJSON(packagePath) - return version -} - -module.exports = getAngularVersion diff --git a/src/helpers/getPackageVersion.js b/src/helpers/getPackageVersion.js new file mode 100644 index 00000000..dbe80b99 --- /dev/null +++ b/src/helpers/getPackageVersion.js @@ -0,0 +1,43 @@ +const { readJSON } = require('fs-extra') + +/** + * Get Angular version from package.json. + * @param {string} root + * @returns {Promise} + */ +const getAngularVersion = async function (root) { + let packagePath + try { + // eslint-disable-next-line n/no-missing-require + packagePath = require.resolve('@angular/core/package.json', { paths: [root] }) + } catch { + // module not found + return + } + + const { version } = await readJSON(packagePath) + return version +} + +module.exports.getAngularVersion = getAngularVersion + +/** + * Get Angular Runtime version from package.json. + * @param {string} root + * @returns {Promise} + */ +const getAngularRuntimeVersion = async function (root) { + let packagePath + try { + // eslint-disable-next-line n/no-missing-require + packagePath = require.resolve('@netlify/angular-runtime/package.json', { paths: [root] }) + } catch { + // module not found + return + } + + const { version } = await readJSON(packagePath) + return version +} + +module.exports.getAngularRuntimeVersion = getAngularRuntimeVersion diff --git a/src/helpers/serverModuleHelpers.js b/src/helpers/serverModuleHelpers.js index af0b5295..facc6da7 100644 --- a/src/helpers/serverModuleHelpers.js +++ b/src/helpers/serverModuleHelpers.js @@ -5,6 +5,7 @@ const { parse, join } = require('node:path') const { satisfies } = require('semver') const getAngularJson = require('./getAngularJson') +const { getAngularRuntimeVersion } = require('./getPackageVersion') const { getEngineBasedOnKnownSignatures } = require('./serverTsSignature') const { getProject } = require('./setUpEdgeFunction') @@ -102,6 +103,20 @@ const fixServerTs = async function ({ angularVersion, siteRoot, failPlugin, fail return } + // check if user has installed runtime package and if the version is 2.2.0 or newer + // userspace `server.ts` file does import utils from runtime, so it has to be resolvable + // from site root and auto-installed plugin in `.netlify/plugins` wouldn't suffice for that. + const angularRuntimeVersionInstalledByUser = await getAngularRuntimeVersion(siteRoot) + if (!angularRuntimeVersionInstalledByUser) { + failBuild( + "Angular@19 SSR on Netlify requires '@netlify/angular-runtime' version 2.2.0 or later to be installed. Please install it and try again.", + ) + } else if (!satisfies(angularRuntimeVersionInstalledByUser, '>=2.2.0', { includePrerelease: true })) { + failBuild( + `Angular@19 SSR on Netlify requires '@netlify/angular-runtime' version 2.2.0 or later to be installed. Found version "${angularRuntimeVersionInstalledByUser}. Please update it to version 2.2.0 or later and try again.`, + ) + } + // check wether project is using stable CommonEngine or Developer Preview AppEngine const serverModuleContents = await readFile(serverModuleLocation, 'utf8') diff --git a/src/index.js b/src/index.js index 191f3989..8a3c807b 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ const ensureNoCompetingPlugin = require('./helpers/ensureNoCompetingPlugin') const fixOutputDir = require('./helpers/fixOutputDir') const getAngularJson = require('./helpers/getAngularJson') const getAngularRoot = require('./helpers/getAngularRoot') -const getAngularVersion = require('./helpers/getAngularVersion') +const { getAngularVersion } = require('./helpers/getPackageVersion') const { fixServerTs, revertServerTsFix } = require('./helpers/serverModuleHelpers') const { getProject, setUpEdgeFunction } = require('./helpers/setUpEdgeFunction') const setUpHeaders = require('./helpers/setUpHeaders') diff --git a/tests/integration.test.mjs b/tests/integration.test.mjs index 92f868e5..0465bbec 100644 --- a/tests/integration.test.mjs +++ b/tests/integration.test.mjs @@ -6,7 +6,7 @@ import { fileURLToPath } from 'node:url' import build from '@netlify/build' -import getAngularVersion from '../src/helpers/getAngularVersion.js' +import { getAngularVersion } from '../src/helpers/getPackageVersion.js' import validateAngularVersion from '../src/helpers/validateAngularVersion.js' test('project without angular config file fails the plugin execution but does not error', async () => {