@johnf/react-native-owl
Version:
Visual regression testing for React Native
70 lines (68 loc) • 3.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.takeScreenshot = exports.cleanupScreenshots = void 0;
var _execa = _interopRequireDefault(require("execa"));
var _fs = require("fs");
var _path = _interopRequireDefault(require("path"));
var _fileExists = require("./utils/file-exists.js");
var _logger = require("./logger.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const cleanupScreenshots = async () => {
const latestDirPath = _path.default.join(process.cwd(), '.owl', 'latest');
await _fs.promises.rm(latestDirPath, {
recursive: true,
force: true
});
const diffDirPath = _path.default.join(process.cwd(), '.owl', 'diff');
await _fs.promises.rm(diffDirPath, {
recursive: true,
force: true
});
};
/**
* Takes a screenshot from the simulator.
* @param filename - Required. The filename(excluding the extension) that will be used to save the screenshot. ie. 'homepage'
* @returns the path to the screenshot.
*/
exports.cleanupScreenshots = cleanupScreenshots;
const takeScreenshot = async filename => {
const platform = process.env.OWL_PLATFORM;
const iosDevice = process.env.OWL_IOS_SIMULATOR;
const iosSimulator = iosDevice?.replace(/([ /])/g, '\\$1');
const debug = process.env.OWL_DEBUG === 'true';
const updateBaseline = process.env.OWL_UPDATE_BASELINE === 'true';
const screenshotFilename = `${filename}.png`;
const stdio = debug ? 'inherit' : 'ignore';
const logger = new _logger.Logger(!!debug);
const screenshotsDirPath = _path.default.join(process.cwd(), '.owl');
await _fs.promises.mkdir(screenshotsDirPath, {
recursive: true
});
const gitignoreExist = await (0, _fileExists.fileExists)(_path.default.join(screenshotsDirPath, '.gitignore'));
if (!gitignoreExist) {
await _fs.promises.writeFile(_path.default.join(process.cwd(), '.owl', '.gitignore'), '# generated by react-native-owl\ndiff/\nlatest/\nreport/\n');
}
const baselineExist = await (0, _fileExists.fileExists)(_path.default.join(screenshotsDirPath, 'baseline', platform, screenshotFilename));
const DIR_NAME = updateBaseline || !baselineExist ? 'baseline' : 'latest';
const cwd = _path.default.join(screenshotsDirPath, DIR_NAME, platform);
await _fs.promises.mkdir(cwd, {
recursive: true
});
const screenshotCommand = platform === 'ios' ? `xcrun simctl io ${iosSimulator} screenshot ${screenshotFilename}` : `adb exec-out screencap -p > ${screenshotFilename}`;
logger.info(`[OWL - CLI] Will run the screenshot command: ${screenshotCommand}.`);
await _execa.default.command(screenshotCommand, {
stdio,
cwd,
shell: platform === 'android'
});
if (!baselineExist) {
logger.print(`[OWL - CLI] ${screenshotFilename} baseline screenshot created.`);
}
const screenshotPath = `${cwd}/${screenshotFilename}`;
logger.info(`[OWL - CLI] Screenshot saved to ${screenshotPath}.`);
return screenshotPath;
};
exports.takeScreenshot = takeScreenshot;
//# sourceMappingURL=screenshot.js.map