-
Notifications
You must be signed in to change notification settings - Fork 205
/
index.ts
executable file
·359 lines (326 loc) · 13.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
import * as fs from 'fs';
import * as path from 'path';
import * as appInsights from 'applicationinsights';
import * as chalk from 'chalk';
import * as _ from 'lodash';
import * as opn from 'opn';
import * as uuid from 'uuid/v4';
import * as yosay from 'yosay';
import * as yo from 'yeoman-generator';
let insight = appInsights.getClient('68a8ef35-112c-4d33-a118-3c346947f2fe');
module.exports = yo.extend({
/**
* Setup the generator
*/
constructor: function () {
yo.apply(this, arguments);
this.argument('name', { type: String, required: false });
this.argument('host', { type: String, required: false });
this.argument('framework', { type: String, required: false });
this.option('skip-install', {
type: Boolean,
required: false,
desc: 'Skip running `npm install` post scaffolding.'
});
this.option('js', {
type: Boolean,
required: false,
desc: 'Use JavaScript templates instead of TypeScript.'
});
},
/**
* Generator initalization
*/
initializing: function () {
let message = `Welcome to the ${chalk.bold.green('Office Add-in')} generator, by ${chalk.bold.green('@OfficeDev')}! Let\'s create a project together!`;
this.log(yosay(message));
this.project = {};
},
/**
* Prompt users for options
*/
prompting: async function () {
try {
let jsTemplates = getDirectories(this.templatePath('js'));
let tsTemplates = getDirectories(this.templatePath('ts'));
let manifests = getFiles(this.templatePath('manifest')).map(manifest => _.capitalize(manifest.replace('.xml', '')));
updateHostNames(manifests, 'Onenote', 'OneNote');
updateHostNames(manifests, 'Powerpoint', 'PowerPoint');
/** begin prompting */
/** whether to create a new folder for the project */
let startForFolder = (new Date()).getTime();
let askForFolder = [{
name: 'folder',
message: 'Would you like to create a new subfolder for your project?',
type: 'confirm',
default: false
}];
let answerForFolder = await this.prompt(askForFolder);
let endForFolder = (new Date()).getTime();
let durationForFolder = (endForFolder - startForFolder) / 1000;
/** name for the project */
let startForName = (new Date()).getTime();
let askForName = [{
name: 'name',
type: 'input',
message: 'What do you want to name your add-in?',
default: 'My Office Add-in',
when: this.options.name == null
}];
let answerForName = await this.prompt(askForName);
let endForName = (new Date()).getTime();
let durationForName = (endForName - startForName) / 1000;
/** office client application that can host the addin */
let startForHost = (new Date()).getTime();
let askForHost = [{
name: 'host',
message: 'Which Office client application would you like to support?',
type: 'list',
default: 'Excel',
choices: manifests.map(manifest => ({ name: manifest, value: manifest })),
when: this.options.host == null
}];
let answerForHost = await this.prompt(askForHost);
let endForHost = (new Date()).getTime();
let durationForHost = (endForHost - startForHost) / 1000;
/** set flag for manifest-only to prompt accordingly later */
let startForManifestOnly = (new Date()).getTime();
let askForManifestOnly = [{
name: 'isManifestOnly',
message: 'Would you like to create a new add-in?',
type: 'list',
default: false,
choices: [
{
name: 'Yes, I want a new add-in.',
value: false
},
{
name: 'No, I only need the manifest file.',
value: true
}
],
when: this.options.framework == null
}];
let answerForManifestOnly = await this.prompt(askForManifestOnly); // trigger prompts and store user input
let endForManifestOnly = (new Date()).getTime();
let durationForManifestOnly = (endForManifestOnly - startForManifestOnly) / 1000;
/**
* Configure user input to have correct values
*/
this.project = {
folder: answerForFolder.folder,
name: this.options.name || answerForName.name,
host: this.options.host || answerForHost.host,
framework: this.options.framework || null,
isManifestOnly: answerForManifestOnly.isManifestOnly
};
if (answerForManifestOnly.isManifestOnly) {
this.project.framework = 'manifest-only';
}
if (this.options.framework != null) {
if (this.options.framework === 'manifest-only') {
this.project.isManifestOnly = true;
} else {
this.project.isManifestOnly = false;
}
}
/** askForTs and askForFramework will only be triggered if it's not a manifest-only project */
/** use TypeScript for the project */
let startForTs = (new Date()).getTime();
let askForTs = [
{
name: 'ts',
type: 'confirm',
message: 'Would you like to use TypeScript?',
default: true,
when: (this.options.js == null) && (!this.project.isManifestOnly) && (this.options.framework !== 'react')
}
];
let answerForTs = await this.prompt(askForTs);
let endForTs = (new Date()).getTime();
let durationForTs = (endForTs - startForTs) / 1000;
if (!(this.options.js == null)) {
this.project.ts = !this.options.js;
}
else {
this.project.ts = answerForTs.ts || false;
}
if (this.options.framework === 'react') {
this.project.ts = true;
}
/** technology used to create the addin (html / angular / etc) */
let startForFramework = (new Date()).getTime();
let askForFramework = [
{
name: 'framework',
message: 'Choose a framework:',
type: 'list',
default: 'react',
choices: tsTemplates.map(template => ({ name: _.capitalize(template), value: template })),
when: (this.project.framework == null) && this.project.ts && !this.options.js && !answerForManifestOnly.isManifestOnly
},
{
name: 'framework',
message: 'Choose a framework:',
type: 'list',
default: 'jquery',
choices: jsTemplates.map(template => ({ name: _.capitalize(template), value: template })),
when: (this.project.framework == null) && !this.project.ts && this.options.js && !answerForManifestOnly.isManifestOnly
}
];
let answerForFramework = await this.prompt(askForFramework);
let endForFramework = (new Date()).getTime();
let durationForFramework = (endForFramework - startForFramework) / 1000;
if (!(this.options.framework == null)) {
this.project.framework = this.options.framework;
}
else if (this.project.isManifestOnly === true) {
this.project.framework = 'manifest-only';
}
else {
this.project.framework = answerForFramework.framework;
}
let startForResourcePage = (new Date()).getTime();
this.log('\nFor more information and resources on your next steps, we have created a resource.html file in your project.');
let askForOpenResourcePage = [
/** ask to open resource page */
{
name: 'open',
type: 'confirm',
message: 'Would you like to open it now while we finish creating your project?',
default: true
}
];
let answerForOpenResourcePage = await this.prompt(askForOpenResourcePage);
let endForResourcePage = (new Date()).getTime();
let durationForResourcePage = (endForResourcePage - startForResourcePage) / 1000;
this.project.isResourcePageOpened = answerForOpenResourcePage.open;
this.project.duration = (endForResourcePage - startForFolder) / 1000;
/** appInsights logging */
insight.trackEvent('Folder', { CreatedSubFolder: this.project.folder.toString() }, { durationForFolder });
insight.trackEvent('Name', { Name: this.project.name }, { durationForName });
insight.trackEvent('Host', { Host: this.project.host }, { durationForHost });
insight.trackEvent('IsManifestOnly', { IsManifestOnly: this.project.isManifestOnly.toString() }, { durationForManifestOnly });
insight.trackEvent('IsResourcePageOpened', { IsResourcePageOpened: this.project.isResourcePageOpened.toString() }, { durationForResourcePage });
if (this.project.isManifestOnly === false) {
insight.trackEvent('IsTs', { IsTs: this.project.ts.toString() }, { durationForTs });
insight.trackEvent('Framework', { Framework: this.project.framework }, { durationForFramework });
}
} catch (err) {
insight.trackException(new Error('Prompting Error: ' + err));
}
},
/**
* save configs & config project
*/
configuring: function () {
try {
this.project.projectInternalName = _.kebabCase(this.project.name);
this.project.projectDisplayName = this.project.name;
this.project.projectId = uuid();
this.project.hostInternalName = _.toLower(this.project.host);
if (this.project.folder) {
this.destinationRoot(this.project.projectInternalName);
}
let duration = this.project.duration;
insight.trackEvent('App_Data', { AppID: this.project.projectId, Host: this.project.host, Framework: this.project.framework, isTypeScript: this.project.ts.toString() }, { duration });
} catch (err) {
insight.trackException(new Error('Configuration Error: ' + err));
}
},
writing: {
copyFiles: function () {
try {
let language = this.project.ts ? 'ts' : 'js';
/** Show type of project creating in progress */
if (this.project.framework !== 'manifest-only') {
this.log('\n----------------------------------------------------------------------------------\n');
this.log(` Creating ${chalk.bold.green(this.project.projectDisplayName)} add-in using ${chalk.bold.magenta(language)} and ${chalk.bold.cyan(this.project.framework)}\n`);
this.log('----------------------------------------------------------------------------------\n\n');
}
else {
this.log('----------------------------------------------------------------------------------\n');
this.log(` Creating manifest for ${chalk.bold.green(this.project.projectDisplayName)} add-in\n`);
this.log('----------------------------------------------------------------------------------\n\n');
}
/** Copy the manifest */
this.fs.copyTpl(this.templatePath(`manifest/${this.project.hostInternalName}.xml`), this.destinationPath(`${this.project.projectInternalName}-manifest.xml`), this.project);
if (this.project.framework === 'manifest-only') {
this.fs.copyTpl(this.templatePath(`manifest-only/**`), this.destinationPath(), this.project);
}
else {
/** Copy the base template */
this.fs.copy(this.templatePath(`${language}/base/**`), this.destinationPath());
/** Copy the framework specific overrides */
this.fs.copyTpl(this.templatePath(`${language}/${this.project.framework}/**`), this.destinationPath(), this.project);
}
} catch (err) {
insight.trackException(new Error('File Copy Error: ' + err));
}
}
},
install: function () {
try {
if (this.project.isResourcePageOpened) {
opn(`resource.html`);
}
if (this.options['skip-install']) {
this.installDependencies({
npm: false,
bower: false,
callback: this._postInstallHints.bind(this)
});
}
else {
this.installDependencies({
npm: true,
bower: false,
callback: this._exitProcess.bind(this)
});
}
} catch (err) {
insight.trackException(new Error('Installation Error: ' + err));
process.exitCode = 1;
}
},
_postInstallHints: function () {
/** Next steps and npm commands */
this.log('----------------------------------------------------------------------------------------------------------\n');
this.log(` ${chalk.green('Congratulations!')} Your add-in has been created! Your next steps:\n`);
this.log(` 1. Launch your local web server via ${chalk.inverse(' npm start ')} (you may also need to`);
this.log(` trust the Self-Signed Certificate for the site if you haven't done that)`);
this.log(` 2. Sideload the add-in into your Office application.\n`);
this.log(` Please refer to resource.html in your project for more information.`);
this.log(` Or visit our repo at: https://github.com/officeDev/generator-office \n`);
this.log('----------------------------------------------------------------------------------------------------------\n');
this._exitProcess();
},
_exitProcess: function () {
process.exit();
}
} as any);
function getDirectories(root) {
return fs.readdirSync(root).filter(file => {
if (file === 'base') {
return false;
}
return fs.statSync(path.join(root, file)).isDirectory();
});
}
function getFiles(root) {
return fs.readdirSync(root).filter(file => {
return !(fs.statSync(path.join(root, file)).isDirectory());
});
}
function updateHostNames(arr, key, newval) {
let match = _.some(arr, _.method('match', key));
if (match) {
let index = _.indexOf(arr, key);
arr.splice(index, 1, newval);
}
}