♻️ Use MacRunner

This commit is contained in:
Yann Bertrand 2022-08-18 11:02:00 +02:00
parent 8bca39f739
commit 9feedb13c9
5 changed files with 49 additions and 144 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View file

@ -137,11 +137,14 @@ class MacRunner {
* Capture the app window into a file
* @param {*} appName Application name to capture
* @param {*} output Output file name (png)
* @param {boolean} disableShadow Do not capture the App shadow
*/
captureApp(appName, output) {
captureApp(appName, output, disableShadow = true) {
return this.register(() =>
execCommand(
`screencapture -o -l$(osascript -e 'tell app "${appName}" to id of window 1') ${output}`
`screencapture ${
disableShadow ? '-o' : ''
} -l$(osascript -e 'tell app "${appName}" to id of window 1') ${output}`
)
)
}

View file

@ -1,85 +1,36 @@
const delay = require('delay')
const glob = require('glob-promise')
const robot = require('robotjs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const { makeAppActive, compressPngImage } = require('../../utils')
const MacRunner = require('../../mac-runner')
const { compressPngImage } = require('../../utils')
module.exports = {
run: async (outputPath) => {
console.log('> Recording screenshot disable-shadow with param set to false')
const { stderr: setEnvError } = await exec(
'defaults write com.apple.screencapture show-thumbnail -bool false && defaults write com.apple.screencapture disable-shadow -bool false'
console.log(
'> Recording screencapture disable-shadow with param set to false'
)
if (setEnvError) {
console.error(
'An error occured while setting up the screenshot disble-shadow command'
)
logRollbackInfo()
throw new Error(setEnvError)
}
// Preparation
robot.moveMouse(
robot.getScreenSize().width / 2,
robot.getScreenSize().height / 2
)
robot.mouseClick()
await makeAppActive('Safari')
robot.keyTap('l', 'command')
await delay(100)
robot.keyTap('h')
robot.typeString('ttps://www.apple.com/macos/')
await delay(100)
robot.keyTap('enter')
await delay(8000)
// Screenshot
robot.keyTap('4', ['command', 'shift'])
await delay(100)
robot.keyTap('space')
await delay(100)
robot.keyTap('enter')
await delay(100)
robot.keyTap('w', 'command')
await delay(1000)
const screenshot = (
await glob(`/Users/${process.env.USER}/Desktop/*.png`)
).pop()
try {
await compressPngImage(screenshot, outputPath, 'false')
const runner = new MacRunner()
await runner
.openApp('Safari', '-F https://apple.com')
.activateApp('Safari')
.moveAndResizeApp('Safari', 0, 0, 740 * 2, 413 * 2)
// We do not set the default as `captureApp` takes a param to disable shadow
.captureApp('Safari', `${outputPath}/false-tmp.png`, false)
.killApp('Safari')
.run()
} catch (runnerError) {
logRollbackInfo()
throw new Error(runnerError)
}
try {
await compressPngImage(`${outputPath}/false-tmp.png`, outputPath, 'false')
} catch (compressPngImageError) {
logRollbackInfo()
throw new Error(compressPngImageError)
}
const { stderr: deleteEnvError } = await exec(
'defaults delete com.apple.screencapture show-thumbnail && defaults delete com.apple.screencapture disable-shadow'
)
if (deleteEnvError) {
console.error(
'An error occured while cleaning the dock autohide-delay environment'
)
logRollbackInfo()
throw new Error(deleteEnvError)
}
return { filepath: `${outputPath}/false`, isVideo: false }
return { filepath: `${outputPath}/false` }
},
}
function logRollbackInfo() {
console.info(
'Please manually run this command to make sure everything is properly reset:'
)
console.info(
'defaults delete com.apple.screencapture show-thumbnail && defaults delete com.apple.screencapture disable-shadow'
)
}
function logRollbackInfo() {}

View file

@ -1,85 +1,36 @@
const delay = require('delay')
const glob = require('glob-promise')
const robot = require('robotjs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const { makeAppActive, compressPngImage } = require('../../utils')
const MacRunner = require('../../mac-runner')
const { compressPngImage } = require('../../utils')
module.exports = {
run: async (outputPath) => {
console.log('> Recording screenshot disable-shadow with param set to true')
const { stderr: setEnvError } = await exec(
'defaults write com.apple.screencapture show-thumbnail -bool false && defaults write com.apple.screencapture disable-shadow -bool true'
console.log(
'> Recording screencapture disable-shadow with param set to true'
)
if (setEnvError) {
console.error(
'An error occured while setting up the screenshot disble-shadow command'
)
logRollbackInfo()
throw new Error(setEnvError)
}
// Preparation
robot.moveMouse(
robot.getScreenSize().width / 2,
robot.getScreenSize().height / 2
)
robot.mouseClick()
await makeAppActive('Safari')
robot.keyTap('l', 'command')
await delay(100)
robot.keyTap('h')
robot.typeString('ttps://www.apple.com/macos/')
await delay(100)
robot.keyTap('enter')
await delay(8000)
// Screenshot
robot.keyTap('4', ['command', 'shift'])
await delay(100)
robot.keyTap('space')
await delay(100)
robot.keyTap('enter')
await delay(100)
robot.keyTap('w', 'command')
await delay(1000)
const screenshot = (
await glob(`/Users/${process.env.USER}/Desktop/*.png`)
).pop()
try {
await compressPngImage(screenshot, outputPath, 'true')
const runner = new MacRunner()
await runner
.openApp('Safari', '-F https://apple.com')
.activateApp('Safari')
.moveAndResizeApp('Safari', 0, 0, 740 * 2, 413 * 2)
// We do not set the default as `captureApp` takes a param to disable shadow
.captureApp('Safari', `${outputPath}/true-tmp.png`, true)
.killApp('Safari')
.run()
} catch (runnerError) {
logRollbackInfo()
throw new Error(runnerError)
}
try {
await compressPngImage(`${outputPath}/true-tmp.png`, outputPath, 'true')
} catch (compressPngImageError) {
logRollbackInfo()
throw new Error(compressPngImageError)
}
const { stderr: deleteEnvError } = await exec(
'defaults delete com.apple.screencapture show-thumbnail && defaults delete com.apple.screencapture disable-shadow'
)
if (deleteEnvError) {
console.error(
'An error occured while cleaning the dock autohide-delay environment'
)
logRollbackInfo()
throw new Error(deleteEnvError)
}
return { filepath: `${outputPath}/true`, isVideo: false }
return { filepath: `${outputPath}/true` }
},
}
function logRollbackInfo() {
console.info(
'Please manually run this command to make sure everything is properly reset:'
)
console.info(
'defaults delete com.apple.screencapture show-thumbnail && defaults delete com.apple.screencapture disable-shadow'
)
}
function logRollbackInfo() {}