-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
unset.ts
88 lines (73 loc) Β· 2.97 KB
/
unset.ts
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
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, StreamReport, MessageName} from '@yarnpkg/core';
import {Command, Option, Usage, UsageError} from 'clipanion';
import cloneDeep from 'lodash/cloneDeep';
import hasPath from 'lodash/has';
import unsetPath from 'lodash/unset';
// eslint-disable-next-line arca/no-default-export
export default class ConfigUnsetCommand extends BaseCommand {
static paths = [
[`config`, `unset`],
];
static usage: Usage = Command.Usage({
description: `unset a configuration setting`,
details: `
This command will unset a configuration setting.
`,
examples: [[
`Unset a simple configuration setting`,
`yarn config unset initScope`,
], [
`Unset a complex configuration setting`,
`yarn config unset packageExtensions`,
], [
`Unset a nested configuration setting`,
`yarn config unset npmScopes.company.npmRegistryServer`,
]],
});
home = Option.Boolean(`-H,--home`, false, {
description: `Update the home configuration instead of the project configuration`,
});
name = Option.String();
async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const assertProjectCwd = () => {
if (!configuration.projectCwd)
throw new UsageError(`This command must be run from within a project folder`);
return configuration.projectCwd;
};
const name = this.name.replace(/[.[].*$/, ``);
const path = this.name.replace(/^[^.[]*\.?/, ``);
const setting = configuration.settings.get(name);
if (typeof setting === `undefined`)
throw new UsageError(`Couldn't find a configuration settings named "${name}"`);
const updateConfiguration: (patch: ((current: any) => any)) => Promise<boolean> =
this.home
? patch => Configuration.updateHomeConfiguration(patch)
: patch => Configuration.updateConfiguration(assertProjectCwd(), patch);
const report = await StreamReport.start({
configuration,
includeFooter: false,
stdout: this.context.stdout,
}, async report => {
let bailedOutEarly = false;
await updateConfiguration(current => {
if (!hasPath(current, this.name)) {
report.reportWarning(MessageName.UNNAMED, `Configuration doesn't contain setting ${this.name}; there is nothing to unset`);
bailedOutEarly = true;
return current;
}
const clone = path
? cloneDeep(current)
: {...current};
unsetPath(clone, this.name);
return clone;
});
// We can't show the success message in the callback as we must first wait for the new configuration to be persisted
if (!bailedOutEarly) {
report.reportInfo(MessageName.UNNAMED, `Successfully unset ${this.name}`);
}
});
return report.exitCode();
}
}