Skip to content

Commit

Permalink
fix(cli): improve feedback error
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaslz committed Feb 3, 2024
1 parent f905796 commit 85773b0
Show file tree
Hide file tree
Showing 10 changed files with 2,054 additions and 255 deletions.
1,497 changes: 1,482 additions & 15 deletions package-lock.json

Large diffs are not rendered by default.

138 changes: 138 additions & 0 deletions packages/cli/src/__snapshots__/on-create-action.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,141 @@ CMD ["nginx", "-g", "daemon off;"]",
],
}
`;

exports[`Actions - onCreateAction > success > single service > domain config created sucessfully 2`] = `
[MockFunction writeFileSync] {
"calls": [
[
"/root/path/.local-ssl-management/nginx.conf",
"user nginx;
worker_processes 20;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
server {
listen 443 ssl;
server_name _;
ssl_certificate /etc/nginx/localhost-cert.pem;
ssl_certificate_key /etc/nginx/localhost-key.pem;
location / {
root /var/www/html;
}
}
server {
listen 80 default_server;
server_name _;
include /etc/nginx/mime.types;
default_type application/octet-stream;
location / {
root /var/www/html;
}
}
server {
listen 443 ssl;
autoindex off;
access_log /var/log/nginx/some-domain.com.access.log;
error_log /var/log/nginx/some-domain.com.error.log;
server_tokens off;
server_name some-domain.com;
ssl_certificate /etc/nginx/some-domain.com-cert.pem;
ssl_certificate_key /etc/nginx/some-domain.com-key.pem;
gzip_static on;
location ~ ^/(/?)(.*) {
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
proxy_pass http://11.22.33.445:3000/$2;
proxy_redirect off;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header 'Access-Control-Allow-Origin' '*';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off;
}
}
}",
],
[
"/root/path/.local-ssl-management/Dockerfile",
"FROM nginx
# RUN rm -f /etc/nginx/conf.d/default.conf
# WORKDIR /var/www/html
# COPY index.html /var/www/html
# RUN chmod 755 /var/www/html/index.html
COPY nginx.conf /etc/nginx/conf.d/
COPY ssl/some-domain.com-key.pem /etc/nginx/
COPY ssl/some-domain.com-cert.pem /etc/nginx/
COPY ssl/localhost-key.pem /etc/nginx/
COPY ssl/localhost-cert.pem /etc/nginx/
COPY nginx.conf /etc/nginx/
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]",
],
[
"/root/path/.local-ssl-management/config.json",
"[
{
"id": "48d1a85c-377a-40ef-8a82-d1405f7a074f",
"domain": "some-domain.com",
"services": [
{
"id": "da406b35-79b8-4909-9af1-07cfdcf37d00",
"location": "/",
"port": "3000"
}
]
}
]",
],
],
"results": [
{
"type": "return",
"value": undefined,
},
{
"type": "return",
"value": undefined,
},
{
"type": "return",
"value": undefined,
},
],
}
`;
48 changes: 36 additions & 12 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Command } from "commander";
import consola from "consola";
import fs from "fs";
import path from "path";
import shell from "shelljs";

import onCreateAction from "@/on-create-action";
import onListAction from "@/on-list-action";
Expand Down Expand Up @@ -40,16 +41,24 @@ const cli = () => {
const program = new Command();

program
.name("create")
.usage("<domain> [options]")
.command("create <domain>")
.description("Create domain")
.option("-p, --port <port>", "Port where is running the application")
.option(
"-l, --location <location>",
'Location where nginx will serve the application. By default is "/"',
)
.configureOutput({
writeErr: (str) => consola.error(str.replace("error: ", "")),
})
.addHelpText(
"after",
`
Example:
$ local-ssl create your-domain.com # location "/" and port "3000" by default
$ local-ssl create your-domain.com --location / # port "3000" by default
$ local-ssl create your-domain.com --location /app --port 4000`,
)
.action(onCreateAction);

program.command("list").description("List domains").action(onListAction);
Expand All @@ -62,9 +71,15 @@ const cli = () => {
"-l, --location <location>",
'Location where nginx will serve the application. By default is "/"',
)
.configureOutput({
writeErr: (str) => consola.error(str.replace("error: ", "")),
})
.addHelpText(
"after",
`
Example:
$ local-ssl update your-domain.com --location /,/app # "/" to "/app"
$ local-ssl update your-domain.com --location / --port 4000 # "3000" to "4000"
$ local-ssl update your-domain.com --location /,/app --port 4000 # "3000" to "4000", "/" to "/app"`,
)
.action(onUpdateAction);

program
Expand All @@ -74,19 +89,28 @@ const cli = () => {
"-l, --location <location>",
"Location where nginx will serve the application.",
)
.configureOutput({
writeErr: (str) => consola.error(str.replace("error: ", "")),
})
.addHelpText(
"after",
`
Example:
$ local-ssl remove your-domain.com|39edd1b4-ba9c-46fb-9929-cc3534bb6f3f # remove endpoint
$ local-ssl remove your-domain.com|39edd1b4-ba9c-46fb-9929-cc3534bb6f3f --location /app # remove location "/app"`,
)
.action(onRemoveAction);

program
.command("reset")
.description("Remove all domain in `/etc/hosts` created by this cli")
.configureOutput({
writeErr: (str) => consola.error(str.replace("error: ", "")),
})
.action(onResetHosts);

program.configureOutput({
writeErr: (str) => {
consola.error(str.replace("error: ", ""));
shell.exec("local-ssl --help").stdout;
},
});

program.parse();
};

Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/list-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ describe("Actions - listContainer", () => {
};
});

listContainer();

expect(consola.error).toBeCalledWith(
"Something have been failure. Contact with the author.",
);
expect(() => {
listContainer();
}).toThrowError("Something have been failure. Contact with the author.");

// read files
expect(fs.readFileSync).not.toBeCalled();
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/list-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const listContainer = () => {
.find((line) => /local-ssl-management/.test(line)) || "";

if (!containerData) {
consola.error("Something have been failure. Contact with the author.");

return;
throw new Error("Something have been failure. Contact with the author.");
}

const [containerId, containerName, ports] = containerData
Expand Down
Loading

0 comments on commit 85773b0

Please sign in to comment.