qwgit / lib / openssl-mkcert.js
http{/,s} git server
git clone http://git.nthia.dev/qwgit

const { spawn } = require('child_process')

module.exports = function (outdir, cb) {
  let commands = [
    ['openssl','genpkey','-algorithm','ed25519','-out','key.pem'],
    [ 'openssl', 'req', '-new', '-key', 'key.pem', '-out', 'csr.pem', '-subj', '/CN=localhost' ],
    [ 'openssl', 'x509', '-req', '-days', '7300', '-in', 'csr.pem', '-signkey', 'key.pem', '-out', 'cert.pem' ],
    [ 'openssl', 'x509', '-noout', '-in', 'cert.pem', '-fingerprint', '-sha512' ]
  ]
  ;(function next(i) {
    if (i >= commands.length) return cb()
    let cmd = commands[i]
    let ps = spawn(cmd[0], cmd.slice(1), {
      cwd: outdir,
      stdio: ['ignore','ignore','ignore'],
    })
    ps.once('exit', code => {
      if (code !== 0) cb(new Error(`non-zero exit code ${code} while running: ${cmd}`))
      else next(i+1)
    })
  })(0)
}