Skip to content

Commit 00617aa

Browse files
committed
fix(extensions): detect installed scoped npm packages in ExtensibilityService
1 parent c77ff7d commit 00617aa

3 files changed

Lines changed: 154 additions & 3 deletions

File tree

‎lib/services/extensibility-service.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,25 @@ export class ExtensibilityService implements IExtensibilityService {
508508
extensionName: string,
509509
): Promise<void> {
510510
this.$logger.trace(`Asserting extension ${extensionName} is installed.`);
511-
const installedExtensions = this.$fs.readDirectory(
512-
path.join(this.pathToExtensions, constants.NODE_MODULES_FOLDER_NAME),
511+
const nodeModulesPath = path.join(
512+
this.pathToExtensions,
513+
constants.NODE_MODULES_FOLDER_NAME,
513514
);
515+
const installedExtensions = this.$fs.readDirectory(nodeModulesPath);
516+
517+
let isInstalled: boolean;
518+
if (extensionName.startsWith("@") && extensionName.includes("/")) {
519+
const [scope, pkgName] = extensionName.split("/");
520+
isInstalled =
521+
installedExtensions.indexOf(scope) !== -1 &&
522+
this.$fs
523+
.readDirectory(path.join(nodeModulesPath, scope))
524+
.indexOf(pkgName) !== -1;
525+
} else {
526+
isInstalled = installedExtensions.indexOf(extensionName) !== -1;
527+
}
514528

515-
if (installedExtensions.indexOf(extensionName) === -1) {
529+
if (!isInstalled) {
516530
this.$logger.trace(
517531
`Extension ${extensionName} is not installed, starting installation.`,
518532
);

‎test/extension-manifests.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,4 +1023,51 @@ describe("extension manifests", () => {
10231023
assert.deepEqual(capture.loadedModules, ["defaults-default"]);
10241024
});
10251025
});
1026+
1027+
describe("scoped extensions", () => {
1028+
it("loads an installed scoped extension lazily without reinstalling", async () => {
1029+
const extensionName = "@nsm/scoped-ext";
1030+
const contractsPath = require.resolve("../lib/contracts");
1031+
writeExtension(
1032+
extensionName,
1033+
{ commands: { "nsmscoped|run": "./dist/run.js" } },
1034+
{
1035+
"main.js": mainModule("scoped-main"),
1036+
"dist/run.js": `const { defineCommand } = require(${JSON.stringify(contractsPath)});
1037+
global.__nsmCapture.loadedModules.push("scoped-run");
1038+
module.exports = defineCommand({
1039+
name: "nsmscoped|run",
1040+
params: "any",
1041+
async run(ctx) {
1042+
global.__nsmCapture.executed.push({ marker: "scoped-run", args: ctx.args });
1043+
},
1044+
});`,
1045+
},
1046+
);
1047+
1048+
let installCalled = false;
1049+
testInjector.register("packageManager", {
1050+
install: async () => {
1051+
installCalled = true;
1052+
throw new Error("should not reinstall an installed scoped extension");
1053+
},
1054+
});
1055+
1056+
const extensibilityService = resolveService(testInjector);
1057+
const data = await extensibilityService.loadExtension(extensionName);
1058+
1059+
assert.isFalse(installCalled);
1060+
assert.equal(data.extensionName, extensionName);
1061+
assert.deepEqual(capture.loadedModules, []);
1062+
1063+
const command = testInjector.resolveCommand("nsmscoped|run");
1064+
assert.isOk(command);
1065+
assert.deepEqual(capture.loadedModules, ["scoped-run"]);
1066+
1067+
await command.execute(["ok"]);
1068+
assert.deepEqual(capture.executed, [
1069+
{ marker: "scoped-run", args: ["ok"] },
1070+
]);
1071+
});
1072+
});
10261073
});

‎test/services/extensibility-service.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,96 @@ describe("extensibilityService", () => {
409409
assert.isTrue(isNpmInstallCalled);
410410
});
411411

412+
it("loads already-installed scoped extensions without re-running npm install", async () => {
413+
const testInjector = getTestInjector();
414+
const fs: IFileSystem = testInjector.resolve("fs");
415+
const extensionNames = ["@scope/extension1", "extension2"];
416+
fs.exists = (): boolean => true;
417+
fs.readDirectory = (dir: string): string[] => {
418+
if (path.basename(dir) === constants.NODE_MODULES_FOLDER_NAME) {
419+
return ["@scope", "extension2"];
420+
}
421+
if (path.basename(dir) === "@scope") {
422+
return ["extension1"];
423+
}
424+
return [];
425+
};
426+
427+
mockFsReadJson(testInjector, extensionNames);
428+
429+
let isNpmInstallCalled = false;
430+
const npm: INodePackageManager = testInjector.resolve("npm");
431+
npm.install = async (): Promise<any> => {
432+
isNpmInstallCalled = true;
433+
return {};
434+
};
435+
436+
const expectedResults: IExtensionData[] = _.map(
437+
extensionNames,
438+
(extensionName) => ({
439+
extensionName,
440+
version: "1.0.0",
441+
pathToExtension: getExpectedInstallationPathForExtension(
442+
testInjector,
443+
extensionName,
444+
),
445+
docs: undefined as any,
446+
}),
447+
);
448+
449+
const extensibilityService: IExtensibilityService =
450+
testInjector.resolve(ExtensibilityService);
451+
const actualResult = await Promise.all(
452+
extensibilityService.loadExtensions(),
453+
);
454+
assert.deepStrictEqual(actualResult, expectedResults);
455+
assert.isFalse(isNpmInstallCalled);
456+
});
457+
458+
it("installs a missing scoped extension when its scope directory exists without the package", async () => {
459+
const testInjector = getTestInjector();
460+
const fs: IFileSystem = testInjector.resolve("fs");
461+
const extensionName = "@scope/missing-ext";
462+
fs.exists = (pathToCheck: string): boolean =>
463+
pathToCheck !== path.resolve(extensionName);
464+
fs.readDirectory = (dir: string): string[] => {
465+
if (path.basename(dir) === constants.NODE_MODULES_FOLDER_NAME) {
466+
return ["@scope"];
467+
}
468+
if (path.basename(dir) === "@scope") {
469+
return ["other-ext"];
470+
}
471+
return [];
472+
};
473+
474+
mockFsReadJson(testInjector, [extensionName]);
475+
476+
const installedPackages: string[] = [];
477+
const npm: INodePackageManager = testInjector.resolve("npm");
478+
npm.install = async (packageName: string): Promise<any> => {
479+
installedPackages.push(packageName);
480+
return { name: packageName };
481+
};
482+
483+
const extensibilityService: IExtensibilityService =
484+
testInjector.resolve(ExtensibilityService);
485+
const actualResult = await Promise.all(
486+
extensibilityService.loadExtensions(),
487+
);
488+
assert.deepStrictEqual(installedPackages, [extensionName]);
489+
assert.deepStrictEqual(actualResult, [
490+
{
491+
extensionName,
492+
version: "1.0.0",
493+
pathToExtension: getExpectedInstallationPathForExtension(
494+
testInjector,
495+
extensionName,
496+
),
497+
docs: undefined as any,
498+
},
499+
]);
500+
});
501+
412502
it("rejects only promises for extensions that cannot be loaded", async () => {
413503
const testInjector = getTestInjector();
414504
const fs: IFileSystem = testInjector.resolve("fs");

0 commit comments

Comments
 (0)