Primeiro commit do projeto Angular
This commit is contained in:
+91
@@ -0,0 +1,91 @@
|
||||
const process = require('node:process')
|
||||
const nodeOs = require('node:os')
|
||||
const fs = require('node:fs')
|
||||
|
||||
function isMusl (file) {
|
||||
return file.includes('libc.musl-') || file.includes('ld-musl-')
|
||||
}
|
||||
|
||||
function os () {
|
||||
return process.platform
|
||||
}
|
||||
|
||||
function cpu () {
|
||||
return process.arch
|
||||
}
|
||||
|
||||
const LDD_PATH = '/usr/bin/ldd'
|
||||
function getFamilyFromFilesystem () {
|
||||
try {
|
||||
const content = fs.readFileSync(LDD_PATH, 'utf-8')
|
||||
if (content.includes('musl')) {
|
||||
return 'musl'
|
||||
}
|
||||
if (content.includes('GNU C Library')) {
|
||||
return 'glibc'
|
||||
}
|
||||
return null
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
function getFamilyFromReport () {
|
||||
const originalExclude = process.report.excludeNetwork
|
||||
process.report.excludeNetwork = true
|
||||
const report = process.report.getReport()
|
||||
process.report.excludeNetwork = originalExclude
|
||||
if (report.header?.glibcVersionRuntime) {
|
||||
family = 'glibc'
|
||||
} else if (Array.isArray(report.sharedObjects) && report.sharedObjects.some(isMusl)) {
|
||||
family = 'musl'
|
||||
} else {
|
||||
family = null
|
||||
}
|
||||
return family
|
||||
}
|
||||
|
||||
let family
|
||||
function libc (osName) {
|
||||
if (osName !== 'linux') {
|
||||
return undefined
|
||||
}
|
||||
if (family === undefined) {
|
||||
family = getFamilyFromFilesystem()
|
||||
if (family === undefined) {
|
||||
family = getFamilyFromReport()
|
||||
}
|
||||
}
|
||||
return family
|
||||
}
|
||||
|
||||
function devEngines (env = {}) {
|
||||
const osName = env.os || os()
|
||||
return {
|
||||
cpu: {
|
||||
name: env.cpu || cpu(),
|
||||
},
|
||||
libc: {
|
||||
name: env.libc || libc(osName),
|
||||
},
|
||||
os: {
|
||||
name: osName,
|
||||
version: env.osVersion || nodeOs.release(),
|
||||
},
|
||||
packageManager: {
|
||||
name: 'npm',
|
||||
version: env.npmVersion,
|
||||
},
|
||||
runtime: {
|
||||
name: 'node',
|
||||
version: env.nodeVersion || process.version,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
cpu,
|
||||
libc,
|
||||
os,
|
||||
devEngines,
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
const satisfies = require('semver/functions/satisfies')
|
||||
const validRange = require('semver/ranges/valid')
|
||||
|
||||
const recognizedOnFail = [
|
||||
'ignore',
|
||||
'warn',
|
||||
'error',
|
||||
'download',
|
||||
]
|
||||
|
||||
const recognizedProperties = [
|
||||
'name',
|
||||
'version',
|
||||
'onFail',
|
||||
]
|
||||
|
||||
const recognizedEngines = [
|
||||
'packageManager',
|
||||
'runtime',
|
||||
'cpu',
|
||||
'libc',
|
||||
'os',
|
||||
]
|
||||
|
||||
/** checks a devEngine dependency */
|
||||
function checkDependency (wanted, current, opts) {
|
||||
const { engine } = opts
|
||||
|
||||
if ((typeof wanted !== 'object' || wanted === null) || Array.isArray(wanted)) {
|
||||
throw new Error(`Invalid non-object value for "${engine}"`)
|
||||
}
|
||||
|
||||
const properties = Object.keys(wanted)
|
||||
|
||||
for (const prop of properties) {
|
||||
if (!recognizedProperties.includes(prop)) {
|
||||
throw new Error(`Invalid property "${prop}" for "${engine}"`)
|
||||
}
|
||||
}
|
||||
|
||||
if (!properties.includes('name')) {
|
||||
throw new Error(`Missing "name" property for "${engine}"`)
|
||||
}
|
||||
|
||||
if (typeof wanted.name !== 'string') {
|
||||
throw new Error(`Invalid non-string value for "name" within "${engine}"`)
|
||||
}
|
||||
|
||||
if (typeof current.name !== 'string' || current.name === '') {
|
||||
throw new Error(`Unable to determine "name" for "${engine}"`)
|
||||
}
|
||||
|
||||
if (properties.includes('onFail')) {
|
||||
if (typeof wanted.onFail !== 'string') {
|
||||
throw new Error(`Invalid non-string value for "onFail" within "${engine}"`)
|
||||
}
|
||||
if (!recognizedOnFail.includes(wanted.onFail)) {
|
||||
throw new Error(`Invalid onFail value "${wanted.onFail}" for "${engine}"`)
|
||||
}
|
||||
}
|
||||
|
||||
if (wanted.name !== current.name) {
|
||||
return new Error(
|
||||
`Invalid name "${wanted.name}" does not match "${current.name}" for "${engine}"`
|
||||
)
|
||||
}
|
||||
|
||||
if (properties.includes('version')) {
|
||||
if (typeof wanted.version !== 'string') {
|
||||
throw new Error(`Invalid non-string value for "version" within "${engine}"`)
|
||||
}
|
||||
if (typeof current.version !== 'string' || current.version === '') {
|
||||
throw new Error(`Unable to determine "version" for "${engine}" "${wanted.name}"`)
|
||||
}
|
||||
if (validRange(wanted.version)) {
|
||||
if (!satisfies(current.version, wanted.version, opts.semver)) {
|
||||
return new Error(
|
||||
// eslint-disable-next-line max-len
|
||||
`Invalid semver version "${wanted.version}" does not match "${current.version}" for "${engine}"`
|
||||
)
|
||||
}
|
||||
} else if (wanted.version !== current.version) {
|
||||
return new Error(
|
||||
`Invalid version "${wanted.version}" does not match "${current.version}" for "${engine}"`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** checks devEngines package property and returns array of warnings / errors */
|
||||
function checkDevEngines (wanted, current = {}, opts = {}) {
|
||||
if ((typeof wanted !== 'object' || wanted === null) || Array.isArray(wanted)) {
|
||||
throw new Error(`Invalid non-object value for "devEngines"`)
|
||||
}
|
||||
|
||||
const errors = []
|
||||
|
||||
for (const engine of Object.keys(wanted)) {
|
||||
if (!recognizedEngines.includes(engine)) {
|
||||
throw new Error(`Invalid property "devEngines.${engine}"`)
|
||||
}
|
||||
const dependencyAsAuthored = wanted[engine]
|
||||
const dependencies = [dependencyAsAuthored].flat()
|
||||
const currentEngine = current[engine] || {}
|
||||
|
||||
// this accounts for empty array eg { runtime: [] } and ignores it
|
||||
if (dependencies.length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
const depErrors = []
|
||||
for (const dep of dependencies) {
|
||||
const result = checkDependency(dep, currentEngine, { ...opts, engine })
|
||||
if (result) {
|
||||
depErrors.push(result)
|
||||
}
|
||||
}
|
||||
|
||||
const invalid = depErrors.length === dependencies.length
|
||||
|
||||
if (invalid) {
|
||||
const lastDependency = dependencies[dependencies.length - 1]
|
||||
let onFail = lastDependency.onFail || 'error'
|
||||
if (onFail === 'download') {
|
||||
onFail = 'error'
|
||||
}
|
||||
|
||||
const err = Object.assign(new Error(`Invalid devEngines.${engine}`), {
|
||||
errors: depErrors,
|
||||
engine,
|
||||
isWarn: onFail === 'warn',
|
||||
isError: onFail === 'error',
|
||||
current: currentEngine,
|
||||
required: dependencyAsAuthored,
|
||||
})
|
||||
|
||||
errors.push(err)
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
checkDevEngines,
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
const semver = require('semver')
|
||||
const currentEnv = require('./current-env')
|
||||
const { checkDevEngines } = require('./dev-engines')
|
||||
|
||||
const checkEngine = (target, npmVer, nodeVer, force = false) => {
|
||||
const nodev = force ? null : nodeVer
|
||||
const eng = target.engines
|
||||
const opt = { includePrerelease: true }
|
||||
if (!eng) {
|
||||
return
|
||||
}
|
||||
|
||||
const nodeFail = nodev && eng.node && !semver.satisfies(nodev, eng.node, opt)
|
||||
const npmFail = npmVer && eng.npm && !semver.satisfies(npmVer, eng.npm, opt)
|
||||
if (nodeFail || npmFail) {
|
||||
throw Object.assign(new Error('Unsupported engine'), {
|
||||
pkgid: target._id,
|
||||
current: { node: nodeVer, npm: npmVer },
|
||||
required: eng,
|
||||
code: 'EBADENGINE',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const checkPlatform = (target, force = false, environment = {}) => {
|
||||
if (force) {
|
||||
return
|
||||
}
|
||||
|
||||
const os = environment.os || currentEnv.os()
|
||||
const cpu = environment.cpu || currentEnv.cpu()
|
||||
const libc = environment.libc || currentEnv.libc(os)
|
||||
|
||||
const osOk = target.os ? checkList(os, target.os) : true
|
||||
const cpuOk = target.cpu ? checkList(cpu, target.cpu) : true
|
||||
let libcOk = target.libc ? checkList(libc, target.libc) : true
|
||||
if (target.libc && !libc) {
|
||||
libcOk = false
|
||||
}
|
||||
|
||||
if (!osOk || !cpuOk || !libcOk) {
|
||||
throw Object.assign(new Error('Unsupported platform'), {
|
||||
pkgid: target._id,
|
||||
current: {
|
||||
os,
|
||||
cpu,
|
||||
libc,
|
||||
},
|
||||
required: {
|
||||
os: target.os,
|
||||
cpu: target.cpu,
|
||||
libc: target.libc,
|
||||
},
|
||||
code: 'EBADPLATFORM',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const checkList = (value, list) => {
|
||||
if (typeof list === 'string') {
|
||||
list = [list]
|
||||
}
|
||||
if (list.length === 1 && list[0] === 'any') {
|
||||
return true
|
||||
}
|
||||
// match none of the negated values, and at least one of the
|
||||
// non-negated values, if any are present.
|
||||
let negated = 0
|
||||
let match = false
|
||||
for (const entry of list) {
|
||||
const negate = entry.charAt(0) === '!'
|
||||
const test = negate ? entry.slice(1) : entry
|
||||
if (negate) {
|
||||
negated++
|
||||
if (value === test) {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
match = match || value === test
|
||||
}
|
||||
}
|
||||
return match || negated === list.length
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
checkEngine,
|
||||
checkPlatform,
|
||||
checkDevEngines,
|
||||
currentEnv,
|
||||
}
|
||||
Reference in New Issue
Block a user