-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathvalidateNextUsage.js
43 lines (37 loc) · 1.46 KB
/
validateNextUsage.js
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
const { yellowBright } = require('chalk')
const { lt: ltVersion, gte: gteVersion } = require('semver')
// Ensure Next.js is available.
// We use `peerDependencies` instead of `dependencies` so that users can choose
// the Next.js version. However, this requires them to install "next" in their
// site.
const validateNextUsage = function (failBuild) {
if (!hasPackage('next')) {
return failBuild(
'This site does not seem to be using Next.js. Please run "npm install next" or "yarn next" in the repository.',
)
}
// We cannot load `next` at the top-level because we validate whether the
// site is using `next` inside `onPreBuild`.
// Old Next.js versions are not supported
const { version } = require('next/package.json')
if (ltVersion(version, MIN_VERSION)) {
return failBuild(`Please upgrade to Next.js ${MIN_VERSION} or later`)
}
// Recent Next.js versions are sometimes unstable and we might not officially
// support them yet. However, they might still work for some users, so we
// only print a warning
if (gteVersion(version, MIN_EXPERIMENTAL_VERSION)) {
console.log(yellowBright(`** Warning: support for Next.js >=${MIN_EXPERIMENTAL_VERSION} is experimental **`))
}
}
const MIN_VERSION = '10.0.6'
const MIN_EXPERIMENTAL_VERSION = '11.0.0'
const hasPackage = function (packageName) {
try {
require(`${packageName}/package.json`)
return true
} catch (error) {
return false
}
}
module.exports = validateNextUsage