macos-defaults/record/textedit/RichText/false.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

const MacRunner = require('../../mac-runner')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const { compressPngImage } = require('../../utils')
module.exports = {
run: async (outputPath) => {
console.log('> Recording TextEdit opens rich text files to true')
try {
2021-12-20 19:58:38 +00:00
const fileName = '/tmp/lorem.txt'
await manageFile(fileName, true)
2021-12-20 19:58:38 +00:00
await addContentToFile(fileName, '')
const runner = new MacRunner()
await runner
2021-12-20 19:58:38 +00:00
.setDefault('com.apple.TextEdit', 'RichText', '-bool false', '0')
.openApp('TextEdit', fileName)
2022-08-18 14:12:33 +00:00
.moveAndResizeApp('TextEdit', 0, 0, 740, 400)
.captureApp('TextEdit', `${outputPath}/false.png`)
2021-12-20 19:58:38 +00:00
.deleteDefault('com.apple.TextEdit', 'RichText')
.killApp('TextEdit')
.run()
2021-12-20 19:58:38 +00:00
await manageFile(fileName, false)
} catch (runnerError) {
throw new Error(runnerError)
}
try {
await compressPngImage(`${outputPath}/false.png`, outputPath, 'false')
} catch (compressPngImageError) {
throw new Error(compressPngImageError)
}
return { filepath: `${outputPath}/false` }
2021-12-20 19:58:38 +00:00
},
}
async function manageFile(filename, create) {
console.log(` Command: ${create ? 'create' : 'remove'} ${filename}`)
const { stderr: mngFile } = await exec(
`${create ? 'touch' : 'rm -f'} ${filename}`,
)
if (mngFile) {
2021-12-20 19:58:38 +00:00
console.error('An error occured while working with a file')
throw new Error(mngFile)
}
}
async function addContentToFile(filename, content) {
console.log(` Command: add content to ${filename}`)
const { stderr: mngFile } = await exec(
`cat > ${filename} << EOF
${content}
EOF`,
)
if (mngFile) {
2021-12-20 19:58:38 +00:00
console.error('An error occured while working with a file')
throw new Error(mngFile)
}
}