Get certificate from async function?

39 Views Asked by At

I am trying to convert my build of a JavaSript Outlook Add-In from webpack to parceljs.

Mostly works, but fails on the configuration of the certificate for https.

The webpack configuration used an npm module called "office-addin-dev-certs" to dynamically install and use a certificate:

webpack.conf

const devCerts = require("office-addin-dev-certs");
async function getHttpsOptions() {
  const httpsOptions = await devCerts.getHttpsServerOptions();
  return { ca: httpsOptions.ca, key: httpsOptions.key, cert: httpsOptions.cert };
}
...
 server: {
        type: "https",
        options: await getHttpsOptions(),
      },

However parcel expects the certificate file and the key as command line parameters:

package.json:

{
   "scripts": {
      "dev-server": "parcel --https --cert certificate.cert --key private.key"
   }
}

How can I call the async function from the node module and make the parameters dynamic?

1

There are 1 best solutions below

0
eekboom On

Found a solution that works for me by copying certificate and private key to fixed file names using a node script:

install-certificate.js

const devCerts = require("office-addin-dev-certs");
const fs = require('node:fs');

devCerts
    .getHttpsServerOptions()
    .then(httpsOptions => {
        if (!fs.existsSync('build')) {
            fs.mkdirSync('build')
        }
        fs.writeFileSync('build/localhost.crt', httpsOptions.cert.toString());
        fs.writeFileSync('build/localhost.key', httpsOptions.key.toString());
    });

Then invoking this as a "pre" script in package.json:

  "scripts": {
    "predev-server": "node install-certificate.js",
    "dev-server": "parcel --https --port 3000 --cert build/localhost.crt --key build/localhost.key",