Skip to content

Commit

Permalink
Support overriding various package upload form data (#136)
Browse files Browse the repository at this point in the history
* Support overriding various package upload form data

* adds support for squashfs deployments

---------

Co-authored-by: George Cook <[email protected]>
  • Loading branch information
TwitchBronBron and georgejecook authored Mar 1, 2024
1 parent a5e3600 commit 519541f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
30 changes: 20 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/RokuDeploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,34 @@ describe('index', () => {
} catch (e) { }
});

it('uses overridden route', async () => {
const stub = mockDoPostRequest();
await rokuDeploy.publish({
...options,
packageUploadOverrides: {
route: 'alt_path'
}
});
expect(stub.getCall(0).args[0].url).to.eql('http://0.0.0.0:80/alt_path');
});

it('overrides formData', async () => {
const stub = mockDoPostRequest();
await rokuDeploy.publish({
...options,
remoteDebug: true,
packageUploadOverrides: {
formData: {
remotedebug: null,
newfield: 'here'
}
}
});
expect(stub.getCall(0).args[0].formData).to.include({
newfield: 'here'
}).and.to.not.haveOwnProperty('remotedebug');
});

it('does not delete the archive by default', async () => {
let zipPath = `${options.outDir}/${options.outFile}`;

Expand Down Expand Up @@ -2230,6 +2258,9 @@ describe('index', () => {
it('rejects the promise when an error occurs', async () => {
//zip path doesn't exist
await assertThrowsAsync(async () => {
sinon.stub(fsExtra, 'outputFile').callsFake(() => {
throw new Error();
});
await rokuDeploy.zipFolder('source', '.tmp/some/zip/path/that/does/not/exist');
});
});
Expand Down
20 changes: 16 additions & 4 deletions src/RokuDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class RokuDeploy {
if (options.incrementBuildNumber) {
let timestamp = dateformat(new Date(), 'yymmddHHMM');
parsedManifest.build_version = timestamp; //eslint-disable-line camelcase
await this.fsExtra.writeFile(manifestPath, this.stringifyManifest(parsedManifest));
await this.fsExtra.outputFile(manifestPath, this.stringifyManifest(parsedManifest));
}

if (beforeZipCallback) {
Expand Down Expand Up @@ -433,7 +433,9 @@ export class RokuDeploy {
readStream.on('open', resolve);
});

let requestOptions = this.generateBaseRequestOptions('plugin_install', options, {
const route = options.packageUploadOverrides?.route ?? 'plugin_install';

let requestOptions = this.generateBaseRequestOptions(route, options, {
mysubmit: 'Replace',
archive: readStream
});
Expand All @@ -449,6 +451,16 @@ export class RokuDeploy {
requestOptions.formData.remotedebug_connect_early = '1';
}

//apply any supplied formData overrides
for (const key in options.packageUploadOverrides?.formData ?? {}) {
const value = options.packageUploadOverrides.formData[key];
if (value === undefined || value === null) {
delete requestOptions.formData[key];
} else {
requestOptions.formData[key] = value;
}
}

//try to "replace" the channel first since that usually works.
let response: HttpResponse;
try {
Expand Down Expand Up @@ -985,7 +997,7 @@ export class RokuDeploy {
options = this.getOptions(options);

let zipFileName = options.outFile;
if (!zipFileName.toLowerCase().endsWith('.zip')) {
if (!zipFileName.toLowerCase().endsWith('.zip') && !zipFileName.toLowerCase().endsWith('.squashfs')) {
zipFileName += '.zip';
}
let outFolderPath = path.resolve(options.outDir);
Expand Down Expand Up @@ -1170,7 +1182,7 @@ export class RokuDeploy {
await Promise.all(promises);
// level 2 compression seems to be the best balance between speed and file size. Speed matters more since most will be calling squashfs afterwards.
const content = await zip.generateAsync({ type: 'nodebuffer', compressionOptions: { level: 2 } });
return this.fsExtra.writeFile(zipFilePath, content);
return this.fsExtra.outputFile(zipFilePath, content);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/RokuDeployOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ export interface RokuDeployOptions {
* If true, the previously installed dev channel will be deleted before installing the new one
*/
deleteInstalledChannel?: boolean;

/**
* Overrides for values used during the zip upload process. You probably don't need to change these...
*/
packageUploadOverrides?: {
/**
* The route to use for uploading to the Roku device. Defaults to 'plugin_install'
* @default 'plugin_install'
*/
route?: string;

/**
* A dictionary of form fields to be included in the package upload request. Set a value to null to delete from the form
*/
formData?: Record<string, any>;
};
}

export type FileEntry = (string | { src: string | string[]; dest?: string });

0 comments on commit 519541f

Please sign in to comment.