Skip to content

Commit 174ff4e

Browse files
committed
chore: several improvements
1 parent bea49da commit 174ff4e

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

bin/cli.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { TlsOption } from '../src/types'
12
import os from 'node:os'
23
import { log } from '@stacksjs/cli'
34
import { CAC } from 'cac'
@@ -7,35 +8,39 @@ import { config } from '../src/config'
78

89
const cli = new CAC('tlsx')
910

10-
interface Options {
11-
domain: string
12-
output: string
13-
key: string
14-
cert: string
15-
ca: string
16-
verbose: boolean
17-
}
18-
1911
cli
2012
.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 })
2725
.usage('tlsx secure <domain> [options]')
2826
.example('tlsx secure example.com --output /etc/ssl')
29-
.action(async (domain: string, options?: Options) => {
27+
.action(async (domain: string, options?: TlsOption) => {
3028
domain = domain ?? config?.altNameURIs[0]
3129

3230
log.info(`Generating a self-signed SSL certificate for: ${domain}`)
3331
log.debug('Options:', options)
3432

3533
const caCert = await createRootCA()
3634
const hostCert = await generateCert({
37-
hostCertCN: config?.commonName ?? domain,
35+
hostCertCN: options?.commonName ?? config.commonName ?? domain,
3836
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,
3944
rootCAObject: {
4045
certificate: caCert.certificate,
4146
privateKey: caCert.privateKey,

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@
6262
"release": "bun run changelog && bunx bumpp package.json --all",
6363
"test": "bun test",
6464
"typecheck": "bun --bun tsc --noEmit",
65-
"dev:docs": "vitepress dev docs",
66-
"build:docs": "vitepress build docs",
67-
"preview:docs": "vitepress preview docs"
65+
"dev:docs": "bunx --bun vitepress dev docs",
66+
"build:docs": "bunx --bun vitepress build docs",
67+
"preview:docs": "bunx --bun vitepress preview docs"
6868
},
6969
"dependencies": {
7070
"@stacksjs/cli": "^0.68.2",

src/certificate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function getCertNotBefore(): Date {
2424
const year = twoDaysAgo.getFullYear()
2525
const month = (twoDaysAgo.getMonth() + 1).toString().padStart(2, '0')
2626
const day = twoDaysAgo.getDate().toString().padStart(2, '0')
27-
return new Date(`${year}-${month}-${day}T00:00:00Z`)
27+
return new Date(`${year}-${month}-${day}T23:59:59Z`)
2828
}
2929

3030
/**

src/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import type { TlsConfig } from './types'
22
import os from 'node:os'
33
import path from 'node:path'
4-
import process from 'node:process'
54
import { loadConfig } from 'bun-config'
65

76
// eslint-disable-next-line antfu/no-top-level-await
87
export const config: TlsConfig = await loadConfig({
98
name: 'tls',
10-
cwd: process.cwd(),
119
defaultConfig: {
1210
altNameIPs: ['127.0.0.1'],
1311
altNameURIs: ['localhost'],

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface TlsConfig {
44
caCertPath: string
55
certPath: string
66
keyPath: string
7-
rootCAObject: { certificate: string, privateKey: string }
7+
rootCAObject?: { certificate: string, privateKey: string }
88
altNameIPs: string[]
99
altNameURIs: string[]
1010
commonName: string
@@ -13,6 +13,7 @@ export interface TlsConfig {
1313
localityName: string
1414
organizationName: string
1515
validityDays: number
16+
verbose: boolean
1617
}
1718

1819
export interface CertOption {

tls.config.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ const config: TlsConfig = {
88
caCertPath: path.join(os.homedir(), '.stacks', 'ssl', `tlsx.localhost.ca.crt`),
99
certPath: path.join(os.homedir(), '.stacks', 'ssl', `tlsx.localhost.crt`),
1010
keyPath: path.join(os.homedir(), '.stacks', 'ssl', `tlsx.localhost.crt.key`),
11-
rootCAObject: {
12-
certificate: '',
13-
privateKey: '',
14-
},
1511
altNameIPs: ['127.0.0.1'],
1612
altNameURIs: ['localhost'],
1713
organizationName: 'stacksjs.org',

0 commit comments

Comments
 (0)