Skip to content

Commit 301d093

Browse files
feat: Warn that it requires npm install
After the package version is updated in the `package.json`, it warn the user to run `npm install`. Otherwise, nothing done!
1 parent 60a5403 commit 301d093

File tree

8 files changed

+61
-7
lines changed

8 files changed

+61
-7
lines changed

locales/bundle.l10n.jsonc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"Package \"{0}\" pending installation: {1}.": "", // The package has been declared as a dependency, but no version has yet been installed.
2929
"Newer version of \"{0}\" is available: {1}.": "", // New version available which can be used by user.
3030
"Pre-release version of \"{0}\".": "", // The defined user version is a pre-release, and there is no better alternative yet.
31+
"Ready-to-install package. Just run your package manager install command.": "", // User must run the `npm install` to install new packages based on version defined.
3132
"Security advisory: this package version has a known flaw level {0}/{1}.": "", // Diagnostic warning when a security advisory exists.
3233
"Please upgrade to version {0} or higher.": "", // Suggested upgrade to a version above.
3334
"No fix available yet.": "", // The flaw is not yet known to be resolved in a future release.
@@ -42,6 +43,7 @@
4243
"already installed, just formalization": "", // The version informed by the user is already installed, only a version formalization of package.json will be needed.
4344
"attention: major update!": "", // Warns the user that a major update will take place.
4445
"pre-release": "", // Warns the user that this is a pre-release version.
46+
"Now run your package manager install command.": "", // User must run the `npm install` to install new packages based on version defined.
4547
"Security advisory": "", // Informs that the package has a security advisory.
4648
"low": "", // Security advisory level.
4749
"moderate": "", // Security advisory level.

locales/bundle.l10n.pt-br.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"Update {0} selected packages": "Atualizar as {0} dependências selecionadas",
66
"Update all {0} packages": "Atualizar todas as {0} dependências",
77

8-
"Save your package.json and run your package manager install command to finish updating packages.": "Salve o package.json e use o seu gerenciador de pacotes para concluir a instalação de suas dependências.",
8+
"Save your package.json and run your package manager install command to finish updating packages.": "Salve o package.json e use o seu gerenciador de dependências para concluir a instalação de suas dependências.",
99
"Do it for me!": "Faça para mim!",
1010

1111
"Installing selected packages...": "Instalando as dependências selecionadas...",
@@ -19,6 +19,7 @@
1919
"Package \"{0}\" pending installation: {1}.": "Dependência \"{0}\" com instalação pendente: {1}",
2020
"Newer version of \"{0}\" is available: {1}.": "Versão mais nova disponível para a dependência \"{0}\": {1}",
2121
"Pre-release version of \"{0}\".": "Versão pre-release de \"{0}\".",
22+
"Ready-to-install package. Just run your package manager install command.": "Pacote pronto para instalar. Basta executar o comando `install` do seu gerenciador de dependências.",
2223
"Security advisory: this package version has a known flaw level {0}/{1}.": "",
2324
"Please upgrade to version {0} or higher.": "Por favor, atualize para a versão {0} ou superior.",
2425
"No fix available yet.": "Ainda não há uma correção disponível.",
@@ -31,6 +32,7 @@
3132
"install pending": "instalação pendente",
3233
"already installed, just formalization": "já instalado, apenas formalização",
3334
"attention: major update!": "atenção: atualização major!",
35+
"Now run your package manager install command.": "Agora, rode o comando `install` do seu gerenciador de dependências.",
3436
"Security advisory": "Aviso de segurança",
3537
"low": "baixo",
3638
"moderate": "moderado",

