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:
John Knox
2020-02-07 12:50:47 -08:00
committed by Facebook Github Bot
parent f0a482af2f
commit 2acf81027e
4 changed files with 54 additions and 30 deletions

View File

@@ -8,6 +8,31 @@
*/
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.