Skip to content

Commit 5a0bb2c

Browse files
committed
office-addin-manifest modify: allow --guid with optional value, --displayName with required value
If an option is provided with an optional value, commander specifies the option as a boolean. To help with this, I've added the function getCommandOptionString(option: string | boolean): string | undefined. I updated the options so that --guid take an optional value, while --displayName requires a value to be provided. This command line will change the guid in the manifest to a random value: office-addin-manifest modify manifest.xml --guid The special value "random" will also do this: office-addin-manifest modify manifest.xml --guid random As will an empty value: office-addin-manifest modify manifest.xml --guid "" If a value is provided, it will be used. office-addin-manifest modify manifest.xml --guid 978d16a5-08b0-4ab3-a27c-816c89189fd6
1 parent 9b1d297 commit 5a0bb2c

5 files changed

Lines changed: 30 additions & 25 deletions

File tree

packages/office-addin-manifest/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@types/node": "^10.5.7",
3434
"@types/node-fetch": "^2.1.2",
3535
"@types/uuid": "^3.4.4",
36+
"@types/validator": "^9.4.2",
3637
"@types/xml2js": "^0.4.3",
3738
"concurrently": "^3.6.1",
3839
"copy-dir": "^0.4.0",

packages/office-addin-manifest/src/commands.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
import * as commnder from "commander";
55
import * as manifestInfo from "./manifestInfo";
66

