Refactor screenshot capturing
Summary: Adding a utility for capturing screenshots to the configured location without having to worry about where to store it or use buffers etc. Reviewed By: mweststrate Differential Revision: D19765926 fbshipit-source-id: d6b51c4ffafab7450e97a60468926d84a25a8c0f
This commit is contained in:
committed by
Facebook Github Bot
parent
f0a482af2f
commit
2acf81027e
@@ -18,10 +18,7 @@ import config from '../utils/processConfig';
|
|||||||
import BaseDevice from '../devices/BaseDevice';
|
import BaseDevice from '../devices/BaseDevice';
|
||||||
import {State as Store} from '../reducers';
|
import {State as Store} from '../reducers';
|
||||||
import open from 'open';
|
import open from 'open';
|
||||||
|
import {capture, CAPTURE_LOCATION, getFileName} from '../utils/screenshot';
|
||||||
const CAPTURE_LOCATION = expandTilde(
|
|
||||||
config().screenCapturePath || remote.app.getPath('desktop'),
|
|
||||||
);
|
|
||||||
|
|
||||||
type OwnProps = {};
|
type OwnProps = {};
|
||||||
|
|
||||||
@@ -49,11 +46,6 @@ export async function openFile(path: string | null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileName(extension: 'png' | 'mp4'): string {
|
|
||||||
// Windows does not like `:` in its filenames. Yes, I know ...
|
|
||||||
return `screencap-${new Date().toISOString().replace(/:/g, '')}.${extension}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
type Props = OwnProps & StateFromProps & DispatchFromProps;
|
type Props = OwnProps & StateFromProps & DispatchFromProps;
|
||||||
class ScreenCaptureButtons extends Component<Props, State> {
|
class ScreenCaptureButtons extends Component<Props, State> {
|
||||||
videoPath: string | null | undefined;
|
videoPath: string | null | undefined;
|
||||||
@@ -82,17 +74,10 @@ class ScreenCaptureButtons extends Component<Props, State> {
|
|||||||
this.setState({recordingEnabled});
|
this.setState({recordingEnabled});
|
||||||
};
|
};
|
||||||
|
|
||||||
captureScreenshot: Promise<void> | any = () => {
|
captureScreenshot: Promise<void> | any = async () => {
|
||||||
const {selectedDevice} = this.props;
|
const {selectedDevice} = this.props;
|
||||||
const pngPath = path.join(CAPTURE_LOCATION, getFileName('png'));
|
|
||||||
if (selectedDevice != null) {
|
if (selectedDevice != null) {
|
||||||
reportPlatformFailures(
|
await capture(selectedDevice);
|
||||||
selectedDevice
|
|
||||||
.screenshot()
|
|
||||||
.then(buffer => writeBufferToFile(pngPath, buffer))
|
|
||||||
.then(path => openFile(path)),
|
|
||||||
'captureScreenshot',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,15 @@ import {init as initLogger} from '../../fb-stubs/Logger.tsx';
|
|||||||
import configureStore from 'redux-mock-store';
|
import configureStore from 'redux-mock-store';
|
||||||
import {TEST_PASSING_GK, TEST_FAILING_GK} from '../../fb-stubs/GK.tsx';
|
import {TEST_PASSING_GK, TEST_FAILING_GK} from '../../fb-stubs/GK.tsx';
|
||||||
import TestPlugin from './TestPlugin';
|
import TestPlugin from './TestPlugin';
|
||||||
|
import {resetConfigForTesting} from '../../utils/processConfig.tsx';
|
||||||
|
|
||||||
const mockStore = configureStore([])(reducers(undefined, {type: 'INIT'}));
|
const mockStore = configureStore([])(reducers(undefined, {type: 'INIT'}));
|
||||||
const logger = initLogger(mockStore);
|
const logger = initLogger(mockStore);
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
resetConfigForTesting();
|
||||||
|
});
|
||||||
|
|
||||||
test('dispatcher dispatches REGISTER_PLUGINS', () => {
|
test('dispatcher dispatches REGISTER_PLUGINS', () => {
|
||||||
dispatcher(mockStore, logger);
|
dispatcher(mockStore, logger);
|
||||||
const actions = mockStore.getActions();
|
const actions = mockStore.getActions();
|
||||||
|
|||||||
@@ -7,25 +7,27 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function getUser() {
|
import {User} from '../reducers/user';
|
||||||
return Promise.reject();
|
|
||||||
|
export async function getUser(): Promise<User> {
|
||||||
|
throw new Error('Feature not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function internGraphPOSTAPIRequest(
|
export async function internGraphPOSTAPIRequest(
|
||||||
endpoint: string,
|
_endpoint: string,
|
||||||
formFields: {
|
_formFields: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
return Promise.reject();
|
throw new Error('Feature not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function graphQLQuery(query: string) {
|
export async function graphQLQuery(_query: string) {
|
||||||
return Promise.reject();
|
throw new Error('Feature not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logoutUser(): Promise<void> {
|
export function logoutUser(): Promise<void> {
|
||||||
return Promise.reject();
|
throw new Error('Feature not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DataExportResult = {
|
export type DataExportResult = {
|
||||||
@@ -44,12 +46,19 @@ export type DataExportError = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export async function shareFlipperData(
|
export async function shareFlipperData(
|
||||||
trace: string,
|
_trace: string,
|
||||||
): Promise<DataExportError | DataExportResult> {
|
): Promise<DataExportError | DataExportResult> {
|
||||||
new Notification('Feature not implemented');
|
new Notification('Feature not implemented');
|
||||||
return Promise.reject();
|
throw new Error('Feature not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writeKeychain(token: string) {
|
export async function writeKeychain(_token: string) {
|
||||||
return Promise.reject();
|
throw new Error('Feature not implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function uploadFlipperMedia(
|
||||||
|
_path: string,
|
||||||
|
_kind: 'Image' | 'Video',
|
||||||
|
): Promise<string> {
|
||||||
|
throw new Error('Feature not implemented');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,31 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import {BaseDevice} from 'flipper';
|
||||||
|
import {reportPlatformFailures} from './metrics';
|
||||||
|
import expandTilde from 'expand-tilde';
|
||||||
|
import {remote} from 'electron';
|
||||||
|
import config from '../utils/processConfig';
|
||||||
|
|
||||||
|
// TODO: refactor so this doesn't need to be exported
|
||||||
|
export const CAPTURE_LOCATION = expandTilde(
|
||||||
|
config().screenCapturePath || remote.app.getPath('desktop'),
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: refactor so this doesn't need to be exported
|
||||||
|
export function getFileName(extension: 'png' | 'mp4'): string {
|
||||||
|
// Windows does not like `:` in its filenames. Yes, I know ...
|
||||||
|
return `screencap-${new Date().toISOString().replace(/:/g, '')}.${extension}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function capture(device: BaseDevice): Promise<string> {
|
||||||
|
const pngPath = path.join(CAPTURE_LOCATION, getFileName('png'));
|
||||||
|
return reportPlatformFailures(
|
||||||
|
device.screenshot().then(buffer => writeBufferToFile(pngPath, buffer)),
|
||||||
|
'captureScreenshot',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a buffer to a specified file path.
|
* Writes a buffer to a specified file path.
|
||||||
|
|||||||
Reference in New Issue
Block a user