Skip to content

Commit 187f31b

Browse files
committed
feat: Add code action to update all packages
Fixes #1
1 parent 2a80e14 commit 187f31b

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ Displays a diagnostic message in package.json files for packages which have newe
88

99
![Screenshot](./assets/screenshot.jpg)
1010

11+
## Usage
12+
13+
This extension provides three primary means of updating outdated packages. The following code actions are available in `package.json` files.
14+
15+
1. `Update all packages` - This command will update all `dependencies` and `devDependencies` in the package.json file.
16+
1. `Update package` - This command will update a single package to the latest version. This will show when a single package is selected.
17+
1. `Update x packages` - This command will update all the selected packages to the latest version. This will show when multiple packages are selected.
18+
1119
## Contributors ✨
1220

1321
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

src/PackageJsonCodeActionProvider.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class PackageJsonCodeActionProvider
1010
range: vscode.Range,
1111
ctx: vscode.CodeActionContext
1212
): Promise<vscode.CodeAction[]> {
13-
// TODO: Add a single command to update all the packages
13+
// TODO: Update command to allow multiline selection
1414
if (!range.isSingleLine) {
1515
return Promise.resolve([])
1616
}
@@ -21,28 +21,64 @@ export class PackageJsonCodeActionProvider
2121
.filter((diagnostic) => diagnostic.code === DIAGNOSTIC_CODE)
2222
.map((diagnostic) => this.createCommandCodeAction(doc, diagnostic))
2323

24+
const allDiagnostics = vscode.languages
25+
.getDiagnostics(doc.uri)
26+
.filter((diagnostic) => diagnostic.code === DIAGNOSTIC_CODE)
27+
28+
// Only show the update all code action if there are outdated packages
29+
if (allDiagnostics.length) {
30+
promises.push(this.createUpdateAllCodeAction(doc, allDiagnostics))
31+
}
32+
2433
return Promise.all(promises)
2534
}
2635

36+
private async createUpdateAllCodeAction(
37+
doc: vscode.TextDocument,
38+
diagnostics: vscode.Diagnostic[]
39+
) {
40+
const edit = new vscode.WorkspaceEdit()
41+
const action = new vscode.CodeAction(
42+
"Update all packages",
43+
vscode.CodeActionKind.QuickFix
44+
)
45+
46+
action.edit = edit
47+
action.isPreferred = true
48+
action.diagnostics = diagnostics
49+
50+
const promises = action.diagnostics.map((diagnostic) =>
51+
this.createEdit(edit, doc, diagnostic.range)
52+
)
53+
await Promise.all(promises)
54+
55+
return action
56+
}
57+
2758
private async createCommandCodeAction(
2859
doc: vscode.TextDocument,
2960
diagnostic: vscode.Diagnostic
3061
) {
62+
const edit = new vscode.WorkspaceEdit()
3163
const action = new vscode.CodeAction(
32-
"Update package to latest version",
64+
"Update package",
3365
vscode.CodeActionKind.QuickFix
3466
)
3567

36-
action.edit = await this.createEdit(doc, diagnostic.range)
68+
await this.createEdit(edit, doc, diagnostic.range)
69+
70+
action.edit = edit
3771
action.diagnostics = [diagnostic]
3872
action.isPreferred = true
3973

4074
return action
4175
}
4276

43-
private async createEdit(doc: vscode.TextDocument, range: vscode.Range) {
44-
const edit = new vscode.WorkspaceEdit()
45-
77+
private async createEdit(
78+
edit: vscode.WorkspaceEdit,
79+
doc: vscode.TextDocument,
80+
range: vscode.Range
81+
) {
4682
// Get the latest version from the registry
4783
const line = doc.lineAt(range.start.line)
4884
const { name, version } = parseDependency(line.text)
@@ -58,7 +94,5 @@ export class PackageJsonCodeActionProvider
5894
edit.replace(doc.uri, range, prefix + info.version)
5995
}
6096
}
61-
62-
return edit
6397
}
6498
}

0 commit comments

Comments
 (0)