Skip to content

Commit 23b57e9

Browse files
authored
Merge pull request #17 from akrantz/manifest-command-fixes
office-addin-manifest command fixes
2 parents 1002589 + 5a0bb2c commit 23b57e9

5 files changed

Lines changed: 48 additions & 38 deletions

File tree

packages/office-addin-manifest/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"@types/mocha": "^5.2.5",
3333
"@types/node": "^10.5.7",
3434
"@types/node-fetch": "^2.1.2",
35+
"@types/uuid": "^3.4.4",
36+
"@types/validator": "^9.4.2",
3537
"@types/xml2js": "^0.4.3",
3638
"concurrently": "^3.6.1",
3739
"copy-dir": "^0.4.0",

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

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

7-
export async function info(path: string) {
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+
14+
export async function info(manifestPath: string) {
815
try {
9-
const manifest = await manifestInfo.readManifestFile(path);
10-
logManifestInfo(path, manifest);
16+
const manifest = await manifestInfo.readManifestFile(manifestPath);
17+
logManifestInfo(manifestPath, manifest);
1118
} catch (err) {
12-
console.error(`Error: ${err}`);
19+
logErrorMessage(err);
1320
}
1421
}
1522

16-
function logManifestInfo(path: string, manifest: manifestInfo.ManifestInfo) {
17-
console.log(`Manifest: ${path}`);
23+
function logErrorMessage(err: any) {
24+
console.error(`Error: ${err instanceof Error ? err.message : err}`);
25+
}
26+
27+
function logManifestInfo(manifestPath: string, manifest: manifestInfo.ManifestInfo) {
28+
console.log(`Manifest: ${manifestPath}`);
1829
console.log(` Id: ${manifest.id || ""}`);
1930
console.log(` Name: ${manifest.displayName || ""}`);
2031
console.log(` Provider: ${manifest.providerName || ""}`);
@@ -24,18 +35,15 @@ function logManifestInfo(path: string, manifest: manifestInfo.ManifestInfo) {
2435
console.log(` Description: ${manifest.description || ""}`);
2536
}
2637

27-
export async function modify(path: string, command: commnder.Command) {
38+
export async function modify(manifestPath: string, command: commnder.Command) {
2839
try {
29-
const guid: string | undefined = (command.guid) ? command.guid : undefined;
30-
const displayName: string | undefined = (command.displayName) ? command.displayName : undefined;
31-
32-
if (guid === undefined && displayName === undefined) {
33-
throw new Error("You need to specify something to change in the manifest.");
34-
}
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);
3543

36-
const manifest = await manifestInfo.modifyManifestFile(path, guid, displayName);
37-
logManifestInfo(path, manifest);
44+
const manifest = await manifestInfo.modifyManifestFile(manifestPath, guid, displayName);
45+
logManifestInfo(manifestPath, manifest);
3846
} catch (err) {
39-
console.error(`Error: ${err}`);
47+
logErrorMessage(err);
4048
}
41-
}
49+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import * as commands from "./commands";
88

99
if (process.argv[1].endsWith("\\manifest.js")) {
1010
commander
11-
.command("info <path>")
11+
.command("info <manifest-path>")
1212
.action(commands.info);
1313

1414
commander
15-
.command("modify <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.")
15+
.command("modify <manifest-path>")
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as fs from "fs";
22
import * as util from "util";
3+
import * as uuid from "uuid";
34
import * as xml2js from "xml2js";
45
import * as xmlMethods from "./xml";
56
const readFileAsync = util.promisify(fs.readFile);
6-
const uuid = require('uuid/v1');
77
const writeFileAsync = util.promisify(fs.writeFile);
88
type Xml = any;
99

@@ -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)