Skip to content

Commit

Permalink
First commit 🧙🏼‍♂️
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamote committed Nov 17, 2022
0 parents commit d0913de
Show file tree
Hide file tree
Showing 23 changed files with 281 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/makecert.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
19.1.0
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 🔐 makecert

`makecert` is a small utility to generate self-signed certificates for local development.

## Prerequisites

This package requires `mkcert` to be installed on your system. Please follow the [installation instructions](https://github.com/FiloSottile/mkcert#installation) for your platform.

## Installation

```shell
# With Yarn
yarn add makecert

# or with npm
npm i makecert
```

## Usage

```ts
import makeCert from 'makecert';

const { key, cert } = await makeCert();
```

## ✨Roadmap

- [ ] Make it a class, so we offer more ways to retrieve the cert
- [ ] Add a check to see if `mkcert` is installed and if not, install it
- [ ] Make sure that `mkcert` was initialized: `mkcert -install`
- [ ] Add a check to see if the certificate is already created and ask the user if they want to overwrite it
- [ ] Add more options for customizations (e.g. hosts, expiration time etc.)
- [ ] Add a way to revoke the certificate
- [ ] Add a way to check if the certificate is valid
- [ ] Allow creating multiple certificates
- [ ] Add tests
- [ ] Add CI/CD
- [ ] Add ESLint and Prettier

_There is no place like 127.0.0.1 🏠_
9 changes: 9 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="node" />
/**
* Create a certificate using `mkcert` and return it
*/
declare const makeCert: () => Promise<{
key: Buffer;
cert: Buffer;
}>;
export default makeCert;
46 changes: 46 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions dist/info.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function getInfo(): {
name: any;
version: any;
};
11 changes: 11 additions & 0 deletions dist/info.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/info.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added dist/types.d.ts
Empty file.
2 changes: 2 additions & 0 deletions dist/types.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/types.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "makecert",
"version": "0.0.0",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"author": "David Gamote",
"repository": {
"type": "git",
"url": "https://github.com/Gamote/makecert.git"
},
"keywords": ["makecert", "certificate", "ssl", "https", "tls", "local", "development", "mkcert"],
"scripts": {
"build": "rm -rf dist && tsc",
"start": "node dist/index.js"
},
"dependencies": {
"typescript": "^4.9.3"
},
"devDependencies": {
"@types/node": "^18.11.9"
}
}
56 changes: 56 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import util from 'util';
import {exec, ExecSyncOptionsWithBufferEncoding} from 'child_process';
import fs from 'fs';
import path from 'path';
import getInfo from "./info";

const { name } = getInfo();

const execAsync = util.promisify(exec);

const config = {
tempDir: `node_modules/.${name}`,
privateKeyName: 'private_key.pem',
certName: 'cert.pem',
hosts: ['localhost', '0.0.0.0', '::1'],
execOptions: {
stdio: 'pipe',
} as ExecSyncOptionsWithBufferEncoding,
};

/**
* Create a certificate using `mkcert` and return it
*/
const makeCert = async () => {
try {
// Create the temp directory
await execAsync(`mkdir -p ${config.tempDir}`, config.execOptions);

// Create the certificate
await execAsync(
`mkcert -key-file ${config.tempDir}/${config.privateKeyName} -cert-file ${config.tempDir}/${config.certName} ${config.hosts.join(' ')}`,
config.execOptions,
);
} catch (e) {
throw new Error(
// @ts-ignore
`"${name}" failed with the following error:\n${e?.stderr?.toString()}`,
);
}

// Read the private key
const privateKey = await fs.readFileSync(
path.resolve(process.cwd(), config.tempDir, config.privateKeyName),
);

// Read the certificate
const cert = await fs.readFileSync(
path.resolve(process.cwd(), config.tempDir, config.certName),
);

// TODO: should we delete the files after we're done? (maybe not, because we might need them again)

return { key: privateKey, cert };
};

export default makeCert;
8 changes: 8 additions & 0 deletions src/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Cannot be `import` as it's not under TS root dir
// This way it will be excluded from the `dist` folder
// More info [here](https://stackoverflow.com/a/57934516/10878244)
const { name, version } = require('../package.json');

export default function getInfo() {
return { name, version };
}
Empty file added src/types.ts
Empty file.
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"declaration": true,
"strictNullChecks": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "./dist",
"baseUrl": "./",
"resolveJsonModule": true,
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": ["node_modules", "dist"]
}
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/node@^18.11.9":
version "18.11.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.9.tgz#02d013de7058cea16d36168ef2fc653464cfbad4"
integrity sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==

typescript@^4.9.3:
version "4.9.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.3.tgz#3aea307c1746b8c384435d8ac36b8a2e580d85db"
integrity sha512-CIfGzTelbKNEnLpLdGFgdyKhG23CKdKgQPOBc+OUNrkJ2vr+KSzsSV5kq5iWhEQbok+quxgGzrAtGWCyU7tHnA==

0 comments on commit d0913de

Please sign in to comment.