1
1
import fs from 'fs' ;
2
2
import { ServerOptions } from 'https' ;
3
3
import path from 'path' ;
4
+ import { spawn } from 'child_process' ;
5
+ import { rootDir } from 'config/paths' ;
4
6
5
7
require ( 'dotenv-flow' ) . config ( ) ;
6
8
7
- declare var process : {
8
- env : {
9
- [ key : string ] : string ,
10
- }
11
- } ;
12
-
13
9
const {
14
10
NODE_ENV ,
15
11
@@ -30,7 +26,9 @@ const {
30
26
31
27
AWS_ACCESS_KEY_ID ,
32
28
AWS_SECRET_ACCESS_KEY ,
33
- } = process . env ;
29
+ } = process . env as {
30
+ [ key : string ] : string ,
31
+ } ;
34
32
35
33
const isEnabled = ( v : string ) => v === '1' ;
36
34
@@ -67,12 +65,30 @@ export const webhookOptions = isEnabled(WEBHOOK_ENABLED) ? {
67
65
secret : WEBHOOK_SECRET ,
68
66
} : undefined ;
69
67
70
- const readCredentials = ( file : string ) => fs . readFileSync ( path . resolve ( CREDENTIALS_PATH , file ) ) ;
71
- export const credentials : ServerOptions | undefined = isEnabled ( CREDENTIALS_ENABLED ) ? {
72
- ca : readCredentials ( CREDENTIALS_CA ) ,
73
- key : readCredentials ( CREDENTIALS_KEY ) ,
74
- cert : readCredentials ( CREDENTIALS_CERT ) ,
75
- } : undefined ;
68
+ export let credentials : ServerOptions | undefined ;
69
+ if ( isEnabled ( CREDENTIALS_ENABLED ) ) {
70
+ if ( fs . existsSync ( CREDENTIALS_PATH ) ) {
71
+ const readCredentials = ( file : string ) => fs . readFileSync ( path . resolve ( CREDENTIALS_PATH , file ) ) ;
72
+ credentials = {
73
+ ca : readCredentials ( CREDENTIALS_CA ) ,
74
+ key : readCredentials ( CREDENTIALS_KEY ) ,
75
+ cert : readCredentials ( CREDENTIALS_CERT ) ,
76
+ } ;
77
+ } else {
78
+ const certbotIniPath = path . resolve ( rootDir , 'certbot.ini' ) ;
79
+ const childProcess = spawn ( 'certbot' , [ 'certonly' , '--non-interactive' , '--agree-tos' , '--config' , certbotIniPath ] ) ;
80
+ childProcess . stdout . pipe ( process . stdout ) ;
81
+ childProcess . stderr . pipe ( process . stderr ) ;
82
+ childProcess . on ( 'error' , console . error ) ;
83
+ childProcess . on ( 'exit' , code => {
84
+ if ( code === 0 ) {
85
+ process . exit ( 0 ) ;
86
+ } else {
87
+ console . error ( new Error ( `certbot failed with exit code ${ code } .` ) ) ;
88
+ }
89
+ } ) ;
90
+ }
91
+ }
76
92
77
93
export const githubClientId = GITHUB_CLIENT_ID ;
78
94
export const githubClientSecret = GITHUB_CLIENT_SECRET ;
0 commit comments