90 lines
3.8 KiB
JavaScript
90 lines
3.8 KiB
JavaScript
// Run the installed JXA resolver against a pasteboard shared by shell fixture processes.
|
|
const fs = require('node:fs')
|
|
const vm = require('node:vm')
|
|
const {pathToFileURL} = require('node:url')
|
|
|
|
const [resolver, ...argv] = process.argv.slice(2)
|
|
const stateFile = 'test-pasteboard.json'
|
|
const state = fs.existsSync(stateFile) ? JSON.parse(fs.readFileSync(stateFile)) : {changeCount: 0, items: []}
|
|
const nil = {isNil: () => true}
|
|
const wrap = js => js == null ? nil : {
|
|
js, isNil: () => false, dataUsingEncoding: () => js
|
|
}
|
|
const unwrap = value => value?.js ?? value
|
|
const array = values => ({
|
|
count: values.length, isNil: () => false, objectAtIndex: i => values[i],
|
|
containsObject: v => values.some(entry => unwrap(entry) === unwrap(v))
|
|
})
|
|
const item = (flavors = {}) => ({
|
|
flavors,
|
|
get types() { return array(Object.keys(flavors).map(wrap)) },
|
|
dataForType: type => wrap(flavors[unwrap(type)]),
|
|
stringForType: type => wrap(flavors[unwrap(type)]),
|
|
setDataForType(value, type) { flavors[unwrap(type)] = unwrap(value); return true },
|
|
setStringForType(value, type) { flavors[unwrap(type)] = unwrap(value); return true }
|
|
})
|
|
|
|
const pasteboard = {
|
|
get pasteboardItems() {
|
|
const items = state.items.map(item)
|
|
if (fs.existsSync('test-reverse-file-readback')) items.reverse()
|
|
return array(items)
|
|
},
|
|
get changeCount() { return state.changeCount },
|
|
get clearContents() { state.items = []; return ++state.changeCount },
|
|
writeObjects(items) {
|
|
state.items = items.map(i => i.flavors)
|
|
state.changeCount++
|
|
return true
|
|
}
|
|
}
|
|
|
|
// The host reads the staged clipboard, optionally allowing a user copy before it releases the ACK.
|
|
class CurlTask {
|
|
get waitUntilExit() {
|
|
fs.writeFileSync('test-ack.json', JSON.stringify({
|
|
path: this.launchPath, args: this.arguments, config: this.standardInput.config, items: state.items
|
|
}))
|
|
if (fs.existsSync('test-user-copy.json')) {
|
|
state.items = JSON.parse(fs.readFileSync('test-user-copy.json'))
|
|
state.changeCount++
|
|
}
|
|
this.terminationStatus = fs.existsSync('test-fail-ack') ? 22 : 0
|
|
}
|
|
}
|
|
|
|
const $ = Object.assign(value => typeof value === 'string' ? wrap(value) : value, {
|
|
NSPasteboard: {generalPasteboard: pasteboard},
|
|
NSPasteboardItem: {alloc: {get init() { return item() }}},
|
|
NSPasteboardTypeFileURL: 'public.file-url',
|
|
NSPasteboardTypeString: 'public.utf8-plain-text',
|
|
NSData: {data: wrap('')},
|
|
NSString: {stringWithContentsOfFileEncodingError: path => wrap(fs.readFileSync(path, 'utf8'))},
|
|
NSURL: {fileURLWithPath: path => ({
|
|
absoluteString: wrap(pathToFileURL(path).href), fileReferenceURL: fs.existsSync(path) ? wrap(path) : nil
|
|
})},
|
|
NSFileManager: {defaultManager: {
|
|
fileExistsAtPathIsDirectory(path, dir) {
|
|
dir[0] = fs.statSync(path, {throwIfNoEntry: false})?.isDirectory() ?? false
|
|
return fs.existsSync(path)
|
|
},
|
|
contentsOfDirectoryAtPathError: path => fs.readdirSync(path),
|
|
fileExistsAtPath: fs.existsSync
|
|
}},
|
|
NSDate: {dateWithTimeIntervalSinceNow: seconds => seconds},
|
|
NSRunLoop: {currentRunLoop: {runUntilDate: () => fs.writeFileSync('test-pasteboard-flush', String(state.changeCount))}},
|
|
NSProcessInfo: {processInfo: {environment: {objectForKey: key => wrap(process.env[unwrap(key)])}}},
|
|
NSTask: {alloc: {get init() { return new CurlTask() }}},
|
|
NSPipe: {get pipe() {
|
|
const pipe = {fileHandleForWriting: {writeData(data) { pipe.config = data }}}
|
|
return pipe
|
|
}},
|
|
NSFileHandle: {fileHandleWithNullDevice: null}
|
|
})
|
|
const context = {ObjC: {import() {}, unwrap}, $, argv, Ref: () => []}
|
|
try {
|
|
console.log(vm.runInNewContext(fs.readFileSync(resolver, 'utf8') + '\nrun(argv)', context))
|
|
} finally {
|
|
fs.writeFileSync(stateFile, JSON.stringify(state))
|
|
}
|