-
Notifications
You must be signed in to change notification settings - Fork 167
/
util.js
124 lines (103 loc) · 3.48 KB
/
util.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict'
const fs = require('node:fs')
const path = require('node:path')
const url = require('node:url')
const semver = require('semver')
const pkgUp = require('pkg-up')
const resolveFrom = require('resolve-from')
const moduleSupport = semver.satisfies(process.version, '>= 14 || >= 12.17.0 < 13.0.0')
function exit (message) {
if (message instanceof Error) {
console.log(message)
return process.exit(1)
} else if (message) {
console.log(`Warn: ${message}`)
return process.exit(1)
}
process.exit()
}
function requireModule (moduleName) {
if (fs.existsSync(moduleName)) {
const moduleFilePath = path.resolve(moduleName)
return require(moduleFilePath)
} else {
return require(moduleName)
}
}
async function requireESModule (moduleName) {
if (fs.existsSync(moduleName)) {
const moduleFilePath = path.resolve(moduleName)
return import(url.pathToFileURL(moduleFilePath))
} else {
return import(moduleName)
}
}
function requireFastifyForModule (modulePath) {
try {
const basedir = path.resolve(process.cwd(), modulePath)
const module = require(resolveFrom.silent(basedir, 'fastify') || 'fastify')
return { module }
} catch (e) {
exit('unable to load fastify module')
}
}
function isInvalidAsyncPlugin (plugin) {
return plugin && plugin.length !== 2 && plugin.constructor.name === 'AsyncFunction'
}
async function getPackageType (cwd) {
const nearestPackage = await pkgUp({ cwd })
if (nearestPackage) {
return require(nearestPackage).type
}
}
function getScriptType (fname, packageType) {
const modulePattern = /\.mjs$/i
const commonjsPattern = /\.cjs$/i
return (modulePattern.test(fname) ? 'module' : commonjsPattern.test(fname) ? 'commonjs' : packageType) || 'commonjs'
}
async function requireServerPluginFromPath (modulePath) {
const resolvedModulePath = path.resolve(process.cwd(), modulePath)
if (!fs.existsSync(resolvedModulePath)) {
throw new Error(`${resolvedModulePath} doesn't exist within ${process.cwd()}`)
}
const packageType = await getPackageType(resolvedModulePath)
const type = getScriptType(resolvedModulePath, packageType)
let serverPlugin
if (type === 'module') {
if (moduleSupport) {
serverPlugin = await import(url.pathToFileURL(resolvedModulePath).href)
} else {
throw new Error(`fastify-cli cannot import plugin at '${resolvedModulePath}'. Your version of node does not support ES modules. To fix this error upgrade to Node 14 or use CommonJS syntax.`)
}
} else {
serverPlugin = require(resolvedModulePath)
}
if (isInvalidAsyncPlugin(type === 'commonjs' ? serverPlugin : serverPlugin.default)) {
throw new Error('Async/Await plugin function should contain 2 arguments. ' +
'Refer to documentation for more information.')
}
return serverPlugin
}
function showHelpForCommand (commandName) {
const helpFilePath = path.join(__dirname, 'help', `${commandName}.txt`)
try {
console.log(fs.readFileSync(helpFilePath, 'utf8'))
exit()
} catch (e) {
exit(`unable to get help for command "${commandName}"`)
}
}
function isKubernetes () {
// Detection based on https://kubernetes.io/docs/reference/kubectl/#in-cluster-authentication-and-namespace-overrides
return process.env.KUBERNETES_SERVICE_HOST !== undefined ||
fs.existsSync('/run/secrets/kubernetes.io/serviceaccount/token')
}
module.exports = {
isKubernetes,
exit,
requireModule,
requireESModule,
requireFastifyForModule,
showHelpForCommand,
requireServerPluginFromPath
}