/
aspnetcore-https.ts
33 lines (27 loc) · 1.12 KB
/
aspnetcore-https.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import * as fs from 'fs'
import * as path from 'path'
import * as child_process from 'child_process'
const spawn = child_process.spawn;
const baseFolder = process.env.APPDATA !== undefined && process.env.APPDATA !== ''
? `${process.env.APPDATA}/ASP.NET/https`
: `${process.env.HOME}/.aspnet/https`;
const certArg = process.argv.map(arg => arg.match('/--name=(?<value>.+)/i')).filter(Boolean)[0];
const certName = certArg ? certArg?.groups?.value : process.env.npm_package_name;
if (!certName) {
console.error('Invalid certificate name. Run this script in the context of an npm/yarn script or pass --name=<<app>> explicitly');
process.exit(-1);
}
const moduleCertFilePath = path.join(baseFolder, `${certName}.pem`);
const moduleKeyFilePath = path.join(baseFolder, `${certName}.key`);
if (!fs.existsSync(moduleCertFilePath) || !fs.existsSync(moduleKeyFilePath)) {
spawn('dotnet', [
'dev-certs',
'https',
'--export-path',
moduleCertFilePath,
'--format',
'Pem',
'--no-password',
], { stdio: 'inherit', })
.on('exit', (code: any) => process.exit(code));
}