htpdate/adjust_time.coffee

57 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2021-01-25 05:20:52 +01:00
util = require 'util'
{ spawn } = require 'child_process'
dayjs = require 'dayjs'
dayjs.extend require 'dayjs/plugin/utc'
2021-01-25 05:20:52 +01:00
platform = require('os').platform()
adjust_time = (delta) ->
console.log 'Adjust time...'
shell = spawn spawn_args...
shell.stderr.on 'data', (data) -> console.error "> " + "#{data}".trim()
shell.stdout.on 'data', (data) -> console.debug "> " + "#{data}".trim()
shell.on 'close', (code) ->
shell.stdin.end()
console.log 'Done'
process.exit 0
wait_data = util.promisify (cb) ->
shell.stdout.once 'data', (chunk) ->
setTimeout ->
cb null, chunk
, 1
input_line = util.promisify (cmd, cb) ->
console.debug "$ #{cmd}"
shell.stdin.write "#{cmd}\n", (p...) ->
await wait_data() if platform in ['win32']
cb p...
await wait_data() if platform in ['win32']
cmd = dayjs().add(delta, 'ms').utc().format adjust_time.command
2021-01-25 05:20:52 +01:00
await input_line cmd
await input_line 'exit'
COMMANDS = {
2021-11-03 16:41:19 +01:00
win32: '[wmic os set localdatetime=]YYYYMMDDHHmmss[.]SSS[000][+000]'
linux: '[date --utc -s ]YYYY[-]MM[-]DDTHH[:]mm[:]ss[.]SSS'
2021-01-25 05:20:52 +01:00
}
adjust_time.command = COMMANDS[platform] or COMMANDS.linux
SHELL_ARGS = {
win32: [
'cmd'
['/q', '/k', 'prompt $H && chcp 65001 > nul']
{
windowsHide: true
}
]
linux: [
'sh'
]
}
spawn_args = SHELL_ARGS[platform] or SHELL_ARGS.linux
if not /UTF\-8/i.test (process.env.LANGUAGE or '')
process.env.LANGUAGE = 'C.UTF-8'
module.exports = adjust_time