7+
function getCommandOptionString(option: string | boolean, defaultValue?: string): string | undefined {
8+
// For a command option defined with an optional value, e.g. "--option [value]",
9+
// when the option is provided with a value, it will be of type "string", return the specified value;
10+
// when the option is provided without a value, it will be of type "boolean", return undefined.
11+
return (typeof(option) === "boolean") ? defaultValue : option;
12+
}
13+
714
export async function info(manifestPath: string) {
815
try {
916
const manifest = await manifestInfo.readManifestFile(manifestPath);
@@ -30,16 +37,13 @@ function logManifestInfo(manifestPath: string, manifest: manifestInfo.ManifestIn
3037

3138
export async function modify(manifestPath: string, command: commnder.Command) {
3239
try {
33-
const guid: string | undefined = (command.guid) ? command.guid : undefined;
34-
const displayName: string | undefined = (command.displayName) ? command.displayName : undefined;
35-
36-
if (guid === undefined && displayName === undefined) {
37-
throw new Error("You need to specify something to change in the manifest.");
38-
}
40+
// if the --guid command option is provided without a value, use "" to specify to change to a random guid value.
41+
const guid: string | undefined = getCommandOptionString(command.guid, "");
42+
const displayName: string | undefined = getCommandOptionString(command.displayName);
3943

4044
const manifest = await manifestInfo.modifyManifestFile(manifestPath, guid, displayName);
4145
logManifestInfo(manifestPath, manifest);
4246
} catch (err) {
4347
logErrorMessage(err);
4448
}
45-
}
49+
}

packages/office-addin-manifest/src/manifest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ if (process.argv[1].endsWith("\\manifest.js")) {
1313

1414
commander
1515
.command("modify <manifest-path>")
16-
.option("-g,--guid [guid]", "Change the guid. Specify \"random\" for a random guid.")
17-
.option("-d, --displayName [name]", "Display name for the add-in.")
16+
.option("-g,--guid [guid]", "Change the guid. A random guid is used if one is not provided.")
17+
.option("-d, --displayName <name>", "Change the display name.")
1818
.action(commands.modify);
1919

2020
commander.parse(process.argv);

packages/office-addin-manifest/src/manifestInfo.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function parseManifest(xml: Xml): ManifestInfo {
3535
export async function modifyManifestFile(manifestPath: string, guid?: string, displayName?: string): Promise<ManifestInfo> {
3636
let manifestData: ManifestInfo = {};
3737
if (manifestPath) {
38-
if (!guid && !displayName) {
38+
if (guid === undefined && displayName === undefined) {
3939
throw new Error("You need to specify something to change in the manifest.");
4040
} else {
4141
manifestData = await modifyManifestXml(manifestPath, guid, displayName);
@@ -85,15 +85,15 @@ async function readXmlFromManifestFile(manifestPath: string): Promise<Xml> {
8585
return xml;
8686
}
8787

88-
function setModifiedXmlData(xml: any, guid: string | undefined, displayName: string | undefined) {
89-
if (guid) {
90-
if (guid === "random") {
88+
function setModifiedXmlData(xml: any, guid: string | undefined, displayName: string | undefined): void {
89+
if (typeof(guid) !== "undefined") {
90+
if (!guid || guid === "random") {
9191
guid = uuid();
9292
}
9393
xmlMethods.setXmlElementValue(xml, "Id", guid);
9494
}
9595

96-
if (displayName) {
96+
if (typeof(displayName) !== "undefined") {
9797
xmlMethods.setXmlElementAttributeValue(xml, "DisplayName", displayName);
9898
}
9999
}

packages/office-addin-manifest/test/test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as assert from "assert";
22
import * as fs from "fs";
33
import * as mocha from "mocha";
4+
import * as uuid from "uuid";
5+
import { isUUID } from "validator";
46
import * as manifestInfo from "../src/manifestInfo";
5-
const uuid = require('uuid');
6-
const validator = require("validator");
77
const manifestOriginalFolder = process.cwd() + "/test/manifests";
88
const manifestTestFolder = process.cwd() + "\\testExecution\\testManifests";
99
const testManifest = manifestTestFolder + "\\manifest.xml";
@@ -70,9 +70,9 @@ describe("Manifest", function() {
7070
const updatedInfo = await manifestInfo.modifyManifestFile(testManifest, "random", undefined);
7171

7272
// verify guid updated, that it"s a valid guid and that the displayName is not updated
73-
assert.notStrictEqual(originalInfo.id, updatedInfo.id);
74-
assert.equal(true, validator.isUUID(updatedInfo.id));
75-
assert.strictEqual(originalInfo.displayName, updatedInfo.displayName);
73+
assert.notStrictEqual(updatedInfo.id, originalInfo.id);
74+
assert.strictEqual(updatedInfo.id && isUUID(updatedInfo.id), true);
75+
assert.strictEqual(updatedInfo.displayName, originalInfo.displayName);
7676
});
7777
it("should handle specifying displayName only", async function() {
7878
// get original manifest info and create copy of manifest that we can overwrite in this test
@@ -83,18 +83,18 @@ describe("Manifest", function() {
8383
const updatedInfo = await manifestInfo.modifyManifestFile(testManifest, undefined, testDisplayName);
8484

8585
// verify displayName updated and guid not updated
86-
assert.notStrictEqual(originalInfo.displayName, updatedInfo.displayName);
86+
assert.notStrictEqual(updatedInfo.displayName, originalInfo.displayName);
8787
assert.strictEqual(updatedInfo.displayName, testDisplayName);
88-
assert.strictEqual(originalInfo.id, updatedInfo.id);
88+
assert.strictEqual( updatedInfo.id, originalInfo.id);
8989
});
9090
it("should handle not specifying either a guid or displayName", async function() {
9191
let result;
9292
try {
93-
await manifestInfo.modifyManifestFile(testManifest, undefined, undefined);
93+
await manifestInfo.modifyManifestFile(testManifest);
9494
} catch (err) {
95-
result = err;
95+
result = err.message;
9696
}
97-
assert.equal(result, "Error: You need to specify something to change in the manifest.");
97+
assert.strictEqual(result, `You need to specify something to change in the manifest.`);
9898
});
9999
it("should handle an invalid manifest file path", async function() {
100100
// call modify, specifying an invalid manifest path with a valid guid and displayName
@@ -108,7 +108,7 @@ describe("Manifest", function() {
108108
result = err.message;
109109
}
110110

111-
assert.equal(result, `Unable to modify xml data for manifest file: ${invalidManifest}. \nError: ENOENT: no such file or directory, open '${invalidManifest}'`);
111+
assert.strictEqual(result, `Unable to modify xml data for manifest file: ${invalidManifest}. \nError: ENOENT: no such file or directory, open '${invalidManifest}'`);
112112
});
113113
});
114114
});

0 commit comments

Comments
 (0)