Skip to content

Commit d2afdf7

Browse files
committed
feat: Add install notification
Fixes #3
1 parent 52faaee commit d2afdf7

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

src/PackageJsonCodeActionProvider.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import vscode from "vscode"
2+
import { commands } from "./commands"
23
import { fetchPackage } from "./utils/packages"
34
import { parseDependency } from "./utils/parseDependency"
45
import { DIAGNOSTIC_CODE } from "./utils/vars"
@@ -44,15 +45,23 @@ export class PackageJsonCodeActionProvider
4445
return Promise.all(promises)
4546
}
4647

47-
private createAction(message: string) {
48+
private createAction(
49+
doc: vscode.TextDocument,
50+
message: string,
51+
commandMessage: string
52+
) {
4853
const edit = new vscode.WorkspaceEdit()
4954
const action = new vscode.CodeAction(
5055
message,
5156
vscode.CodeActionKind.QuickFix
5257
)
5358

5459
action.edit = edit
55-
action.isPreferred = true
60+
action.command = {
61+
arguments: [commandMessage, doc.uri],
62+
command: commands.notify,
63+
title: "update",
64+
}
5665

5766
return [action, edit] as const
5867
}
@@ -62,7 +71,11 @@ export class PackageJsonCodeActionProvider
6271
diagnostics: vscode.Diagnostic[],
6372
message: string
6473
) {
65-
const [action, edit] = this.createAction(message)
74+
const [action, edit] = this.createAction(
75+
doc,
76+
message,
77+
`Package${diagnostics.length > 1 ? "s" : ""} updated successfully.`
78+
)
6679
action.diagnostics = diagnostics
6780

6881
await Promise.all(
@@ -78,7 +91,13 @@ export class PackageJsonCodeActionProvider
7891
doc: vscode.TextDocument,
7992
diagnostic: vscode.Diagnostic
8093
) {
81-
const [action, edit] = this.createAction("Update package")
94+
const [action, edit] = this.createAction(
95+
doc,
96+
"Update package",
97+
"Package update successfully."
98+
)
99+
100+
action.isPreferred = true
82101
action.diagnostics = [diagnostic]
83102

84103
await this.createEdit(edit, doc, diagnostic.range)

src/commands/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const commands = {
2+
install: "npm-outdated.install",
3+
notify: "npm-outdated.notify",
4+
}

src/commands/install.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import cp from "child_process"
2+
import vscode from "vscode"
3+
4+
export const install = (outputChannel: vscode.OutputChannel) => async (
5+
cmd: string,
6+
cwd: string
7+
) => {
8+
outputChannel.clear()
9+
10+
const p = cp.exec(cmd, { cwd })
11+
const handleData = (data: string) => outputChannel.append(data)
12+
13+
p.stderr?.on?.("data", handleData)
14+
p.stdout?.on?.("data", handleData)
15+
16+
outputChannel.show()
17+
}

src/commands/notify.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { commands } from "."
2+
import path from "path"
3+
import vscode from "vscode"
4+
5+
async function isYarn(uri: vscode.Uri) {
6+
const path = uri.fsPath.replace("package.json", "yarn.lock")
7+
8+
try {
9+
await vscode.workspace.fs.stat(vscode.Uri.file(path))
10+
return true
11+
} catch (e) {
12+
return false
13+
}
14+
}
15+
16+
export async function notify(message: string, uri: vscode.Uri) {
17+
const yarn = await isYarn(uri)
18+
const installCommand = yarn ? "yarn" : "npm install"
19+
const action = `Run '${installCommand}'`
20+
21+
const result = await vscode.window.showInformationMessage(
22+
`${message} Run your package manager install command to finish updating packages.`,
23+
action
24+
)
25+
26+
if (result === action) {
27+
vscode.commands.executeCommand(
28+
commands.install,
29+
installCommand,
30+
path.dirname(uri.fsPath)
31+
)
32+
}
33+
}

src/extension.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import vscode from "vscode"
22
import { PackageJsonCodeActionProvider } from "./PackageJsonCodeActionProvider"
3+
import { commands } from "./commands"
4+
import { install } from "./commands/install"
5+
import { notify } from "./commands/notify"
36
import { findOutdatedPackages } from "./diagnostics/findOutdatedPackages"
47
import { getPackageRanges } from "./diagnostics/getPackageRanges"
58
import { subscribeToDocument } from "./diagnostics/subscribeToDocument"
@@ -30,7 +33,12 @@ export function activate(ctx: vscode.ExtensionContext): void {
3033
diagnosticCollection.set(doc.uri, diagnostics)
3134
})
3235

36+
const outputChannel = vscode.window.createOutputChannel("npm Outdated")
37+
3338
ctx.subscriptions.push(
39+
outputChannel,
40+
vscode.commands.registerCommand(commands.notify, notify),
41+
vscode.commands.registerCommand(commands.install, install(outputChannel)),
3442
vscode.languages.registerCodeActionsProvider(
3543
{
3644
language: "json",

0 commit comments

Comments
 (0)