ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/schedule/actions/launchd.ts

actions/launchd.ts

34 líneas, 1.8 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // `shtuk run schedule launchd [--every 5]`: writes a launchd plist that calls the tick of this project every N
  2. // minutes into .shtukbase/schedule/ and prints how to turn it on. It loads nothing: the owner does that.
  3. import { mkdirSync, writeFileSync } from 'node:fs';
  4. import { dirname, join, relative } from 'node:path';
  5. import { args } from '../../agents/lib/write.ts';
  6. import { everyArg, host } from '../lib/host.ts';
  7. const a = args(process.argv.slice(2));
  8. const root = process.cwd();
  9. const h = host(root, everyArg(a.every));
  10. const x = (s: string) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  11. const plist = `<?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  13. <plist version="1.0">
  14. <dict>
  15. <key>Label</key><string>${x(h.label)}</string>
  16. <key>ProgramArguments</key>
  17. <array><string>${x(h.bun)}</string><string>${x(h.tick)}</string></array>
  18. <key>WorkingDirectory</key><string>${x(root)}</string>
  19. <key>EnvironmentVariables</key>
  20. <dict><key>PATH</key><string>${x(h.path)}</string></dict>
  21. <key>StartInterval</key><integer>${h.every * 60}</integer>
  22. <key>StandardOutPath</key><string>${x(h.log)}</string>
  23. <key>StandardErrorPath</key><string>${x(h.log)}</string>
  24. </dict>
  25. </plist>
  26. `;
  27. const file = join(root, '.shtukbase', 'schedule', `${h.label}.plist`);
  28. mkdirSync(dirname(file), { recursive: true });
  29. writeFileSync(file, plist);
  30. console.log(`written ${relative(root, file)} (a tick every ${h.every} min; nothing is switched on)`);
  31. console.log('to switch on (the owner does it):');
  32. console.log(` cp ${file} ~/Library/LaunchAgents/ && launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/${h.label}.plist`);
  33. console.log(`to switch off: launchctl bootout gui/$(id -u)/${h.label}`);