@@ -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