-
Notifications
You must be signed in to change notification settings - Fork 24
Allow setting options when creating the Roku-Deploy object #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Christian-Holbrook
wants to merge
4
commits into
v4
Choose a base branch
from
constructor-options
base: v4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7fb903c
Allow setting options when creating the Roku-Deploy object
Christian-Holbrook 006f24e
Use logger instead of log. Make a constructor options interface
Christian-Holbrook 2e479a5
Remove logLevel
Christian-Holbrook 450f75d
Merge branch 'v4' into constructor-options
Christian-Holbrook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,8 @@ import * as errors from './Errors'; | |
| import * as xml2js from 'xml2js'; | ||
| import { parse as parseJsonc } from 'jsonc-parser'; | ||
| import { util } from './util'; | ||
| import type { FileEntry } from './RokuDeployOptions'; | ||
| import { logger, type LogLevel } from '@rokucommunity/logger'; | ||
| import type { FileEntry, RokuDeployOptions } from './RokuDeployOptions'; | ||
| import { logger } from '@rokucommunity/logger'; | ||
| import * as dayjs from 'dayjs'; | ||
| import * as lodash from 'lodash'; | ||
| import type { DeviceInfo, DeviceInfoRaw } from './DeviceInfo'; | ||
|
|
@@ -29,12 +29,57 @@ export class RokuDeploy { | |
| outFile: 'roku-deploy.zip' | ||
| }; | ||
|
|
||
| /** | ||
| * Instance-level default options that are merged into every method call | ||
| */ | ||
| private readonly options: RokuDeployOptions; | ||
|
|
||
| /** | ||
| * Custom logger instance, if provided | ||
| */ | ||
| private readonly _logger: typeof logger; | ||
|
|
||
| /** | ||
| * Create a new RokuDeploy instance with optional default options. | ||
| * These defaults will be merged into every method call, so you don't need to | ||
| * repeat common options like `host` and `password` on every call. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const rd = new RokuDeploy({ host: '192.168.1.100', password: 'secret' }); | ||
| * await rd.sideload({ rootDir: './dist' }); // host and password come from defaults | ||
| * ``` | ||
| */ | ||
| constructor(options?: RokuDeployOptions) { | ||
|
Christian-Holbrook marked this conversation as resolved.
Outdated
|
||
| this.options = options ?? {}; | ||
|
|
||
| // Use custom logger if provided, otherwise use global logger | ||
| if (this.options.logger) { | ||
| this._logger = this.options.logger; | ||
| } else { | ||
| this._logger = logger; | ||
| } | ||
|
|
||
| // Set log level if provided | ||
| if (this.options.logLevel) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We decided to drop logLevel as an option. We will unify with other tools that just take logger. See example from
Christian-Holbrook marked this conversation as resolved.
Outdated
|
||
| this._logger.logLevel = this.options.logLevel; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the logger instance for this RokuDeploy instance | ||
| */ | ||
| private get log() { | ||
|
Christian-Holbrook marked this conversation as resolved.
Outdated
|
||
| return this._logger; | ||
| } | ||
|
|
||
| /** | ||
| * Copies all of the referenced files to the staging folder | ||
| * @param options | ||
| */ | ||
| public async stage(options: StageOptions) { | ||
| logger.info('Beginning to copy files to staging folder'); | ||
| options = { ...this.options, ...options }; | ||
| this.log.info('Beginning to copy files to staging folder'); | ||
| const cwd = options.cwd ?? process.cwd(); | ||
|
|
||
| // Set defaults and resolve paths | ||
|
|
@@ -73,7 +118,7 @@ export class RokuDeploy { | |
| }); | ||
| }, 10); | ||
| })); | ||
| logger.info('Relevant files copied to:', out); | ||
| this.log.info('Relevant files copied to:', out); | ||
| return out; | ||
| } | ||
|
|
||
|
|
@@ -82,7 +127,8 @@ export class RokuDeploy { | |
| * @param options | ||
| */ | ||
| public async zip(options: ZipOptions) { | ||
| logger.info('Beginning to zip folder'); | ||
| options = { ...this.options, ...options }; | ||
| this.log.info('Beginning to zip folder'); | ||
| const cwd = options.cwd ?? process.cwd(); | ||
|
|
||
| // Resolve source directory - dir is required | ||
|
|
@@ -108,7 +154,7 @@ export class RokuDeploy { | |
|
|
||
| //create a zip of the folder | ||
| await this.makeZip(dir, out); | ||
| logger.info('Zip created at:', out); | ||
| this.log.info('Zip created at:', out); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -152,6 +198,7 @@ export class RokuDeploy { | |
| * Get all file paths for the specified options | ||
| */ | ||
| public async getFilePaths(options: GetFilePathsOptions): Promise<StandardizedFileEntry[]> { | ||
| options = { ...this.options, ...options } as GetFilePathsOptions; | ||
| let rootDir = options.rootDir; | ||
| const files = options.files; | ||
|
|
||
|
|
@@ -214,6 +261,7 @@ export class RokuDeploy { | |
| } | ||
|
|
||
| public async keyPress(options: KeyPressOptions) { | ||
| options = { ...this.options, ...options } as KeyPressOptions; | ||
| return this.sendKeyEvent({ | ||
| ...options, | ||
| key: options.key, | ||
|
|
@@ -222,20 +270,23 @@ export class RokuDeploy { | |
| } | ||
|
|
||
| public async keyUp(options: KeyUpOptions) { | ||
| options = { ...this.options, ...options } as KeyUpOptions; | ||
| return this.sendKeyEvent({ | ||
| ...options, | ||
| action: 'keyup' | ||
| }); | ||
| } | ||
|
|
||
| public async keyDown(options: KeyDownOptions) { | ||
| options = { ...this.options, ...options } as KeyDownOptions; | ||
| return this.sendKeyEvent({ | ||
| ...options, | ||
| action: 'keydown' | ||
| }); | ||
| } | ||
|
|
||
| public async sendText(options: SendTextOptions) { | ||
| options = { ...this.options, ...options } as SendTextOptions; | ||
| this.checkRequiredOptions(options, ['host', 'text']); | ||
| const chars = options.text.split(''); | ||
| for (const char of chars) { | ||
|
|
@@ -252,7 +303,8 @@ export class RokuDeploy { | |
| * This makes the roku return to the home screen | ||
| */ | ||
| private async sendKeyEvent(options: SendKeyEventOptions) { | ||
| logger.info('Sending key event:', options.key); | ||
| options = { ...this.options, ...options } as SendKeyEventOptions; | ||
| this.log.info('Sending key event:', options.key); | ||
| this.checkRequiredOptions(options, ['host', 'key']); | ||
| // Set defaults | ||
| const ecpPort = options.ecpPort ?? RokuDeploy.defaults.ecpPort; | ||
|
|
@@ -265,6 +317,7 @@ export class RokuDeploy { | |
| } | ||
|
|
||
| public async closeChannel(options: CloseChannelOptions) { | ||
| options = { ...this.options, ...options } as CloseChannelOptions; | ||
| // TODO: After 13.0 releases, add check for ECP close-app support, and use that twice to kill instant resume if available | ||
| await this.sendKeyEvent({ | ||
| ...options, | ||
|
|
@@ -279,7 +332,8 @@ export class RokuDeploy { | |
| * @param options | ||
| */ | ||
| public async sideload(options: SideloadOptions): Promise<{ message: string; results: any }> { | ||
| logger.info('Beginning to sideload package'); | ||
| options = { ...this.options, ...options } as SideloadOptions; | ||
| this.log.info('Beginning to sideload package'); | ||
| this.checkRequiredOptions(options, ['host', 'password']); | ||
|
|
||
| const cwd = options.cwd ?? process.cwd(); | ||
|
|
@@ -397,7 +451,7 @@ export class RokuDeploy { | |
| if (response.body.indexOf('Identical to previous version -- not replacing.') > -1) { | ||
| return { message: 'Identical to previous version -- not replacing', results: response }; | ||
| } | ||
| logger.info('Successful sideload'); | ||
| this.log.info('Successful sideload'); | ||
| return { message: 'Successful sideload', results: response }; | ||
| } finally { | ||
| //delete the zip file if we generated it from rootDir | ||
|
|
@@ -408,7 +462,7 @@ export class RokuDeploy { | |
| try { | ||
| readStream?.close(); | ||
| } catch (e) { | ||
| logger.warn('Error closing read stream', e); | ||
| this.log.warn('Error closing read stream', e); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -439,6 +493,7 @@ export class RokuDeploy { | |
| * @param options | ||
| */ | ||
| public async convertToSquashfs(options: ConvertToSquashfsOptions) { | ||
| options = { ...this.options, ...options } as ConvertToSquashfsOptions; | ||
| this.checkRequiredOptions(options, ['host', 'password']); | ||
| let requestOptions = this.generateBaseRequestOptions('plugin_install', options, { | ||
| archive: '', | ||
|
|
@@ -459,7 +514,7 @@ export class RokuDeploy { | |
| return results; | ||
| } | ||
| } catch (e) { | ||
| logger.warn('Error converting to squashfs:', error); | ||
| this.log.warn('Error converting to squashfs:', error); | ||
| throw error; | ||
| } | ||
| } else { | ||
|
|
@@ -476,6 +531,7 @@ export class RokuDeploy { | |
| * @param options | ||
| */ | ||
| public async rekeyDevice(options: RekeyDeviceOptions) { | ||
| options = { ...this.options, ...options } as RekeyDeviceOptions; | ||
| this.checkRequiredOptions(options, ['host', 'password', 'pkg', 'signingPassword']); | ||
| const cwd = path.resolve(process.cwd(), options.cwd ?? '.'); | ||
|
|
||
|
|
@@ -523,7 +579,8 @@ export class RokuDeploy { | |
| * @param options | ||
| */ | ||
| public async createSignedPackage(options: CreateSignedPackageOptions): Promise<string> { | ||
| logger.info('Creating signed package'); | ||
| options = { ...this.options, ...options } as CreateSignedPackageOptions; | ||
| this.log.info('Creating signed package'); | ||
| this.checkRequiredOptions(options, ['host', 'password', 'signingPassword']); | ||
| const cwd = options.cwd ?? process.cwd(); | ||
|
|
||
|
|
@@ -593,7 +650,7 @@ export class RokuDeploy { | |
| const url = pkgSearchMatches[1]; | ||
| let requestOptions2 = this.generateBaseRequestOptions(url, options); | ||
| await this.downloadFile(requestOptions2, out); | ||
| logger.info('Signed package created at:', out); | ||
| this.log.info('Signed package created at:', out); | ||
| return out; | ||
| } | ||
|
|
||
|
|
@@ -640,7 +697,7 @@ export class RokuDeploy { | |
| * @param params | ||
| */ | ||
| private async doPostRequest(params: requestType.OptionsWithUrl, verify = true) { | ||
| logger.info('handling POST request to', params.url); | ||
| this.log.info('handling POST request to', params.url); | ||
| let results: { response: any; body: any } = await new Promise((resolve, reject) => { | ||
|
|
||
| this.setUserAgentIfMissing(params); | ||
|
|
@@ -663,7 +720,7 @@ export class RokuDeploy { | |
| * @param params | ||
| */ | ||
| private async doGetRequest(params: requestType.OptionsWithUrl) { | ||
| logger.info('handling GET request to', params.url); | ||
| this.log.info('handling GET request to', params.url); | ||
| let results: { response: any; body: any } = await new Promise((resolve, reject) => { | ||
|
|
||
| this.setUserAgentIfMissing(params); | ||
|
|
@@ -814,7 +871,8 @@ export class RokuDeploy { | |
| * @param options | ||
| */ | ||
| public async deleteDevChannel(options?: DeleteDevChannelOptions) { | ||
| logger.info('Deleting dev channel...'); | ||
| options = { ...this.options, ...options } as DeleteDevChannelOptions; | ||
| this.log.info('Deleting dev channel...'); | ||
| this.checkRequiredOptions(options, ['host', 'password']); | ||
|
|
||
| let deleteOptions = this.generateBaseRequestOptions('plugin_install', options); | ||
|
|
@@ -829,6 +887,7 @@ export class RokuDeploy { | |
| * Delete the component library with the specified filename from the device | ||
| */ | ||
| public async deleteComponentLibrary(options?: { host: string; password: string; fileName: string; username?: string }) { | ||
| options = { ...this.options, ...options } as { host: string; password: string; fileName: string; username?: string }; | ||
| this.checkRequiredOptions(options, ['host', 'password', 'fileName']); | ||
|
|
||
| let deleteOptions = this.generateBaseRequestOptions('plugin_install', options); | ||
|
|
@@ -847,6 +906,7 @@ export class RokuDeploy { | |
| * Delete all component libraries from the device | ||
| */ | ||
| public async deleteAllComponentLibraries(options: { host: string; password: string; username?: string }) { | ||
| options = { ...this.options, ...options } as { host: string; password: string; username?: string }; | ||
| const packages = await this.getInstalledPackages(options); | ||
| for (const pkg of packages) { | ||
| if (pkg.appType === 'dcl') { | ||
|
|
@@ -862,6 +922,7 @@ export class RokuDeploy { | |
| * Fetch the full list of installed packages from the device. Useful for finding the file names of installed component libraries or the dev channel. | ||
| */ | ||
| private async getInstalledPackages(options: { host: string; password: string; username?: string }): Promise<RokuPackage[]> { | ||
| options = { ...this.options, ...options } as { host: string; password: string; username?: string }; | ||
| this.checkRequiredOptions(options, ['host', 'password']); | ||
| let deleteOptions = this.generateBaseRequestOptions('plugin_install', options); | ||
| deleteOptions.qs ??= {}; | ||
|
|
@@ -876,6 +937,7 @@ export class RokuDeploy { | |
| * Gets a screenshot from the device. A side-loaded channel must be running or an error will be thrown. | ||
| */ | ||
| public async captureScreenshot(options: CaptureScreenshotOptions) { | ||
| options = { ...this.options, ...options } as CaptureScreenshotOptions; | ||
| this.checkRequiredOptions(options, ['host', 'password']); | ||
| const cwd = options.cwd ?? process.cwd(); | ||
| const screenshotDir = options.screenshotDir | ||
|
|
@@ -985,6 +1047,7 @@ export class RokuDeploy { | |
| public async getDeviceInfo(options?: GetDeviceInfoOptions & { enhance: true }): Promise<DeviceInfo>; | ||
| public async getDeviceInfo(options?: GetDeviceInfoOptions): Promise<DeviceInfoRaw>; | ||
| public async getDeviceInfo(options: GetDeviceInfoOptions) { | ||
| options = { ...this.options, ...options } as GetDeviceInfoOptions; | ||
| this.checkRequiredOptions(options, ['host']); | ||
|
|
||
| // Set defaults | ||
|
|
@@ -1030,10 +1093,10 @@ export class RokuDeploy { | |
| } | ||
| deviceInfo = result; | ||
| } | ||
| logger.debug('Device info:', deviceInfo); | ||
| this.log.debug('Device info:', deviceInfo); | ||
| return deviceInfo; | ||
| } catch (e) { | ||
| logger.warn('Error getting device info:', e); | ||
| this.log.warn('Error getting device info:', e); | ||
| throw new errors.UnparsableDeviceResponseError('Could not retrieve device info', response); | ||
| } | ||
| } | ||
|
|
@@ -1050,6 +1113,7 @@ export class RokuDeploy { | |
| * - 'permissive': Full access for internal networks | ||
| */ | ||
| public async getEcpNetworkAccessMode(options: GetDeviceInfoOptions): Promise<EcpNetworkAccessMode> { | ||
| options = { ...this.options, ...options } as GetDeviceInfoOptions; | ||
| try { | ||
| const deviceInfo = await this.getDeviceInfo(options); | ||
| return deviceInfo['ecp-setting-mode']; | ||
|
|
@@ -1087,9 +1151,10 @@ export class RokuDeploy { | |
| * @returns | ||
| */ | ||
| public async getDevId(options?: GetDevIdOptions) { | ||
| options = { ...this.options, ...options } as GetDevIdOptions; | ||
| this.checkRequiredOptions(options, ['host']); | ||
| const deviceInfo = await this.getDeviceInfo(options); | ||
| logger.debug('Found dev id:', deviceInfo['keyed-developer-id']); | ||
| this.log.debug('Found dev id:', deviceInfo['keyed-developer-id']); | ||
| return deviceInfo['keyed-developer-id']; | ||
| } | ||
|
|
||
|
|
@@ -1120,6 +1185,7 @@ export class RokuDeploy { | |
| } | ||
|
|
||
| public async rebootDevice(options: RebootDeviceOptions) { | ||
| options = { ...this.options, ...options } as RebootDeviceOptions; | ||
| this.checkRequiredOptions(options, ['host', 'password']); | ||
|
|
||
| // Get device info to check software version | ||
|
|
@@ -1141,6 +1207,7 @@ export class RokuDeploy { | |
| } | ||
|
|
||
| public async checkForUpdate(options: CheckForUpdateOptions) { | ||
| options = { ...this.options, ...options } as CheckForUpdateOptions; | ||
| this.checkRequiredOptions(options, ['host', 'password']); | ||
|
|
||
| // Get device info to check software version | ||
|
|
@@ -1335,7 +1402,6 @@ export interface BaseRequestOptions { | |
| password: string; | ||
| packagePort?: number; | ||
| timeout?: number; | ||
| logLevel?: LogLevel; | ||
| } | ||
|
|
||
| export interface BaseEcpOptions { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.