desktop-activation.mjs 886 B

123456789101112131415161718
  1. import { randomUUID } from 'node:crypto'
  2. import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
  3. import { join } from 'node:path'
  4. export const desktopActivationPath = home => join(home, 'desktop-runtime-activation.json')
  5. export function hasDesktopActivation(home) {
  6. try { return JSON.parse(readFileSync(desktopActivationPath(home), 'utf8')).version === 1 }
  7. catch { return false }
  8. }
  9. export function grantDesktopActivation(home) {
  10. mkdirSync(home, { recursive: true, mode: 0o700 })
  11. const path = desktopActivationPath(home), temporary = `${path}.${randomUUID()}.tmp`
  12. try {
  13. writeFileSync(temporary, JSON.stringify({ version: 1 }), { mode: 0o600, flag: 'wx' })
  14. renameSync(temporary, path)
  15. } finally { rmSync(temporary, { force: true }) }
  16. }
  17. export function deferDesktopActivation(home) { rmSync(desktopActivationPath(home), { force: true }) }