Skip to content
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

feat: migrate from warning to failedToLoadBuiltInConfig flag #8

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = (opts, types, defaults) => {

conf.add(Object.assign({}, opts), 'cli');
const warnings = [];
let failedToLoadBuiltInConfig = false;

if (require.resolve.paths) {
const paths = require.resolve.paths('npm');
Expand All @@ -17,8 +18,9 @@ module.exports = (opts, types, defaults) => {
try {
npmPath = require.resolve('npm', {paths: paths.slice(-1)});
} catch (error) {
// If npm module cannot be found, add warning about adding global config to overwrite builtin config
warnings.push('Load npm builtin configs failed, you can use "pnpm config ls" to show builtin configs. And then use "pnpm config --global set <key> <value>" to migrate configs from builtin to global.');
// Error will be thrown if module cannot be found.
// Update the flag while loading builtin config failed.
failedToLoadBuiltInConfig = true;
}

if (npmPath) {
Expand Down Expand Up @@ -69,6 +71,7 @@ module.exports = (opts, types, defaults) => {
return {
config: conf,
warnings: warnings.filter(Boolean),
failedToLoadBuiltInConfig,
};
};

Expand Down
7 changes: 2 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ test('mirror npm defaults', () => {


test('npm builtin configs require failed', async () => {
const expected = [
expect.stringMatching(/Load npm builtin configs failed/),
];

// In the scope of jest, require.resolve.paths('npm') cannot reach global npm path by default
const { warnings } = m();
const { failedToLoadBuiltInConfig } = m();

expect(warnings).toEqual(expect.arrayContaining(expected));
expect(failedToLoadBuiltInConfig).toBeTruthy();
})