From 8ad6b201dafc9ae51f8075cd917d59c4d2a6c7be Mon Sep 17 00:00:00 2001 From: Jordan Roher Date: Mon, 18 Dec 2023 12:47:46 -0800 Subject: [PATCH] Docker build works --- .gitignore | 3 ++- default.conf | 2 +- dockerfile | 2 +- src/css-cache-break.ts | 17 ++++++++++------- src/shared/files.ts | 27 +++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index b65edcc..dafe049 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ dist src/*.js src/**/*.js public/index.html -public/main.*.css \ No newline at end of file +public/main.css +public/css diff --git a/default.conf b/default.conf index 3f65062..c723440 100644 --- a/default.conf +++ b/default.conf @@ -1,7 +1,7 @@ server { listen 4173; - root /app; + root /app/public; location /app { try_files $uri $uri/ =404; diff --git a/dockerfile b/dockerfile index 88d6e52..855915c 100644 --- a/dockerfile +++ b/dockerfile @@ -41,4 +41,4 @@ EXPOSE 4173 RUN chmod +x /app/docker-entrypoint.sh ENTRYPOINT ["/app/docker-entrypoint.sh"] -CMD ["npm", "run", "build"] \ No newline at end of file +CMD ["sh", "-c", "npm run build && nginx -g 'daemon off;'"] \ No newline at end of file diff --git a/src/css-cache-break.ts b/src/css-cache-break.ts index ba16e0d..d6624b3 100644 --- a/src/css-cache-break.ts +++ b/src/css-cache-break.ts @@ -1,21 +1,24 @@ import CleanCSS from "clean-css"; import * as path from "path"; -import { sbReadFile, sbRename, sbWriteFile } from "./shared/files"; +import { sbMakeFolder, sbReadFile, sbRemoveAllCSS, sbWriteFile } from "./shared/files"; const mainCSSFilename = `main.${new Date().getTime()}.css`; -const cssFileInPath = path.join(__dirname, "../", "./public", "main.css"); -const cssFileOutPath = path.join(__dirname, "../", "./public", mainCSSFilename); -const indexFileInOutPath = path.join(__dirname, "../", "./public", "index.html"); +const cssFilePath = path.join(__dirname, "../public/css"); +const cssFileInPath = path.join(__dirname, "../public", "main.css"); +const cssFileOutPath = path.join(cssFilePath, mainCSSFilename); +const indexFileInOutPath = path.join(__dirname, "../public", "index.html"); async function start(): Promise { - await sbRename(cssFileInPath, cssFileOutPath); - const css = await sbReadFile(cssFileOutPath); + await sbRemoveAllCSS(cssFilePath); + await sbMakeFolder(cssFilePath); + + const css = await sbReadFile(cssFileInPath); await sbWriteFile(cssFileOutPath, new CleanCSS().minify(css).styles); const index = await sbReadFile(indexFileInOutPath); const cssLink = ``; - const cssLinkReplacement = ``; + const cssLinkReplacement = ``; await sbWriteFile(indexFileInOutPath, index.replace(cssLink, cssLinkReplacement)); } diff --git a/src/shared/files.ts b/src/shared/files.ts index 99b868d..76b32e2 100644 --- a/src/shared/files.ts +++ b/src/shared/files.ts @@ -36,3 +36,30 @@ export async function sbRename(fileNameOld: string, fileNameNew: string): Promis } }); } + +export async function sbMakeFolder(path: string): Promise { + return new Promise(async (resolve, reject) => { + try { + await fs.mkdir(path, { recursive: true }); + return resolve(true); + } catch (exception) { + console.error(`Could not make folder: ${path}`, exception); + reject(exception); + } + }); +} + +export async function sbRemoveAllCSS(path: string): Promise { + return new Promise(async (resolve, reject) => { + try { + await fs.rm(path, { + recursive: true, + force: true, + }); + return resolve(true); + } catch (exception) { + console.error(`Could not remove all CSS from path: ${path}`); + reject(exception); + } + }); +}