-
Notifications
You must be signed in to change notification settings - Fork 15
feat: adds checkDevEngines #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
11b7d04
fix: move from `main` to `exports` in package.json
wraithgar 3d0d00b
deps: add proc-log@4.2.0
wraithgar 634bfd2
wip: adding devEngines
wraithgar 2841ec8
devEngines v1
reggi 426273a
Merge remote-tracking branch 'origin' into reggi/dev-engines
reggi 52f82d0
accidental prop
reggi 86d04fa
remove constants
reggi ce64c7f
remove unnecessary export
reggi 432cd01
only run libc check on linux
reggi ecf7da4
guard against non string values
reggi e02ccf5
removed filters
reggi 29105a6
guards against non-object values, and empty arrays
reggi 8cf4e48
added spec 1, 2
reggi c9a04e0
top level value type check
reggi 3a35dd8
check > parse, export everything, import index in tests
reggi e68fb63
cleanup pr artifacts
reggi 3629038
rename current-env
reggi 7c15a8f
rm commonjs
reggi dd0c425
revert package
reggi a1cc700
coninute not break in for loop
reggi 38d2f39
revert to 6.3.0
reggi 83e7c96
add checkDevEngines to readme
reggi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const process = require('node:process') | ||
const nodeOs = require('node:os') | ||
|
||
function isMusl (file) { | ||
return file.includes('libc.musl-') || file.includes('ld-musl-') | ||
} | ||
|
||
function os () { | ||
return process.platform | ||
} | ||
|
||
function cpu () { | ||
return process.arch | ||
} | ||
|
||
function libc (osName) { | ||
// this is to make it faster on non linux machines | ||
if (osName !== 'linux') { | ||
return undefined | ||
} | ||
let family | ||
const report = process.report.getReport() | ||
if (report.header?.glibcVersionRuntime) { | ||
family = 'glibc' | ||
} else if (Array.isArray(report.sharedObjects) && report.sharedObjects.some(isMusl)) { | ||
family = 'musl' | ||
} | ||
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, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "${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 engine "${engine}"`), { | ||
errors: depErrors, | ||
engine, | ||
isWarn: onFail === 'warn', | ||
isError: onFail === 'error', | ||
current: currentEngine, | ||
required: dependencyAsAuthored, | ||
}) | ||
|
||
errors.push(err) | ||
} | ||
} | ||
return errors | ||
} | ||
|
||
module.exports = { | ||
checkDevEngines, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.