src/CodeAction.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ export class PackageJsonCodeActionProvider implements CodeActionProvider {
2323
document: TextDocument,
2424
range: Range
2525
): Promise<CodeAction[]> {
26-
// Get all diagnostics from this extension
26+
// Get all diagnostics from this extension.
2727
const diagnostics = languages
2828
.getDiagnostics(document.uri)
2929
.filter(
3030
(diagnostic) =>
3131
typeof diagnostic.code === "object" &&
32-
diagnostic.code.value === DIAGNOSTIC_ACTION
32+
diagnostic.code.value === DIAGNOSTIC_ACTION &&
33+
(!PackageRelatedDiagnostic.is(diagnostic) || diagnostic.isSelectable)
3334
) as PackageRelatedDiagnostic[]
3435

3536
// Checks if an CodeAction comes through a diagnostic.

src/Diagnostic.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ export class PackageRelatedDiagnostic extends Diagnostic {
114114
message: string,
115115
severity: DiagnosticSeverity,
116116
document: TextDocument,
117-
public packageRelated: PackageInfo
117+
public packageRelated: PackageInfo,
118+
public isSelectable = true
118119
) {
119120
super(range, message, severity)
120121

@@ -173,6 +174,21 @@ export const getPackageDiagnostic = async (
173174
}
174175

175176
if (!(await packageInfo.isVersionUpdatable())) {
177+
// The user has the latest version defined in `package.json`,
178+
// but still needs to run `npm install` to complete.
179+
if (await packageInfo.requiresInstallCommand()) {
180+
return new PackageRelatedDiagnostic(
181+
packageInfo.versionRange,
182+
l10n.t(
183+
"Ready-to-install package. Just run your package manager install command."
184+
),
185+
DiagnosticSeverity.Information,
186+
document,
187+
packageInfo,
188+
false
189+
)
190+
}
191+
176192
return
177193
}
178194

@@ -200,7 +216,7 @@ export const getPackageDiagnostic = async (
200216
)
201217
}
202218

203-
// istanbul ignore next: must never happen
219+
// istanbul ignore next
204220
return
205221
}
206222

src/DocumentDecoration.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ export class DocumentDecoration {
197197
])
198198
}
199199

200+
if (await packageInfo.packageRelated.requiresInstallCommand()) {
201+
return this.setLine(line, [
202+
new Message(
203+
Icons.PENDING,
204+
ThemeLight.ICON_AVAILABLE,
205+
ThemeDark.ICON_AVAILABLE
206+
),
207+
new Message(l10n.t("Now run your package manager install command.")),
208+
])
209+
}
210+
200211
const updateDetails = [
201212
new Message(
202213
Icons.UPDATABLE,

src/PackageInfo.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ export class PackageInfo {
102102
)
103103
}
104104

105+
// If the latest version is already to be installed.
106+
public async requiresInstallCommand(): Promise<boolean> {
107+
const versionClear = this.getVersionClear()
108+
109+
return (
110+
(await this.getVersionLatest()) === versionClear &&
111+
(await this.getVersionInstalled()) !== versionClear
112+
)
113+
}
114+
105115
// Get the package version installed.
106116
public async getVersionInstalled(): Promise<string | undefined> {
107117
const packagesInstalled = await getPackagesInstalled()

src/Utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ describe("utils", () => {
155155
{ Accept: "application/vnd.npm.install-v1+json" }
156156
)
157157

158-
expect(fetchSuccess).toStrictEqual({})
158+
expect(fetchSuccess).toBeInstanceOf(Object)
159159
})
160160
})

src/extension.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ describe("package diagnostics", () => {
240240
expect(diagnostics).toHaveLength(0)
241241
})
242242

243+
it("valid dependency, waiting for run your package manager install", async () => {
244+
const { decorations, diagnostics } = await vscodeSimulator({
245+
packageJson: { dependencies: { "npm-outdated": "^1.0.1" } },
246+
packagesInstalled: { "npm-outdated": "1.0.0" },
247+
packagesRepository: { "npm-outdated": ["1.0.0", "1.0.1"] },
248+
})
249+
250+
expect(diagnostics[0]?.message).toContain("run your package manager")
251+
expect(decorations[0]).toContain(
252+
"Now run your package manager install command."
253+
)
254+
})
255+
243256
it("dependency name is invalid", async () => {
244257
const { decorations, diagnostics } = await vscodeSimulator({
245258
packageJson: { dependencies: { "invalid!": "^1.0.0" } },
@@ -616,7 +629,6 @@ describe("security advisories", () => {
616629

617630
it("needs downgrade", async () => {
618631
const { decorations, diagnostics } = await vscodeSimulator({
619-
cacheEnabled: true,
620632
packageJson: {
621633
dependencies: {
622634
"@types/jest": "1.0.0",

0 commit comments

Comments
 (0)