office-addin-dev-settings
Version:
Configure developer settings for Office Add-ins.
120 lines • 6.72 kB
JavaScript
// copyright (c) Microsoft Corporation. All rights reserved.
// licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
exports.unregisterAllAddIns = exports.unregisterAddIn = exports.registerAddIn = exports.getRegisteredAddIns = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const junk_1 = tslib_1.__importDefault(require("junk"));
const office_addin_manifest_1 = require("office-addin-manifest");
const os_1 = tslib_1.__importDefault(require("os"));
const path_1 = tslib_1.__importDefault(require("path"));
const dev_settings_1 = require("./dev-settings");
const office_addin_usage_data_1 = require("office-addin-usage-data");
const publish_1 = require("./publish");
const path_2 = tslib_1.__importDefault(require("path"));
function getRegisteredAddIns() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const registeredAddins = [];
for (const app of (0, office_addin_manifest_1.getOfficeApps)()) {
const sideloadDirectory = getSideloadDirectory(app);
if (sideloadDirectory && fs_extra_1.default.existsSync(sideloadDirectory)) {
for (const fileName of fs_extra_1.default.readdirSync(sideloadDirectory).filter(junk_1.default.not)) {
const manifestPath = fs_extra_1.default.realpathSync(path_1.default.join(sideloadDirectory, fileName));
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
registeredAddins.push(new dev_settings_1.RegisteredAddin(manifest.id || "", manifestPath));
}
}
}
return registeredAddins;
});
}
exports.getRegisteredAddIns = getRegisteredAddIns;
function getSideloadDirectory(app) {
switch (app) {
case office_addin_manifest_1.OfficeApp.Excel:
return path_1.default.join(os_1.default.homedir(), "Library/Containers/com.microsoft.Excel/Data/Documents/wef");
case office_addin_manifest_1.OfficeApp.PowerPoint:
return path_1.default.join(os_1.default.homedir(), "Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef");
case office_addin_manifest_1.OfficeApp.Word:
return path_1.default.join(os_1.default.homedir(), "Library/Containers/com.microsoft.Word/Data/Documents/wef");
}
}
function registerAddIn(manifestPath, officeApps, registration) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
if (!officeApps) {
officeApps = (0, office_addin_manifest_1.getOfficeAppsForManifestHosts)(manifest.hosts);
if (officeApps.length === 0) {
throw new office_addin_usage_data_1.ExpectedError("The manifest file doesn't specify any hosts for the Office Add-in.");
}
}
if (!manifest.id) {
throw new office_addin_usage_data_1.ExpectedError("The manifest file doesn't contain the id of the Office Add-in.");
}
if (manifestPath.endsWith(".json")) {
if (!registration) {
const targetPath = path_2.default.join(os_1.default.tmpdir(), "manifest.zip");
const zipPath = yield (0, office_addin_manifest_1.exportMetadataPackage)(targetPath, manifestPath);
registration = yield (0, publish_1.registerWithTeams)(zipPath);
}
// TODO: Save registration in "OutlookSideloadManifestPath" as "TitleId"
// and add support for refreshing add-ins in Outlook via registry key
}
else if (manifestPath.endsWith(".xml")) {
// TODO: Look for "Outlook" in manifests.hosts and enable outlook sideloading if there.
// and add support for refreshing add-ins in Outlook via registry key
}
// Save manifest path in "registry"
for (const app of officeApps) {
const sideloadDirectory = getSideloadDirectory(app);
if (sideloadDirectory) {
// include manifest id in sideload filename
const sideloadPath = path_1.default.join(sideloadDirectory, `${manifest.id}.${path_1.default.basename(manifestPath)}`);
fs_extra_1.default.ensureDirSync(sideloadDirectory);
fs_extra_1.default.ensureLinkSync(manifestPath, sideloadPath);
}
}
}
catch (err) {
throw new Error(`Unable to register the Office Add-in.\n${err}`);
}
});
}
exports.registerAddIn = registerAddIn;
function unregisterAddIn(addinId, manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!addinId) {
throw new office_addin_usage_data_1.ExpectedError("The manifest file doesn't contain the id of the Office Add-in.");
}
const registeredAddIns = yield getRegisteredAddIns();
for (const registeredAddIn of registeredAddIns) {
const registeredFileName = path_1.default.basename(registeredAddIn.manifestPath);
const manifestFileName = path_1.default.basename(manifestPath);
if (registeredFileName === manifestFileName || registeredFileName.startsWith(addinId)) {
if (!registeredFileName.endsWith(".xml")) {
(0, publish_1.uninstallWithTeams)(registeredFileName.substring(registeredFileName.indexOf(".") + 1));
// TODO: Add support for refreshing add-ins in Outlook via registry key
}
fs_extra_1.default.unlinkSync(registeredAddIn.manifestPath);
}
}
});
}
exports.unregisterAddIn = unregisterAddIn;
function unregisterAllAddIns() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const registeredAddIns = yield getRegisteredAddIns();
for (const registeredAddIn of registeredAddIns) {
const registeredFileName = path_1.default.basename(registeredAddIn.manifestPath);
if (!registeredFileName.endsWith(".xml")) {
(0, publish_1.uninstallWithTeams)(registeredFileName.substring(registeredFileName.indexOf(".") + 1));
// TODO: Add support for refreshing add-ins in Outlook via registry key
}
fs_extra_1.default.unlinkSync(registeredAddIn.manifestPath);
}
});
}
exports.unregisterAllAddIns = unregisterAllAddIns;
//# sourceMappingURL=dev-settings-mac.js.map
;