Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/services/extensibility-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,25 @@ export class ExtensibilityService implements IExtensibilityService {
extensionName: string,
): Promise<void> {
this.$logger.trace(`Asserting extension ${extensionName} is installed.`);
const installedExtensions = this.$fs.readDirectory(
path.join(this.pathToExtensions, constants.NODE_MODULES_FOLDER_NAME),
const nodeModulesPath = path.join(
this.pathToExtensions,
constants.NODE_MODULES_FOLDER_NAME,
);
const installedExtensions = this.$fs.readDirectory(nodeModulesPath);

let isInstalled: boolean;
if (extensionName.startsWith("@") && extensionName.includes("/")) {
const [scope, pkgName] = extensionName.split("/");
isInstalled =
installedExtensions.indexOf(scope) !== -1 &&
this.$fs
.readDirectory(path.join(nodeModulesPath, scope))
.indexOf(pkgName) !== -1;
} else {
isInstalled = installedExtensions.indexOf(extensionName) !== -1;
}

if (installedExtensions.indexOf(extensionName) === -1) {
if (!isInstalled) {
this.$logger.trace(
`Extension ${extensionName} is not installed, starting installation.`,
);
Expand Down
47 changes: 47 additions & 0 deletions test/extension-manifests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,4 +1023,51 @@ describe("extension manifests", () => {
assert.deepEqual(capture.loadedModules, ["defaults-default"]);
});
});

describe("scoped extensions", () => {
it("loads an installed scoped extension lazily without reinstalling", async () => {
const extensionName = "@nsm/scoped-ext";
const contractsPath = require.resolve("../lib/contracts");
writeExtension(
extensionName,
{ commands: { "nsmscoped|run": "./dist/run.js" } },
{
"main.js": mainModule("scoped-main"),
"dist/run.js": `const { defineCommand } = require(${JSON.stringify(contractsPath)});
global.__nsmCapture.loadedModules.push("scoped-run");
module.exports = defineCommand({
name: "nsmscoped|run",
params: "any",
async run(ctx) {
global.__nsmCapture.executed.push({ marker: "scoped-run", args: ctx.args });
},
});`,
},
);

let installCalled = false;
testInjector.register("packageManager", {
install: async () => {
installCalled = true;
throw new Error("should not reinstall an installed scoped extension");
},
});

const extensibilityService = resolveService(testInjector);
const data = await extensibilityService.loadExtension(extensionName);

assert.isFalse(installCalled);
assert.equal(data.extensionName, extensionName);
assert.deepEqual(capture.loadedModules, []);

const command = testInjector.resolveCommand("nsmscoped|run");
assert.isOk(command);
assert.deepEqual(capture.loadedModules, ["scoped-run"]);

await command.execute(["ok"]);
assert.deepEqual(capture.executed, [
{ marker: "scoped-run", args: ["ok"] },
]);
});
});
});
90 changes: 90 additions & 0 deletions test/services/extensibility-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,96 @@ describe("extensibilityService", () => {
assert.isTrue(isNpmInstallCalled);
});

it("loads already-installed scoped extensions without re-running npm install", async () => {
const testInjector = getTestInjector();
const fs: IFileSystem = testInjector.resolve("fs");
const extensionNames = ["@scope/extension1", "extension2"];
fs.exists = (): boolean => true;
fs.readDirectory = (dir: string): string[] => {
if (path.basename(dir) === constants.NODE_MODULES_FOLDER_NAME) {
return ["@scope", "extension2"];
}
if (path.basename(dir) === "@scope") {
return ["extension1"];
}
return [];
};

mockFsReadJson(testInjector, extensionNames);

let isNpmInstallCalled = false;
const npm: INodePackageManager = testInjector.resolve("npm");
npm.install = async (): Promise<any> => {
isNpmInstallCalled = true;
return {};
};

const expectedResults: IExtensionData[] = _.map(
extensionNames,
(extensionName) => ({
extensionName,
version: "1.0.0",
pathToExtension: getExpectedInstallationPathForExtension(
testInjector,
extensionName,
),
docs: undefined as any,
}),
);

const extensibilityService: IExtensibilityService =
testInjector.resolve(ExtensibilityService);
const actualResult = await Promise.all(
extensibilityService.loadExtensions(),
);
assert.deepStrictEqual(actualResult, expectedResults);
assert.isFalse(isNpmInstallCalled);
});

it("installs a missing scoped extension when its scope directory exists without the package", async () => {
const testInjector = getTestInjector();
const fs: IFileSystem = testInjector.resolve("fs");
const extensionName = "@scope/missing-ext";
fs.exists = (pathToCheck: string): boolean =>
pathToCheck !== path.resolve(extensionName);
fs.readDirectory = (dir: string): string[] => {
if (path.basename(dir) === constants.NODE_MODULES_FOLDER_NAME) {
return ["@scope"];
}
if (path.basename(dir) === "@scope") {
return ["other-ext"];
}
return [];
};

mockFsReadJson(testInjector, [extensionName]);

const installedPackages: string[] = [];
const npm: INodePackageManager = testInjector.resolve("npm");
npm.install = async (packageName: string): Promise<any> => {
installedPackages.push(packageName);
return { name: packageName };
};

const extensibilityService: IExtensibilityService =
testInjector.resolve(ExtensibilityService);
const actualResult = await Promise.all(
extensibilityService.loadExtensions(),
);
assert.deepStrictEqual(installedPackages, [extensionName]);
assert.deepStrictEqual(actualResult, [
{
extensionName,
version: "1.0.0",
pathToExtension: getExpectedInstallationPathForExtension(
testInjector,
extensionName,
),
docs: undefined as any,
},
]);
});

it("rejects only promises for extensions that cannot be loaded", async () => {
const testInjector = getTestInjector();
const fs: IFileSystem = testInjector.resolve("fs");
Expand Down
Loading