包文件→ .shtukbase/packages/schedule/actions/launchd.ts
actions/launchd.ts
34 行, 1.8 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // `shtuk run schedule launchd [--every 5]`: writes a launchd plist that calls the tick of this project every N
- // minutes into .shtukbase/schedule/ and prints how to turn it on. It loads nothing: the owner does that.
- import { mkdirSync, writeFileSync } from 'node:fs';
- import { dirname, join, relative } from 'node:path';
- import { args } from '../../agents/lib/write.ts';
- import { everyArg, host } from '../lib/host.ts';
- const a = args(process.argv.slice(2));
- const root = process.cwd();
- const h = host(root, everyArg(a.every));
- const x = (s: string) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
- const plist = `<?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>Label</key><string>${x(h.label)}</string>
- <key>ProgramArguments</key>
- <array><string>${x(h.bun)}</string><string>${x(h.tick)}</string></array>
- <key>WorkingDirectory</key><string>${x(root)}</string>
- <key>EnvironmentVariables</key>
- <dict><key>PATH</key><string>${x(h.path)}</string></dict>
- <key>StartInterval</key><integer>${h.every * 60}</integer>
- <key>StandardOutPath</key><string>${x(h.log)}</string>
- <key>StandardErrorPath</key><string>${x(h.log)}</string>
- </dict>
- </plist>
- `;
- const file = join(root, '.shtukbase', 'schedule', `${h.label}.plist`);
- mkdirSync(dirname(file), { recursive: true });
- writeFileSync(file, plist);
- console.log(`written ${relative(root, file)} (a tick every ${h.every} min; nothing is switched on)`);
- console.log('to switch on (the owner does it):');
- console.log(` cp ${file} ~/Library/LaunchAgents/ && launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/${h.label}.plist`);
- console.log(`to switch off: launchctl bootout gui/$(id -u)/${h.label}`);