Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): Convert get-ssl-cert to typescript #22447

Merged
merged 6 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import withResolverContext from "../schema/context"
import sourceNodes from "../utils/source-nodes"
import { createSchemaCustomization } from "../utils/create-schema-customization"
import websocketManager from "../utils/websocket-manager"
import getSslCert from "../utils/get-ssl-cert"
import { getSslCert } from "../utils/get-ssl-cert"
import { slash } from "gatsby-core-utils"
import { initTracer } from "../utils/tracer"
import apiRunnerNode from "../utils/api-runner-node"
Expand All @@ -58,7 +58,7 @@ import {
showFeedbackRequest,
} from "../utils/feedback"

import { BuildHTMLStage, IProgram } from "./types"
import { BuildHTMLStage, IProgram, ICert } from "./types"

// checks if a string is a valid ip
const REGEX_IP = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/
Expand Down Expand Up @@ -401,12 +401,12 @@ module.exports = async (program: IProgram): Promise<void> => {
)
}

program.ssl = await getSslCert({
program.ssl = (await getSslCert({
name: sslHost,
certFile: program[`cert-file`],
keyFile: program[`key-file`],
directory: program.directory,
})
})) as ICert
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
}

// Start bootstrap process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock(`devcert`, () => {

const { certificateFor } = require(`devcert`)
const reporter = require(`gatsby-cli/lib/reporter`)
const getSslCert = require(`../get-ssl-cert`)
const { getSslCert } = require(`../get-ssl-cert`)

describe(`gets ssl certs`, () => {
beforeEach(() => {
Expand All @@ -28,9 +28,9 @@ describe(`gets ssl certs`, () => {
certificateFor.mockClear()
})
describe(`Custom SSL certificate`, () => {
it.each([[{ certFile: `foo` }], [{ keyFile: `bar` }]])(
it.each([{ certFile: `foo` }, { keyFile: `bar` }])(
`panic if cert and key are not both included`,
args => {
(args: any): void => {
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
getSslCert(args)

expect(reporter.panic.mock.calls).toMatchSnapshot()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
const report = require(`gatsby-cli/lib/reporter`)
const fs = require(`fs`)
const path = require(`path`)
const os = require(`os`)
import report from "gatsby-cli/lib/reporter"
import fs from "fs"
import path from "path"
import os from "os"
import { ICert } from "../commands/types"

const absoluteOrDirectory = (directory, filePath) => {
const absoluteOrDirectory = (directory: string, filePath: string): string => {
// Support absolute paths
if (path.isAbsolute(filePath)) {
return filePath
}
return path.join(directory, filePath)
}

module.exports = async ({ name, certFile, keyFile, directory }) => {
interface IGetSslCertArgs {
name: string
certFile?: string
keyFile?: string
directory: string
}

export const getSslCert = async ({
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
name,
certFile,
keyFile,
directory,
}: IGetSslCertArgs): Promise<ICert | false> => {
// check that cert file and key file are both true or both false, if they are both
// false, it defaults to the automatic ssl
if (certFile ? !keyFile : keyFile) {
Expand All @@ -25,11 +38,11 @@ module.exports = async ({ name, certFile, keyFile, directory }) => {
const keyPath = absoluteOrDirectory(directory, keyFile)
const certPath = absoluteOrDirectory(directory, certFile)

return await {
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
return {
keyPath,
certPath,
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
key: fs.readFileSync(keyPath, `utf-8`),
cert: fs.readFileSync(certPath, `utf-8`),
}
}

Expand Down