1
+ import type { TlsOption } from '../src/types'
1
2
import os from 'node:os'
2
3
import { log } from '@stacksjs/cli'
3
4
import { CAC } from 'cac'
@@ -7,35 +8,39 @@ import { config } from '../src/config'
7
8
8
9
const cli = new CAC ( 'tlsx' )
9
10
10
- interface Options {
11
- domain : string
12
- output : string
13
- key : string
14
- cert : string
15
- ca : string
16
- verbose : boolean
17
- }
18
-
19
11
cli
20
12
. command ( 'secure [domain]' , 'Auto generate a self-signed SSL certificate/s' )
21
- . option ( '-d, --domain [domain]' , 'Domain name' , { default : 'localhost' } )
22
- . option ( '-o, --output <output>' , 'Output directory' , { default : os . tmpdir ( ) } )
23
- . option ( '-k, --key <key>' , 'Output key file name' , { default : 'key.pem' } )
24
- . option ( '-c, --cert <cert>' , 'Output certificate file name' , { default : 'cert.pem' } )
25
- . option ( '-ca, --ca <ca>' , 'Output CA file name' , { default : 'ca.pem' } )
26
- . option ( '--verbose' , 'Enable verbose logging' , { default : false } )
13
+ . option ( '-k, --key-path <key>' , 'Output key file name' , { default : config . keyPath } )
14
+ . option ( '-c, --cert-path <cert>' , 'Output certificate file name' , { default : config . certPath } )
15
+ . option ( '-ca, --ca-path <ca>' , 'Output CA file name' , { default : config . caCertPath } )
16
+ . option ( '--alt-name-ips <ips>' , 'Alternative Name IPs (comma-separated)' , { default : config . altNameIPs . join ( ',' ) } )
17
+ . option ( '--alt-name-uris <uris>' , 'Alternative Name URIs (comma-separated)' , { default : config . altNameURIs . join ( ',' ) } )
18
+ . option ( '--common-name <name>' , 'Common Name for the certificate' , { default : config . commonName } )
19
+ . option ( '--country-name <name>' , 'Country Name for the certificate' , { default : config . countryName } )
20
+ . option ( '--state-name <name>' , 'State Name for the certificate' , { default : config . stateName } )
21
+ . option ( '--locality-name <name>' , 'Locality Name for the certificate' , { default : config . localityName } )
22
+ . option ( '--organization-name <name>' , 'Organization Name for the certificate' , { default : config . organizationName } )
23
+ . option ( '--validity-days <days>' , 'Validity Days for the certificate' , { default : config . validityDays } )
24
+ . option ( '--verbose' , 'Enable verbose logging' , { default : config . verbose } )
27
25
. usage ( 'tlsx secure <domain> [options]' )
28
26
. example ( 'tlsx secure example.com --output /etc/ssl' )
29
- . action ( async ( domain : string , options ?: Options ) => {
27
+ . action ( async ( domain : string , options ?: TlsOption ) => {
30
28
domain = domain ?? config ?. altNameURIs [ 0 ]
31
29
32
30
log . info ( `Generating a self-signed SSL certificate for: ${ domain } ` )
33
31
log . debug ( 'Options:' , options )
34
32
35
33
const caCert = await createRootCA ( )
36
34
const hostCert = await generateCert ( {
37
- hostCertCN : config ? .commonName ?? domain ,
35
+ hostCertCN : options ?. commonName ?? config . commonName ?? domain ,
38
36
domain,
37
+ altNameIPs : typeof options ?. altNameIPs === 'string' ? ( options . altNameIPs as string ) . split ( ',' ) : config . altNameIPs ,
38
+ altNameURIs : typeof options ?. altNameURIs === 'string' ? ( options . altNameURIs as string ) . split ( ',' ) : config . altNameURIs ,
39
+ countryName : options ?. countryName || config . countryName ,
40
+ stateName : options ?. stateName || config . stateName ,
41
+ localityName : options ?. localityName || config . localityName ,
42
+ organizationName : options ?. organizationName || config . organizationName ,
43
+ validityDays : Number ( options ?. validityDays ) || config . validityDays ,
39
44
rootCAObject : {
40
45
certificate : caCert . certificate ,
41
46
privateKey : caCert . privateKey ,
0 commit comments