Skip to content

Commit

Permalink
fix: add check for existence of runtime package in user project
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Nov 19, 2024
1 parent 967bb66 commit a332b6e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
22 changes: 0 additions & 22 deletions src/helpers/getAngularVersion.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/helpers/getPackageVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { readJSON } = require('fs-extra')

/**
* Get Angular version from package.json.
* @param {string} root
* @returns {Promise<string | undefined>}
*/
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<string | undefined>}
*/
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
15 changes: 15 additions & 0 deletions src/helpers/serverModuleHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/integration.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit a332b6e

Please sign in to comment.