-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathRokuDeploy.ts
1030 lines (879 loc) · 37.4 KB
/
RokuDeploy.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as path from 'path';
import * as _fsExtra from 'fs-extra';
import * as Q from 'q';
import * as globAll from 'glob-all';
import * as request from 'request';
import * as archiver from 'archiver';
import * as dateformat from 'dateformat';
import * as errors from './Errors';
export class RokuDeploy {
//store the import on the class to make testing easier
public request = request;
public fsExtra = _fsExtra;
/**
* Copies all of the referenced files to the staging folder
* @param options
*/
public async prepublishToStaging(options: RokuDeployOptions) {
options = this.getOptions(options);
//cast some of the options as not null so we don't have to cast them below
options.rootDir = <string>options.rootDir;
options.outDir = <string>options.outDir;
const files = this.normalizeFilesOption(options.files);
//make all path references absolute
this.makeFilesAbsolute(files, options.rootDir);
let stagingFolderPath = this.getStagingFolderPath(options);
//clean the staging directory
await this.fsExtra.remove(stagingFolderPath);
//make sure the staging folder exists
await this.fsExtra.ensureDir(stagingFolderPath);
await this.copyToStaging(files, stagingFolderPath, options.rootDir);
return stagingFolderPath;
}
/**
* Make all file references absolute
* @param files
* @param rootDir
*/
public makeFilesAbsolute(files: { src: string[]; dest: string }[], rootDir: string) {
//append the rootDir to every relative glob and string file entry
for (let fileEntry of files) {
for (let i = 0; i < fileEntry.src.length; i++) {
let src = fileEntry.src[i];
let isNegated = src.indexOf('!') === 0;
if (isNegated) {
src = src.substring(1);
}
if (path.isAbsolute(src) === false) {
let absoluteSource = path.join(rootDir, src);
if (isNegated) {
absoluteSource = '!' + absoluteSource;
}
fileEntry.src[i] = absoluteSource;
}
}
}
return files;
}
/**
* Determine if a given string ends in a file system slash (\ for windows and / for everything else)
* @param dirPath
*/
public endsWithSlash(dirPath: string) {
if ('string' === typeof dirPath && dirPath.length > 0 &&
(dirPath.lastIndexOf('/') === dirPath.length - 1 || dirPath.lastIndexOf('\\') === dirPath.length - 1)
) {
return true;
} else {
return false;
}
}
/**
* Given an array of files, normalize them into a standard {src;dest} object.
* This will make plain folder names into fully qualified paths, add globs to plain folders, etc.
* This makes it easier to reason about later on in the process.
* @param files
*/
public normalizeFilesOption(files: FilesType[]) {
const result: { src: string[]; dest: string }[] = [];
let topLevelGlobs = <string[]>[];
//standardize the files object
for (let fileEntry of (files as any[])) {
//handle single string top-level globs
if (typeof fileEntry === 'string') {
topLevelGlobs.push(fileEntry);
continue;
//handle src;dest; object with single string for src
} else if (typeof fileEntry.src === 'string') {
fileEntry.src = [fileEntry.src];
}
fileEntry.dest = fileEntry.dest ? fileEntry.dest : '';
//hard-fail if dest is anything other than a string at this point
if ('string' !== typeof fileEntry.dest) {
throw new Error('dest must be a string');
}
//standardize the dest path separator
fileEntry.dest = path.normalize(fileEntry.dest).trim();
if (fileEntry.dest === '' || fileEntry.dest === '.' || fileEntry.dest === '.\\' || fileEntry.dest === './') {
fileEntry.dest = '';
}
//force all slashes to the current platform's version
fileEntry.dest = fileEntry.dest.replace('\\', path.sep).replace('/', path.sep);
if (typeof fileEntry !== 'string' && (!fileEntry || fileEntry.src === null || fileEntry.src === undefined || fileEntry.dest === null || fileEntry.dest === undefined)) {
throw new Error('Entry must be a string or a {src;dest;} object');
}
//if there is a wildcard in any src, ensure a slash in dest
{
let srcContainsWildcard = (fileEntry.src as Array<string>).findIndex((src) => {
return src.indexOf('*') > -1;
}) > -1;
if (fileEntry.dest.length > 0 && !this.endsWithSlash(fileEntry.dest) && srcContainsWildcard) {
fileEntry.dest += path.sep;
}
}
result.push(fileEntry);
}
//if there are any top level globs, add that entry to the beginning
if (topLevelGlobs.length > 0) {
result.splice(0, 0, {
src: topLevelGlobs,
dest: ''
});
}
return result;
}
/**
* Given an already-populated staging folder, create a zip archive of it and copy it to the output folder
* @param options
*/
public async zipPackage(options: RokuDeployOptions) {
options = this.getOptions(options);
let stagingFolderPath = this.getStagingFolderPath(options);
//make sure the output folder exists
await this.fsExtra.ensureDir(options.outDir);
let zipFilePath = this.getOutputZipFilePath(options);
//create a zip of the staging folder
await this.zipFolder(stagingFolderPath, zipFilePath);
//delete the staging folder unless told to retain it.
if (options.retainStagingFolder !== true) {
await this.fsExtra.remove(stagingFolderPath);
}
}
/**
* Create a zip folder containing all of the specified roku project files.
* @param options
*/
public async createPackage(options: RokuDeployOptions, beforeZipCallback?: (info: BeforeZipCallbackInfo) => Promise<void> | void) {
options = this.getOptions(options);
await this.prepublishToStaging(options);
let stagingFolderPath = this.getStagingFolderPath(options);
let manifestPath = path.join(stagingFolderPath, 'manifest');
let parsedManifest = await this.parseManifest(manifestPath);
if (options.incrementBuildNumber) {
let timestamp = dateformat(new Date(), 'yymmddHHMM');
parsedManifest.build_version = timestamp;
await this.fsExtra.writeFile(manifestPath, this.stringifyManifest(parsedManifest));
}
if (beforeZipCallback) {
let info: BeforeZipCallbackInfo = {
manifestData: parsedManifest,
stagingFolderPath: stagingFolderPath
};
await Promise.resolve(beforeZipCallback(info));
}
await this.zipPackage(options);
}
/**
* Given a root directory, normalize it to a full path.
* Fall back to cwd if not specified
* @param rootDir
*/
public normalizeRootDir(rootDir: string) {
if (!rootDir || (typeof rootDir === 'string' && rootDir.trim().length === 0)) {
return process.cwd();
} else {
return path.resolve(rootDir);
}
}
/**
* Get all file paths for the specified options
*/
public async getFilePaths(files: FilesType[], stagingPath: string, rootDir: string) {
stagingPath = path.normalize(stagingPath);
const normalizedFiles = this.normalizeFilesOption(files);
rootDir = this.normalizeRootDir(rootDir);
//run glob lookups for every glob string provided
let filePathObjects = await Promise.all(
normalizedFiles.map(async (file) => {
let pathRemoveFromDest: string;
//if we have a single string, and it's a directory, append the wildcard glob on it
if (file.src.length === 1) {
let fileAsDirPath: string;
//try the path as is
if (await this.isDirectory(file.src[0])) {
fileAsDirPath = file.src[0];
}
/* istanbul ignore next */
//assume path is relative, append root dir and try that way
if (!fileAsDirPath && await this.isDirectory(path.join(rootDir, file.src[0]))) {
fileAsDirPath = path.normalize(path.join(rootDir, file.src[0]));
}
if (fileAsDirPath) {
pathRemoveFromDest = fileAsDirPath;
//add the wildcard glob
file.src[0] = path.join(fileAsDirPath, '**', '*');
}
}
let originalSrc = file.src;
let filePathArray = await Q.nfcall(globAll, file.src, { cwd: rootDir });
let output = <{ src: string; dest: string; srcOriginal?: string; }[]>[];
for (let filePath of filePathArray) {
let dest = file.dest;
//if we created this globbed result, maintain the relative position of the files
if (pathRemoveFromDest) {
let normalizedFilePath = path.normalize(filePath);
//remove the specified source path
dest = normalizedFilePath.replace(pathRemoveFromDest, '');
//remove the filename if it's a file
if (await this.isDirectory(filePath) === false) {
dest = path.dirname(dest);
}
//prepend the specified dest
dest = path.join(file.dest, dest, path.basename(normalizedFilePath));
//blank out originalSrc since we already handled the dest
originalSrc = [];
}
//create a src;dest; object for every file or directory that was found
output.push({
src: filePath,
dest: dest,
srcOriginal: originalSrc.length === 1 ? originalSrc[0] : undefined
});
}
return output;
})
);
let fileObjects = <{ src: string; dest: string; srcOriginal?: string; }[]>[];
//create a single array of all paths
for (let filePathObject of filePathObjects) {
fileObjects = fileObjects.concat(filePathObject);
}
//make all file paths absolute
for (let fileObject of fileObjects) {
//only normalize non-absolute paths
if (path.isAbsolute(fileObject.src) === false) {
fileObject.src = path.resolve(
path.join(rootDir, fileObject.src)
);
}
//normalize the path
fileObject.src = path.normalize(fileObject.src);
}
let result: { src: string; dest: string; }[] = [];
//copy each file, retaining their folder structure relative to the rootDir
await Promise.all(
fileObjects.map(async (fileObject) => {
let src = path.normalize(fileObject.src);
let dest = fileObject.dest;
let sourceIsDirectory = await this.isDirectory(src);
let relativeSrc: string;
//if we have an original src, and it contains the ** glob, use the relative position starting at **
let globDoubleStarIndex = fileObject.srcOriginal ? fileObject.srcOriginal.indexOf('**') : -1;
if (fileObject.srcOriginal && globDoubleStarIndex > -1 && sourceIsDirectory === false) {
let pathToDoubleStar = fileObject.srcOriginal.substring(0, globDoubleStarIndex);
relativeSrc = src.replace(pathToDoubleStar, '');
dest = path.join(dest, relativeSrc);
} else {
let rootDirWithTrailingSlash = path.normalize(rootDir + path.sep);
relativeSrc = src.replace(rootDirWithTrailingSlash, '');
}
//if item is a directory
if (sourceIsDirectory) {
//source is a directory (which is only possible when glob resolves it as such)
//do nothing, because we don't want to copy empty directories to output
} else {
let destinationPath: string;
//if the relativeSrc is stil absolute, then this file exists outside of the rootDir. Copy to dest, and only retain filename from src
if (path.isAbsolute(relativeSrc)) {
destinationPath = path.join(stagingPath, dest, path.basename(relativeSrc));
} else {
//the relativeSrc is actually relative
//if the dest ends in a slash, use the filename from src, but the folder structure from dest
if (dest.endsWith(path.sep)) {
destinationPath = path.join(stagingPath, dest, path.basename(src));
//dest is empty, so use the relative path
} else if (dest === '') {
destinationPath = path.join(stagingPath, relativeSrc);
//use all of dest
} else {
destinationPath = path.join(stagingPath, dest);
}
}
fileObject.dest = destinationPath;
delete fileObject.srcOriginal;
//add the file object to the results
result.push(fileObject);
}
})
);
return result;
}
/**
* Copy all of the files to the staging directory
* @param fileGlobs
* @param stagingPath
*/
private async copyToStaging(files: FilesType[], stagingPath: string, rootDir: string) {
let fileObjects = await this.getFilePaths(files, stagingPath, rootDir);
for (let fileObject of fileObjects) {
//make sure the containing folder exists
await this.fsExtra.ensureDir(path.dirname(fileObject.dest));
//sometimes the copyfile action fails due to race conditions (normally to poorly constructed src;dest; objects with duplicate files in them
//Just try a few fimes until it resolves itself.
for (let i = 0; i < 10; i++) {
try {
//copy the src item (file or directory full of files)
await this.fsExtra.copy(fileObject.src, fileObject.dest, {
//copy the actual files that symlinks point to, not the symlinks themselves
dereference: true
});
//copy succeeded,
i = 10; //break out of the loop and still achieve coverage for i++
} catch (e) {
//wait a small amount of time and try again
/* istanbul ignore next */
await new Promise((resolve) => {
setTimeout(resolve, 50);
});
}
}
}
}
private generateBaseRequestOptions(requestPath: string, options: RokuDeployOptions): request.OptionsWithUrl {
options = this.getOptions(options);
let url = `http://${options.host}:${options.packagePort}/${requestPath}`;
let baseRequestOptions = {
url: url,
auth: {
user: options.username,
pass: options.password,
sendImmediately: false
}
};
return baseRequestOptions;
}
/**
* Determine if the given path is a directory
* @param path
*/
public async isDirectory(pathToDirectoryOrFile: string) {
try {
let stat = await Q.nfcall(this.fsExtra.lstat, pathToDirectoryOrFile);
return stat.isDirectory();
} catch (e) {
// lstatSync throws an error if path doesn't exist
return false;
}
}
/**
* Simulate pressing the home button on the remote for this roku.
* This makes the roku return to the home screen
* @param host - the host
* @param port - the port that should be used for the request. defaults to 8060
*/
public async pressHomeButton(host, port?: number) {
port = port ? port : this.getOptions().remotePort;
// press the home button to return to the main screen
return await this.doPostRequest({
url: `http://${host}:${port}/keypress/Home`
});
}
/**
* Publish a pre-existing packaged zip file to a remote Roku.
* @param options
*/
public async publish(options: RokuDeployOptions): Promise<{ message: string, results: any }> {
options = this.getOptions(options);
if (!options.host) {
throw new errors.MissingRequiredOptionError('must specify the host for the Roku device');
}
//make sure the outDir exists
await this.fsExtra.ensureDir(options.outDir);
let zipFilePath = this.getOutputZipFilePath(options);
if ((await this.fsExtra.pathExists(zipFilePath)) === false) {
throw new Error(`Cannot publish because file does not exist at '${zipFilePath}'`);
}
let readStream = this.fsExtra.createReadStream(zipFilePath);
//wait for the stream to open (no harm in doing this, and it helps solve an issue in the tests)
await new Promise((resolve) => {
readStream.on('open', resolve);
});
let requestOptions = this.generateBaseRequestOptions('plugin_install', options);
requestOptions.formData = {
mysubmit: 'Replace',
archive: readStream
};
if (options.remoteDebug) {
requestOptions.formData.remotedebug = '1';
}
let results = await this.doPostRequest(requestOptions);
if (options.failOnCompileError) {
if (results.body.indexOf('Install Failure: Compilation Failed.') > -1) {
throw new errors.CompileError('Compile error', results);
}
}
if (results.body.indexOf('Identical to previous version -- not replacing.') > -1) {
return { message: 'Identical to previous version -- not replacing', results: results };
}
return { message: 'Successful deploy', results: results };
}
/**
* Converts existing loaded package to squashfs for faster loading packages
* @param options
*/
public async convertToSquashfs(options: RokuDeployOptions) {
options = this.getOptions(options);
if (!options.host) {
throw new errors.MissingRequiredOptionError('must specify the host for the Roku device');
}
let requestOptions = this.generateBaseRequestOptions('plugin_install', options);
requestOptions.formData = {
archive: '',
mysubmit: 'Convert to squashfs'
};
let results = await this.doPostRequest(requestOptions);
if (results.body.indexOf('Conversion succeeded') === -1) {
throw new errors.ConvertError('Squashfs conversion failed');
}
}
/**
* resign Roku Device with supplied pkg and
* @param options
*/
public async rekeyDevice(options: RokuDeployOptions) {
options = this.getOptions(options);
if (!options.rekeySignedPackage) {
throw new errors.MissingRequiredOptionError('Must supply rekeySignedPackage');
}
if (!options.signingPassword) {
throw new errors.MissingRequiredOptionError('Must supply signingPassword');
}
let rekeySignedPackagePath = options.rekeySignedPackage;
if (!path.isAbsolute(options.rekeySignedPackage)) {
rekeySignedPackagePath = path.join(options.rootDir, options.rekeySignedPackage);
}
let requestOptions = this.generateBaseRequestOptions('plugin_inspect', options);
requestOptions.formData = {
mysubmit: 'Rekey',
passwd: options.signingPassword,
archive: this.fsExtra.createReadStream(rekeySignedPackagePath)
};
let results = await this.doPostRequest(requestOptions);
let resultTextSearch = /<font color="red">([^<]+)<\/font>/.exec(results.body);
if (!resultTextSearch) {
throw new errors.UnparsableDeviceResponseError('Unknown Rekey Failure');
}
if (resultTextSearch[1] !== 'Success.') {
throw new errors.FailedDeviceResponseError('Rekey Failure: ' + resultTextSearch[1]);
}
if (options.devId) {
let devId = await this.getDevId(options);
if (devId !== options.devId) {
throw new errors.UnknownDeviceResponseError('Rekey was successful but resulting Dev ID "' + devId + '" did not match expected value of "' + options.devId + '"');
}
}
}
/**
* Sign a pre-existing package using Roku and return path to retrieve it
* @param options
*/
public async signExistingPackage(options: RokuDeployOptions): Promise<string> {
options = this.getOptions(options);
if (!options.signingPassword) {
throw new errors.MissingRequiredOptionError('Must supply signingPassword');
}
let stagingFolderpath = this.getStagingFolderPath(options);
let manifestPath = path.join(stagingFolderpath, 'manifest');
let parsedManifest = await this.parseManifest(manifestPath);
let appName = parsedManifest.title + '/' + parsedManifest.major_version + '.' + parsedManifest.minor_version;
let requestOptions = this.generateBaseRequestOptions('plugin_package', options);
requestOptions.formData = {
mysubmit: 'Package',
pkg_time: (new Date()).getTime(),
passwd: options.signingPassword,
app_name: appName,
};
let results = await this.doPostRequest(requestOptions);
let failedSearchMatches = /<font.*>Failed: (.*)/.exec(results.body);
if (failedSearchMatches) {
throw new errors.FailedDeviceResponseError(failedSearchMatches[1], results);
}
let pkgSearchMatches = /<a href="(pkgs\/[^\.]+\.pkg)">/.exec(results.body);
if (pkgSearchMatches) {
return pkgSearchMatches[1];
}
throw new errors.UnknownDeviceResponseError('Unknown error signing package', results);
}
/**
* Sign a pre-existing package using Roku and return path to retrieve it
* @param pkgPath
* @param options
*/
public async retrieveSignedPackage(pkgPath: string, options: RokuDeployOptions): Promise<string> {
options = this.getOptions(options);
let requestOptions = this.generateBaseRequestOptions(pkgPath, options);
let pkgFilePath = this.getOutputPkgFilePath(options);
await this.fsExtra.ensureDir(path.dirname(pkgFilePath));
return new Promise<string>((resolve, reject) => {
this.request.get(requestOptions)
.on('error', (err) => reject(err))
.on('response', (response) => {
if (response.statusCode !== 200) {
reject(new Error('Invalid response code: ' + response.statusCode));
}
resolve(pkgFilePath);
})
.pipe(this.fsExtra.createWriteStream(pkgFilePath));
});
}
/**
* Centralized function for handling POST http requests
* @param params
*/
private async doPostRequest(params: any) {
let results: { response: any; body: any } = await new Promise((resolve, reject) => {
this.request.post(params, (err, resp, body) => {
if (err) {
return reject(err);
}
return resolve({ response: resp, body: body });
});
});
this.checkRequest(results);
return results;
}
/**
* Centralized function for handling GET http requests
* @param params
*/
private async doGetRequest(params: any) {
let results: { response: any; body: any } = await new Promise((resolve, reject) => {
this.request.get(params, (err, resp, body) => {
if (err) {
return reject(err);
}
return resolve({ response: resp, body: body });
});
});
this.checkRequest(results);
return results;
}
private checkRequest(results) {
if (!results || !results.response || typeof results.body !== 'string') {
throw new errors.UnparsableDeviceResponseError('Invalid response', results);
}
if (results.response.statusCode === 401) {
throw new errors.UnauthorizedDeviceResponseError('Unauthorized. Please verify username and password for target Roku.', results);
}
if (results.response.statusCode !== 200) {
throw new errors.InvalidDeviceResponseCodeError('Invalid response code: ' + results.response.statusCode);
}
}
/**
* Create a zip of the project, and then publish to the target Roku device
* @param options
*/
public async deploy(options?: RokuDeployOptions, beforeZipCallback?: (info: BeforeZipCallbackInfo) => void) {
options = this.getOptions(options);
await this.createPackage(options, beforeZipCallback);
await this.deleteInstalledChannel(options);
let result = await this.publish(options);
return result;
}
/**
* Deletes any installed dev channel on the target Roku device
* @param options
*/
public async deleteInstalledChannel(options?: RokuDeployOptions) {
options = this.getOptions(options);
let deleteOptions = this.generateBaseRequestOptions('plugin_install', options);
deleteOptions.formData = {
mysubmit: 'Delete',
archive: ''
};
return (await this.doPostRequest(deleteOptions));
}
/**
* executes sames steps as deploy and signs the package and stores it in the out folder
* @param options
*/
public async deployAndSignPackage(options?: RokuDeployOptions, beforeZipCallback?: (info: BeforeZipCallbackInfo) => void): Promise<string> {
let originalOptionValueRetainStagingFolder = options.retainStagingFolder;
options = this.getOptions(options);
options.retainStagingFolder = true;
await this.deploy(options, beforeZipCallback);
if (options.convertToSquashfs) {
await this.convertToSquashfs(options);
}
let remotePkgPath = await this.signExistingPackage(options);
let localPkgFilePath = await this.retrieveSignedPackage(remotePkgPath, options);
if (originalOptionValueRetainStagingFolder !== true) {
await this.fsExtra.remove(this.getStagingFolderPath(options));
}
return localPkgFilePath;
}
/**
* Get an options with all overridden vaues, and then defaults for missing values
* @param options
*/
public getOptions(options: RokuDeployOptions = {}) {
let fileOptions: RokuDeployOptions = {};
//load a rokudeploy.json file if it exists
if (this.fsExtra.existsSync('rokudeploy.json')) {
let configFileText = this.fsExtra.readFileSync('rokudeploy.json').toString();
fileOptions = JSON.parse(configFileText);
}
let defaultOptions = <RokuDeployOptions>{
outDir: './out',
outFile: 'roku-deploy',
retainStagingFolder: false,
incrementBuildNumber: false,
failOnCompileError: true,
packagePort: 80,
remotePort: 8060,
rootDir: './',
files: [
'source/**/*.*',
'components/**/*.*',
'images/**/*.*',
'manifest'
],
username: 'rokudev'
};
//override the defaults with any found or provided options
let finalOptions = Object.assign({}, defaultOptions, fileOptions, options);
//fully resolve the folder paths
finalOptions.rootDir = path.resolve(finalOptions.rootDir);
finalOptions.outDir = path.resolve(finalOptions.outDir);
return finalOptions;
}
public getStagingFolderPath(options?: RokuDeployOptions) {
options = this.getOptions(options);
if (options.stagingFolderPath) {
return path.resolve(options.stagingFolderPath);
} else {
let stagingFolderPath = path.join(options.outDir, '.roku-deploy-staging');
stagingFolderPath = path.resolve(stagingFolderPath);
return stagingFolderPath;
}
}
/**
* Centralizes getting output zip file path based on passed in options
* @param options
*/
public getOutputZipFilePath(options: RokuDeployOptions) {
options = this.getOptions(options);
let zipFileName = <string>options.outFile;
if (zipFileName.indexOf('.zip') < 0) {
zipFileName += '.zip';
}
let outFolderPath = path.resolve(options.outDir);
let outZipFilePath = path.join(outFolderPath, zipFileName);
return outZipFilePath;
}
/**
* Centralizes getting output pkg file path based on passed in options
* @param options
*/
public getOutputPkgFilePath(options?: RokuDeployOptions) {
options = this.getOptions(options);
let pkgFileName = <string>options.outFile;
if (pkgFileName.indexOf('.zip') < 0) {
pkgFileName += '.pkg';
} else {
pkgFileName = pkgFileName.replace('.zip', '.pkg');
}
let outFolderPath = path.resolve(options.outDir);
let outPkgFilePath = path.join(outFolderPath, pkgFileName);
return outPkgFilePath;
}
public async getDevId(options?: RokuDeployOptions) {
options = this.getOptions(options);
let requestOptions = this.generateBaseRequestOptions('plugin_package', options);
let results = await this.doGetRequest(requestOptions);
let devIdSearchMatches = /Your Dev ID:[^>]+>([^<]+)</.exec(results.body);
if (devIdSearchMatches) {
return devIdSearchMatches[1].trim();
}
throw new errors.UnparsableDeviceResponseError('Could not retrieve Dev ID', results);
}
public async parseManifest(manifestPath: string): Promise<ManifestData> {
if (!await this.fsExtra.pathExists(manifestPath)) {
throw new Error(manifestPath + ' does not exist');
}
let manifestContents = await this.fsExtra.readFile(manifestPath, 'utf-8');
return this.parseManifestFromString(manifestContents);
}
public parseManifestFromString(manifestContents: string): ManifestData {
let manifestLines = manifestContents.split('\n');
let manifestData: ManifestData = {};
manifestData.keyIndexes = {};
manifestData.lineCount = manifestLines.length;
manifestLines.map((line, index) => {
let match = /(\w+)=(.+)/.exec(line);
if (match) {
let key = match[1];
manifestData[key] = match[2];
manifestData.keyIndexes[key] = index;
}
});
return manifestData;
}
public stringifyManifest(manifestData: ManifestData): string {
let output = [];
if (manifestData.keyIndexes && manifestData.lineCount) {
output.fill('', 0, manifestData.lineCount);
let key;
for (key in manifestData) {
if (key === 'lineCount' || key === 'keyIndexes') {
continue;
}
let index = manifestData.keyIndexes[key];
output[index] = `${key}=${manifestData[key]}`;
}
} else {
output = Object.keys(manifestData).map((key) => {
return `${key}=${manifestData[key]}`;
});
}
return output.join('\n');
}
/**
* Given a path to a folder, zip up that folder and all of its contents
* @param srcFolder
* @param zipFilePath
*/
public zipFolder(srcFolder: string, zipFilePath: string) {
return new Promise((resolve, reject) => {
let output = this.fsExtra.createWriteStream(zipFilePath);
let archive = archiver('zip');
output.on('close', () => {
resolve();
});
output.on('error', (err) => {
reject(err);
});
/* istanbul ignore next */
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.warn(err);
} else {
reject(err);
}
});
/* istanbul ignore next */
archive.on('error', (err) => {
reject(err);
});
archive.pipe(output);
//add every file in the source folder
archive.directory(srcFolder, false);
//finalize the archive
archive.finalize();
});
}
}
export interface RokuDeployOptions {
/**
* A full path to the folder where the zip/pkg package should be placed
* @default './out'
*/
outDir?: string;
/**
* The base filename the zip/pkg file should be given (excluding the extension)
* @default 'roku-deploy'
*/
outFile?: string;
/**
* The root path to the folder holding your Roku project's source files (manifest, components/, source/ should be directly under this folder)
* @default './'
*/
rootDir?: string;
// tslint:disable:jsdoc-format
/**
* An array of source file paths, source file globs, or {src,dest} objects indicating
* where the source files are and where they should be placed
* in the output directory
* @default [
'source/**\/*.*',
'components/**\/*.*',
'images/**\/*.*',
'manifest'
],
*/
// tslint:enable:jsdoc-format
files?: FilesType[];
/**
* Set this to true to prevent the staging folder from being deleted after creating the package
* @default false
*/
retainStagingFolder?: boolean;
/**
* The path where roku-deploy should stage all of the files right before being zipped. defaults to ${outDir}/.roku-deploy-staging
*/
stagingFolderPath?: string;
/**
* The IP address or hostname of the target Roku device.
* @required
* @example '192.168.1.21'
*
*/
host?: string;
/**
* The port that should be used when installing the package. Defaults to 80.
* This is mainly useful for things like emulators that use alternate ports,
* or when publishing through some type of port forwarding configuration.
*/
packagePort?: number;
/**
* When publishing a side loaded channel this flag can be used to enable the socket based BrightScript debug protocol. Defaults to false.
* More information on the BrightScript debug protocol can be found here: https://developer.roku.com/en-ca/docs/developer-program/debugging/socket-based-debugger.md
*/
remoteDebug?: boolean;
/**
* The port used to send remote control commands (like home press, back, etc.). Defaults to 8060.
* This is mainly useful for things like emulators that use alternate ports,
* or when sending commands through some type of port forwarding.
*/
remotePort?: number;
/**
* The username for the roku box. This will always be 'rokudev', but allows to be overridden
* just in case roku adds support for custom usernames in the future
* @default 'rokudev'
*/
username?: string;
/**
* The password for logging in to the developer portal on the target Roku device
* @required
*/
password?: string;
/**
* The password used for creating signed packages
* @required
*/
signingPassword?: string;
/**
* Path to a copy of the signed package you want to use for rekeying
* @required
*/
rekeySignedPackage?: string;
/**
* Dev ID we are expecting the device to have. If supplied we check that the dev ID returned after keying matches what we expected
*/
devId?: string;
/**