120 lines
6.2 KiB
JavaScript
120 lines
6.2 KiB
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 = require("fs-extra");
|
||
|
const junk = require("junk");
|
||
|
const office_addin_manifest_1 = require("office-addin-manifest");
|
||
|
const os = require("os");
|
||
|
const path = 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 fspath = require("path");
|
||
|
/* global process */
|
||
|
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.existsSync(sideloadDirectory)) {
|
||
|
for (const fileName of fs.readdirSync(sideloadDirectory).filter(junk.not)) {
|
||
|
const manifestPath = fs.realpathSync(path.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.join(os.homedir(), "Library/Containers/com.microsoft.Excel/Data/Documents/wef");
|
||
|
case office_addin_manifest_1.OfficeApp.PowerPoint:
|
||
|
return path.join(os.homedir(), "Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef");
|
||
|
case office_addin_manifest_1.OfficeApp.Word:
|
||
|
return path.join(os.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 = fspath.join(os.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.join(sideloadDirectory, `${manifest.id}.${path.basename(manifestPath)}`);
|
||
|
fs.ensureDirSync(sideloadDirectory);
|
||
|
fs.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.basename(registeredAddIn.manifestPath);
|
||
|
const manifestFileName = path.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.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.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.unlinkSync(registeredAddIn.manifestPath);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
exports.unregisterAllAddIns = unregisterAllAddIns;
|
||
|
//# sourceMappingURL=dev-settings-mac.js.map
|