-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: totally rework package installation to peer deps
- Loading branch information
Showing
9 changed files
with
145 additions
and
113 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
#!/usr/bin/env node | ||
|
||
const check = require('../lib/check.js') | ||
|
||
const main = async () => { | ||
const { | ||
npm_config_local_prefix: root, | ||
} = process.env | ||
|
||
if (!root) { | ||
throw new Error('This package requires npm >7.21.1') | ||
} | ||
|
||
const problems = await check(root) | ||
if (problems.length) { | ||
console.error('Some problems were detected:') | ||
console.error() | ||
console.error(problems.map((problem) => problem.message).join('\n')) | ||
console.error() | ||
console.error('To correct them:') | ||
console.error(problems.map((problem) => problem.solution).join('\n')) | ||
process.exitCode = 1 | ||
} | ||
} | ||
|
||
module.exports = main().catch((err) => { | ||
console.error(err.stack) | ||
process.exitCode = 1 | ||
}) |
This file contains 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 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,95 @@ | ||
const { join } = require('path') | ||
const fs = require('@npmcli/fs') | ||
|
||
const patchPackage = require('./package.js') | ||
|
||
const unwantedPackages = [ | ||
'@npmcli/lint', | ||
'eslint-plugin-promise', | ||
'eslint-plugin-standard', | ||
'eslint-plugin-import', | ||
] | ||
|
||
const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key) | ||
|
||
const check = async (root) => { | ||
const pkgPath = join(root, 'package.json') | ||
try { | ||
var contents = await fs.readFile(pkgPath, { encoding: 'utf8' }) | ||
} catch (err) { | ||
throw new Error('No package.json found') | ||
} | ||
|
||
const pkg = JSON.parse(contents) | ||
|
||
const problems = [] | ||
|
||
const incorrectFields = [] | ||
// 1. ensure package.json changes have been applied | ||
for (const [key, value] of Object.entries(patchPackage.changes)) { | ||
if (!hasOwn(pkg, key)) { | ||
incorrectFields.push({ | ||
name: key, | ||
found: pkg[key], | ||
expected: value, | ||
}) | ||
continue | ||
} | ||
|
||
if (value && typeof value === 'object') { | ||
for (const [subKey, subValue] of Object.entries(value)) { | ||
if (!hasOwn(pkg[key], subKey) || | ||
pkg[key][subKey] !== subValue) { | ||
incorrectFields.push({ | ||
name: `${key}.${subKey}`, | ||
found: pkg[key][subKey], | ||
expected: subValue, | ||
}) | ||
} | ||
} | ||
} else { | ||
if (pkg[key] !== patchPackage.changes[key]) { | ||
incorrectFields.push({ | ||
name: key, | ||
found: pkg[key], | ||
expected: value, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
if (incorrectFields.length) { | ||
problems.push({ | ||
message: [ | ||
`The following package.json fields are incorrect:`, | ||
...incorrectFields.map((field) => { | ||
const { name, found, expected } = field | ||
return ` Field: "${name}" Expected: "${expected}" Found: "${found}"` | ||
}), | ||
].join('\n'), | ||
solution: 'npm rm @npmcli/template-oss && npm i -D @npmcli/template-oss', | ||
}) | ||
} | ||
|
||
// 2. ensure packages that should not be present are removed | ||
const mustRemove = unwantedPackages.filter((name) => { | ||
return hasOwn(pkg.dependencies || {}, name) || | ||
hasOwn(pkg.devDependencies || {}, name) | ||
}) | ||
|
||
if (mustRemove.length) { | ||
problems.push({ | ||
message: [ | ||
'The following unwanted packages were found:', | ||
...mustRemove, | ||
].join(' '), | ||
solution: `npm rm ${mustRemove.join(' ')}`, | ||
}) | ||
} | ||
|
||
return problems | ||
} | ||
|
||
check.unwantedPackages = unwantedPackages | ||
|
||
module.exports = check |
This file was deleted.
Oops, something went wrong.
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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