-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
281 lines (244 loc) · 8.2 KB
/
gulpfile.js
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
'use script';
var gulp = require('gulp');
var jsx = require('gulp-jsx');
var rename = require("gulp-rename");
var ts = require('gulp-typescript');
var webserver = require('gulp-webserver');
var fs = require('fs');
var minimist = require('minimist');
var xmllint = require('xmllint');
var chalk = require('chalk');
var $ = require('gulp-load-plugins')({ lazy: true });
var del = require('del');
var runSequence = require('run-sequence');
var Xml2Js = require('xml2js');
var config = {
release: './dist'
};
gulp.task('ts', function () {
return gulp.src('src/**/*.ts')
.pipe(ts({
noImplicitAny: true,
target: "es5",
outDir: 'maints.js'
}))
.pipe(gulp.dest('dist'));
});
gulp.task('jsx', function() {
return gulp.src('src/**/*.jsx')
.pipe(jsx({
factory: 'React.createClass'
}))
.pipe(rename({extname: ".js"}))
.pipe(gulp.dest('dist'));
});
var filesToCopy = ['src/**/*.js','src/**/*.htm*'];
gulp.task('copyfiles', function (){
return gulp.src(filesToCopy)
.pipe(gulp.dest('dist'));
});
gulp.task('help', $.taskListing.withFilters(function (task) {
var mainTasks = ['default', 'help', 'serve-web', 'validate', 'dist'];
var isSubTask = mainTasks.indexOf(task) < 0;
return isSubTask;
}));
gulp.task('default', ['help']);
/** +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ **/
function runTasks(staticServerTask){
gulp.watch(['src/**/*.jsx'], ['jsx']);
gulp.watch(['src/**/*.ts'], ['ts']);
gulp.watch(filesToCopy, ['copyfiles']);
runSequence('copyfiles',
'jsx',
'ts',
staticServerTask);
}
/**
* Startup webserver.
*/
gulp.task('serve-web', function () {
runTasks('serve-static');
});
gulp.task('serve-web-insecure', function () {
runTasks('serve-static-insecure');
});
gulp.task('serve-static', function () {
gulp.src('.')
.pipe(webserver({
https: true,
port: '8446',
host: '0.0.0.0',
directoryListing: true,
fallback: 'index.html'
}));
});
gulp.task('serve-static-insecure', function () {
gulp.src('.')
.pipe(webserver({
https: false,
port: '8444',
host: '0.0.0.0',
directoryListing: true,
fallback: 'index.html'
}));
});
/**
* Validates the Office add-in for both catalog and store publishing
*/
gulp.task('validate', ['validate-forstore'], function() {
});
/**
* Validates the Office add-in for submission to the Add-in catalog
*/
gulp.task('validate-forcatalog', ['validate-xml', 'validate-highResolutionIconUrl'], function() {
});
/**
* Validates the Office add-in for submission to the Office Store
*/
gulp.task('validate-forstore', ['validate-xml', 'validate-highResolutionIconUrl'], function() {
var options = minimist(process.argv.slice(2));
var xmlFilePath = options.xmlfile;
var xml = fs.readFileSync(xmlFilePath, 'utf-8');
var parser = new Xml2Js.Parser();
parser.parseString(xml, function(err, manifestJson) {
// 5.7. Apps and add-ins must use SSL
if (manifestJson.OfficeApp.DefaultSettings[0].SourceLocation[0].$.DefaultValue.indexOf('https://') !== 0) {
console.log(chalk.red('ERROR: 5.7. Apps and add-ins must be secured with a valid ' +
'and trusted SSL certificate (HTTPS).'));
console.log(chalk.blue('FIX: Change the URL of OfficeApp/DefaultSettings/SourceLocation/DefaultValue (' +
manifestJson.OfficeApp.DefaultSettings[0].SourceLocation[0].$.DefaultValue) + ') to https://');
}
// 5.10. icon must be present
if (!manifestJson.OfficeApp.HighResolutionIconUrl ||
manifestJson.OfficeApp.HighResolutionIconUrl.length === 0) {
console.log(chalk.red('ERROR: 5.10. You must specify an icon for your app or add-in in your ' +
'add-in package or manifest'));
console.log(chalk.blue('FIX: Add the HighResolutionIconUrl element to the manifest, eg. ' +
'<HighResolutionIconUrl DefaultValue="https://contoso.com/myicon.png" />'));
}
// 7.16. Support URL is required
if (!manifestJson.OfficeApp.SupportUrl) {
console.log(chalk.red('ERROR: 7.16. You must specify a valid Support URL in the SupportURL element ' +
'of your Office Add-in manifest.'));
console.log(chalk.blue('FIX: Add the SupportUrl element to the manifest, eg. ' +
'<SupportUrl DefaultValue="http://contoso.com/support" />'));
}
// 10.9. Add-ins must use v1.1 of the schema
if (manifestJson.OfficeApp.$.xmlns !== 'http://schemas.microsoft.com/office/appforoffice/1.1') {
console.log(chalk.red('ERROR: 10.9. Office Add-ins must use version 1.1 of the Office.js library ' +
'and the manifest schema.'));
console.log(chalk.blue('FIX: In the OfficeApp element change the value of the xlmns attribute to ' +
'http://schemas.microsoft.com/office/appforoffice/1.1'));
}
});
var index = fs.readFileSync('index.html', 'utf-8').toLowerCase();
// 7.15. Add-ins must use hosted Office.js file
if (index.indexOf('//appsforoffice.microsoft.com/lib/1.1/hosted/office.js') < 0 &&
index.indexOf('//appsforoffice.microsoft.com/lib/1/hosted/office.js') < 0) {
console.log(chalk.red('ERROR: 7.15. All Office Add-ins must use the Microsoft-hosted Office.js file.'));
console.log(chalk.blue('FIX: In the index.html change all references to office.js to ' +
'https://appsforoffice.microsoft.com/lib/1/hosted/office.js'));
}
});
/**
* Validates the Office add-in manifest against XSD
*/
gulp.task('validate-xml', function () {
var options = minimist(process.argv.slice(2));
var xsd = fs.readFileSync('./manifest.xsd');
var xmlFilePath = options.xmlfile;
var xml = fs.readFileSync(xmlFilePath);
var result = xmllint.validateXML({
xml: xml,
schema: xsd
});
if (result.errors !== null) {
console.log(chalk.red('Manifest XML invalid'));
result.errors.forEach(function (e) {
console.log(chalk.red(e));
});
}
});
/**
* Validates the URL of the add-in icon
*/
gulp.task('validate-highResolutionIconUrl', function() {
var options = minimist(process.argv.slice(2));
var xmlFilePath = options.xmlfile;
var xml = fs.readFileSync(xmlFilePath, 'utf-8');
var parser = new Xml2Js.Parser();
parser.parseString(xml, function(err, manifestJson) {
if (manifestJson.OfficeApp.HighResolutionIconUrl &&
manifestJson.OfficeApp.HighResolutionIconUrl.length > 0 &&
manifestJson.OfficeApp.HighResolutionIconUrl[0].$ &&
manifestJson.OfficeApp.HighResolutionIconUrl[0].$.DefaultValue) {
var iconUrl = manifestJson.OfficeApp.HighResolutionIconUrl[0].$.DefaultValue;
if (iconUrl.indexOf('https://') < 0) {
console.log(chalk.red('ERROR: The value of the HighResolutionIconUrl attribute contains an unsupported URL.' +
' You can only use https:// URLs.'));
}
if (!/^.+\.(png|jpg|jpeg)$/.test(iconUrl)) {
console.log(chalk.red('ERROR: The URL of your app icon must end with one of the following extensions:' +
' png, jp(e)g'));
}
}
});
});
/**
* Removes existing dist folder
*/
gulp.task('dist-remove', function () {
return del(config.release);
});
/**
* Copies files to the dist folder
*/
gulp.task('dist-copy-files', function() {
return gulp.src([
'./app*/**/*',
'./bower_components/**/*',
'./content/**/*',
'./images/**/*',
'./scripts/**/*',
'./manifest-*.xml',
'./index.html',
'./package.json'
], { base: './' }).pipe(gulp.dest(config.release));
});
/**
* Optimizes JavaScript and CSS files
*/
gulp.task('dist-minify', ['dist-minify-js', 'dist-minify-css'], function() {
});
/**
* Minifies and uglifies JavaScript files
*/
gulp.task('dist-minify-js', function() {
gulp.src([
'./src/**/*.js',
'./scripts/**/*', '!./scripts/MicrosoftAjax.js'
], { base: './' })
.pipe($.uglify())
.pipe(gulp.dest(config.release));
});
/**
* Minifies and uglifies CSS files
*/
gulp.task('dist-minify-css', function() {
gulp.src([
'./app*/**/*.css',
'./content/**/*.css'
], { base: './' })
.pipe($.minifyCss())
.pipe(gulp.dest(config.release));
});
/**
* Creates a release version of the project
*/
gulp.task('dist', function () {
runSequence(
['dist-remove'],
['dist-copy-files'],
['dist-minify']
);
});