Skip to content

Commit

Permalink
feat(cli): add command to reset host created
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaslz committed Jan 25, 2024
1 parent dfe2b11 commit 3027138
Show file tree
Hide file tree
Showing 22 changed files with 1,064 additions and 76 deletions.
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Request to <https://local.your-domain.tld/app-name> will serve <http://localhost
- [remove domain](#remove-domain)
- [remove location for a domain](#remove-location-for-a-domain)
- [list current service configs](#list-current-service-configs)
- [reset hosts](#reset-hosts)
- [Use case](#use-case)
- [Other use cases](#other-use-cases)
- [Requirements](#requirements)
Expand Down Expand Up @@ -118,6 +119,12 @@ $ local-ssl remove local.your-domain.com --location /new-app
$ local-ssl list
```

### reset hosts

```bash
$ local-ssl reset
```

## Use case

Sometimes we need to use HTTPS for some security restrictions or just to work closely to the PRODUCTION reality.
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ vi.mock("fs");
vi.mock("consola");
vi.mock("shelljs");

vi.mock("path", () => ({
default: {
resolve: () => "/root/path",
},
}));

beforeEach(() => {
vi.resetAllMocks();
vi.clearAllMocks();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/__snapshots__/list-container.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`List container > success > commmand found local-ssl-management container running 1`] = `
exports[`Actions - listContainer > success > commmand found local-ssl-management container running 1`] = `
[MockFunction spy] {
"calls": [
[
Expand Down
32 changes: 16 additions & 16 deletions packages/cli/src/__snapshots__/on-create-action.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`On create action > success > multiple services > domain config created sucessfully (does not exists /ssl) 1`] = `
exports[`Actions - onCreateAction > success > multiple services > domain config created sucessfully (does not exists /ssl) 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/nginx.conf",
"/root/path/.local-ssl-management/nginx.conf",
"user nginx;
worker_processes 20;
Expand Down Expand Up @@ -81,7 +81,7 @@ http {
}",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/Dockerfile",
"/root/path/.local-ssl-management/Dockerfile",
"FROM nginx
# RUN rm -f /etc/nginx/conf.d/default.conf
Expand All @@ -105,7 +105,7 @@ EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/config.json",
"/root/path/.local-ssl-management/config.json",
"[
{
"id": "48d1a85c-377a-40ef-8a82-d1405f7a074f",
Expand Down Expand Up @@ -138,11 +138,11 @@ CMD ["nginx", "-g", "daemon off;"]",
}
`;

exports[`On create action > success > multiple services > domain config created sucessfully 1`] = `
exports[`Actions - onCreateAction > success > multiple services > domain config created sucessfully 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/nginx.conf",
"/root/path/.local-ssl-management/nginx.conf",
"user nginx;
worker_processes 20;
Expand Down Expand Up @@ -243,7 +243,7 @@ location ~ ^/app-name(/?)(.*) {
}",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/Dockerfile",
"/root/path/.local-ssl-management/Dockerfile",
"FROM nginx
# RUN rm -f /etc/nginx/conf.d/default.conf
Expand All @@ -267,7 +267,7 @@ EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/config.json",
"/root/path/.local-ssl-management/config.json",
"[
{
"id": "54a04fef-f263-4eab-a17a-016c76986160",
Expand Down Expand Up @@ -305,11 +305,11 @@ CMD ["nginx", "-g", "daemon off;"]",
}
`;

exports[`On create action > success > single service > domain config created sucessfully (does not exists /ssl) 1`] = `
exports[`Actions - onCreateAction > success > single service > domain config created sucessfully (does not exists /ssl) 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/nginx.conf",
"/root/path/.local-ssl-management/nginx.conf",
"user nginx;
worker_processes 20;
Expand Down Expand Up @@ -386,7 +386,7 @@ http {
}",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/Dockerfile",
"/root/path/.local-ssl-management/Dockerfile",
"FROM nginx
# RUN rm -f /etc/nginx/conf.d/default.conf
Expand All @@ -410,7 +410,7 @@ EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/config.json",
"/root/path/.local-ssl-management/config.json",
"[
{
"id": "48d1a85c-377a-40ef-8a82-d1405f7a074f",
Expand Down Expand Up @@ -443,11 +443,11 @@ CMD ["nginx", "-g", "daemon off;"]",
}
`;

exports[`On create action > success > single service > domain config created sucessfully 1`] = `
exports[`Actions - onCreateAction > success > single service > domain config created sucessfully 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/nginx.conf",
"/root/path/.local-ssl-management/nginx.conf",
"user nginx;
worker_processes 20;
Expand Down Expand Up @@ -524,7 +524,7 @@ http {
}",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/Dockerfile",
"/root/path/.local-ssl-management/Dockerfile",
"FROM nginx
# RUN rm -f /etc/nginx/conf.d/default.conf
Expand All @@ -548,7 +548,7 @@ EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]",
],
[
"/Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management/config.json",
"/root/path/.local-ssl-management/config.json",
"[
{
"id": "48d1a85c-377a-40ef-8a82-d1405f7a074f",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/__snapshots__/on-list-action.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`On list action > list domains availables 1`] = `
exports[`Actions - onListAction > list domains availables 1`] = `
[MockFunction spy] {
"calls": [
[
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/__snapshots__/on-update-action.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`On update action > success > update location > update domain by domain id 1`] = `
exports[`Actions - onUpdateAction > success > update location > update domain by domain id 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
Expand Down Expand Up @@ -138,7 +138,7 @@ CMD ["nginx", "-g", "daemon off;"]",
}
`;

exports[`On update action > success > update location > update domain by domain key 1`] = `
exports[`Actions - onUpdateAction > success > update location > update domain by domain key 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
Expand Down Expand Up @@ -276,7 +276,7 @@ CMD ["nginx", "-g", "daemon off;"]",
}
`;

exports[`On update action > success > update location and port > update domain by domain id 1`] = `
exports[`Actions - onUpdateAction > success > update location and port > update domain by domain id 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
Expand Down Expand Up @@ -414,7 +414,7 @@ CMD ["nginx", "-g", "daemon off;"]",
}
`;

exports[`On update action > success > update port > update domain by domain id 1`] = `
exports[`Actions - onUpdateAction > success > update port > update domain by domain id 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
Expand Down Expand Up @@ -552,7 +552,7 @@ CMD ["nginx", "-g", "daemon off;"]",
}
`;

exports[`On update action > success > update port > update domain by domain key 1`] = `
exports[`Actions - onUpdateAction > success > update port > update domain by domain key 1`] = `
[MockFunction writeFileSync] {
"calls": [
[
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const { pathname: DIRNAME } = new URL(import.meta.url);
export const HOSTS_START = "#--------------- LOCAL SSL ---------------#";
export const HOSTS_END = "#--------------- LOCAL SSL ---------------#";
14 changes: 10 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { Command } from "commander";
import fs from "fs";
import path from "path";

import onCreateAction from "./on-create-action";
import onListAction from "./on-list-action";
import onRemoveAction from "./on-remove-action";
import onUpdateAction from "./on-update-action";
import onCreateAction from "@/on-create-action";
import onListAction from "@/on-list-action";
import onRemoveAction from "@/on-remove-action";
import onResetHosts from "@/on-reset-hosts";
import onUpdateAction from "@/on-update-action";

const createBaseFolders = () => {
const distPath = path.resolve(__dirname, "./");
Expand Down Expand Up @@ -68,6 +69,11 @@ const cli = () => {
)
.action(onRemoveAction);

program
.command("reset")
.description("Remove all domain in `/etc/hosts` created by this cli")
.action(onResetHosts);

program.parse();
};

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/list-container.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import consola from "consola";
import shell from "shelljs";

import listContainer from "./list-container";
import listContainer from "@/list-container";

describe("List container", () => {
describe("Actions - listContainer", () => {
describe("success", () => {
test("commmand found local-ssl-management container running", async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
16 changes: 8 additions & 8 deletions packages/cli/src/on-create-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import consola from "consola";
import fs from "fs";
import shell from "shelljs";

import onCreateAction from "./on-create-action";
import onCreateAction from "@/on-create-action";

vi.mock("./list-container");
vi.mock("./utils/domain-exists-in-hosts", () => ({
vi.mock("@/list-container");
vi.mock("@/utils/domain-exists-in-hosts", () => ({
domainExistsInHosts: () => false,
}));

describe("On create action", () => {
describe("Actions - onCreateAction", () => {
beforeEach(() => {
vi.spyOn(fs, "readFileSync").mockReturnValue("[]");
vi.spyOn(fs, "existsSync").mockReturnValueOnce(true);
Expand Down Expand Up @@ -146,7 +146,7 @@ describe("On create action", () => {
expect(shell.exec).toBeCalledTimes(2);
expect(shell.exec).nthCalledWith(
1,
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /root/path/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
{ silent: true },
);
expect(shell.exec).nthCalledWith(
Expand Down Expand Up @@ -204,7 +204,7 @@ describe("On create action", () => {
expect(shell.exec).toBeCalledTimes(2);
expect(shell.exec).nthCalledWith(
1,
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /root/path/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
{ silent: true },
);
expect(shell.exec).nthCalledWith(
Expand Down Expand Up @@ -313,7 +313,7 @@ describe("On create action", () => {
expect(shell.exec).toBeCalledTimes(2);
expect(shell.exec).nthCalledWith(
1,
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /root/path/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
{ silent: true },
);
expect(shell.exec).nthCalledWith(
Expand Down Expand Up @@ -374,7 +374,7 @@ describe("On create action", () => {
expect(shell.exec).toBeCalledTimes(2);
expect(shell.exec).nthCalledWith(
1,
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /Users/dimaslz/Development/local-ssl-management/packages/cli/src/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
"NAME=local-ssl-management && docker rm -f $NAME && docker rmi -f $NAME && docker build --no-cache -t $NAME /root/path/.local-ssl-management && docker run --name $NAME -p 80:80 -p 443:443 -d $NAME",
{ silent: true },
);
expect(shell.exec).nthCalledWith(
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/on-create-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import crypto from "crypto";
import fs from "fs";
import path from "path";

import generateProxyImage from "./generate-proxy-image";
import { generateProxyImage, updateHosts } from "@/utils";

import {
domainExistsInHosts,
validateDomain,
validateLocation,
validatePort,
} from "./utils";
import { updateHosts } from "./utils/hosts";

const distPath = path.resolve(__dirname, "./");
const rootPath = `${distPath}/.local-ssl-management`;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/on-list-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import consola from "consola";
import fs from "fs";
import shell from "shelljs";

import onListAction from "./on-list-action";
import onListAction from "@/on-list-action";

describe("On list action", () => {
describe("Actions - onListAction", () => {
test("no domains available", () => {
vi.spyOn(fs, "readFileSync").mockReturnValue("[]");

Expand Down
19 changes: 7 additions & 12 deletions packages/cli/src/on-remove-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ import consola from "consola";
import fs from "fs";
import shell from "shelljs";

import generateProxyImage from "./generate-proxy-image";
import onRemoveAction from "./on-remove-action";

vi.mock("./list-container");
vi.mock("./generate-proxy-image");
vi.mock("path", () => ({
default: {
resolve: () => "/root/path",
},
}));

describe("On remove action", () => {
import onRemoveAction from "@/on-remove-action";
import { generateProxyImage } from "@/utils/generate-proxy-image";

vi.mock("@/list-container");
vi.mock("@/utils/generate-proxy-image");

describe("Actions - onRemoveAction", () => {
describe("failure", () => {
test("removing domain by name does not exists", () => {
vi.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify([]));
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/on-remove-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import consola from "consola";
import fs from "fs";
import path from "path";

import generateProxyImage from "./generate-proxy-image";
import { generateProxyImage } from "@/utils";

const distPath = path.resolve(__dirname, "./");
const rootPath = `${distPath}/.local-ssl-management`;
const configPath = `${rootPath}/config.json`;
Expand Down
Loading

0 comments on commit 3027138

Please sign in to comment.