From 0a4aa0a737a5a3340f3c80e3cacbb019a6836e4e Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 2 May 2024 10:39:03 +0200 Subject: [PATCH 1/3] Add configuration mode to front scripts cli --- README.md | 6 +++ apps/front/package.json | 13 ++--- cli/config.js | 3 +- cli/tasks/setup/modules/setup-package-json.js | 54 ++++++++++++++++++- cli/tasks/setup/setup.js | 10 +++- 5 files changed, 74 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 736c154..361a277 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,12 @@ npm i ``` - Setup the project: + By default, SSG mode is enabled, you can switch to SPA or SSR mode by setting `MODE` to `SPA` or `SSR` in the `cli/config.js` file. +### Development + +```shell +mode="SSR" # or "SPA" +``` ```shell npm run setup diff --git a/apps/front/package.json b/apps/front/package.json index 226cbbf..3e24007 100644 --- a/apps/front/package.json +++ b/apps/front/package.json @@ -5,19 +5,14 @@ "type": "module", "scripts": { "dev": "node server.dev.js", - "build:spa": "cross-env VITE_SPA=true vite build --outDir dist/spa", - "build:ssr-scripts": "vite build -c vite.ssr-scripts.config.ts", - "build:ssr-client": "vite build --outDir dist/ssr/client", - "build:ssr-server": "vite build --ssr src/index-server.tsx --outDir dist/ssr/server", - "build:ssr": "npm run build:ssr-scripts && npm run build:ssr-client && npm run build:ssr-server", + "start": "cross-env NODE_ENV=production node dist/ssr/scripts/server.prod.js", + "test:watch": "vitest", + "test": "vitest run", "build:static-scripts": "vite build -c vite.static-scripts.config.ts", "build:static-client": "vite build --outDir dist/static/client", "build:static": "npm run build:static-scripts && npm run build:static-client && npm run generate", "generate": "node dist/static/scripts/exe-prerender.js", - "build": "npm run build:spa && npm run build:ssr && npm run build:static", - "start": "cross-env NODE_ENV=production node dist/ssr/scripts/server.prod.js", - "test:watch": "vitest", - "test": "vitest run" + "build": "npm run build:static" }, "dependencies": { "@cher-ami/css-flat": "^1.0.1", diff --git a/cli/config.js b/cli/config.js index ad678eb..73670f0 100644 --- a/cli/config.js +++ b/cli/config.js @@ -14,5 +14,6 @@ export default { // setup taskSetupFolder: resolve("cli/tasks/setup"), installFile: resolve("cli/install"), - setupFakeMode: false + setupFakeMode: false, + mode: "SSG" } diff --git a/cli/tasks/setup/modules/setup-package-json.js b/cli/tasks/setup/modules/setup-package-json.js index b3e7c1e..739abda 100644 --- a/cli/tasks/setup/modules/setup-package-json.js +++ b/cli/tasks/setup/modules/setup-package-json.js @@ -9,7 +9,7 @@ const log = debug("config:manage-package-json") /** * Setup package.json */ -export default async ({ packageJson, defaultProjectName, fakeMode } = {}) => { +export default async ({ packageJson, defaultProjectName, fakeMode, mode } = {}) => { return new Promise(async (resolve) => { logs.start("Setup package.json") @@ -94,3 +94,55 @@ export default async ({ packageJson, defaultProjectName, fakeMode } = {}) => { resolve({ projectName, projectAuthor, projectDescription }) }) } + +export const setupScriptsFront = async ({ frontPackageJson, mode, fakeMode }) => { + return new Promise(async (resolve) => { + const scripts = frontPackageJson.scripts || {} + + logs.start("Setup front package.json scripts") + switch (mode) { + case "SSG": + scripts["build:static-scripts"] = "vite build -c vite.static-scripts.config.ts" + scripts["build:static-client"] = "vite build --outDir dist/static/client" + scripts["build:static"] = + "npm run build:static-scripts && npm run build:static-client && npm run generate" + scripts["generate"] = "node dist/static/scripts/exe-prerender.js" + scripts["build"] = "npm run build:static" + break + case "SSR": + { + ;(scripts["build:ssr-scripts"] = "vite build -c vite.ssr-scripts.config.ts"), + (scripts["build:ssr-client"] = "vite build --outDir dist/ssr/client"), + (scripts["build:ssr-server"] = + "vite build --ssr src/index-server.tsx --outDir dist/ssr/server"), + (scripts["build:ssr"] = + "npm run build:ssr-scripts && npm run build:ssr-client && npm run build:ssr-server") + scripts["build"] = "npm run build:ssr" + } + break + case "SPA": + scripts["build:spa"] = "cross-env VITE_SPA=true vite build --outDir dist/spa" + scripts["build"] = "npm run build:spa" + break + default: + scripts.dev = "npm run dev:front" + scripts.build = "npm run build:front" + scripts.start = "npm run start:front" + } + + if (!fakeMode) { + log("Modify front package.json") + await mfs.createFile( + path.resolve("./apps/front/package.json"), + JSON.stringify(frontPackageJson, null, 2) + ) + } else { + log("FakeMode is activated, do nothing.") + } + + logs.done(`Front package.json scripts are setup with mode ${mode}`) + + frontPackageJson.scripts = scripts + resolve() + }) +} diff --git a/cli/tasks/setup/setup.js b/cli/tasks/setup/setup.js index 5b92bcf..19277dd 100644 --- a/cli/tasks/setup/setup.js +++ b/cli/tasks/setup/setup.js @@ -1,6 +1,6 @@ import checkInstallFile from "./modules/check-install-file.js" import setupReadme from "./modules/setup-readme.js" -import setupPackageJson from "./modules/setup-package-json.js" +import setupPackageJson, { setupScriptsFront } from "./modules/setup-package-json.js" import resetGit from "./modules/reset-git.js" import createInstallFile from "./modules/create-install-file.js" import logs from "../../helpers/logger.js" @@ -9,6 +9,7 @@ import path from "path" // TODO assert { type: "json" } will change in the future import packageJson from "../../../package.json" assert { type: "json" } +import frontPackageJson from "../../../apps/front/package.json" assert { type: "json" } import debug from "@cher-ami/debug" const log = debug(`config:setup`) @@ -21,6 +22,7 @@ const setup = () => if (config.setupFakeMode) { logs.start("\n ⚠️ Fake mode is active, nothing action will be process.") } + // check if cache file exist, if exist, do not continue if (await checkInstallFile(config.installFile)) return @@ -31,6 +33,12 @@ const setup = () => fakeMode: config.setupFakeMode }) + await setupScriptsFront({ + frontPackageJson, + mode: config.mode, + fakeMode: config.setupFakeMode + }) + // setup readme await setupReadme({ templatesPath: `${config.taskSetupFolder}/templates`, From b9b0d6bb614101f21fc63f80e6bde96cd0f46756 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 2 May 2024 10:46:46 +0200 Subject: [PATCH 2/3] Modify conf --- apps/front/package.json | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/apps/front/package.json b/apps/front/package.json index 3e24007..a636f59 100644 --- a/apps/front/package.json +++ b/apps/front/package.json @@ -7,12 +7,7 @@ "dev": "node server.dev.js", "start": "cross-env NODE_ENV=production node dist/ssr/scripts/server.prod.js", "test:watch": "vitest", - "test": "vitest run", - "build:static-scripts": "vite build -c vite.static-scripts.config.ts", - "build:static-client": "vite build --outDir dist/static/client", - "build:static": "npm run build:static-scripts && npm run build:static-client && npm run generate", - "generate": "node dist/static/scripts/exe-prerender.js", - "build": "npm run build:static" + "test": "vitest run" }, "dependencies": { "@cher-ami/css-flat": "^1.0.1", From 2fbf2dd889de2f7b52e6c72f5d4ad951592d951e Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 2 May 2024 10:50:27 +0200 Subject: [PATCH 3/3] Update front package json --- apps/front/package.json | 12 +++++++++- cli/tasks/setup/modules/setup-package-json.js | 24 ++++--------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/apps/front/package.json b/apps/front/package.json index a636f59..802af0c 100644 --- a/apps/front/package.json +++ b/apps/front/package.json @@ -7,7 +7,17 @@ "dev": "node server.dev.js", "start": "cross-env NODE_ENV=production node dist/ssr/scripts/server.prod.js", "test:watch": "vitest", - "test": "vitest run" + "test": "vitest run", + "build:spa": "cross-env VITE_SPA=true vite build --outDir dist/spa", + "build:ssr-scripts": "vite build -c vite.ssr-scripts.config.ts", + "build:ssr-client": "vite build --outDir dist/ssr/client", + "build:ssr-server": "vite build --ssr src/index-server.tsx --outDir dist/ssr/server", + "build:ssr": "npm run build:ssr-scripts && npm run build:ssr-client && npm run build:ssr-server", + "build:static-scripts": "vite build -c vite.static-scripts.config.ts", + "build:static-client": "vite build --outDir dist/static/client", + "build:static": "npm run build:static-scripts && npm run build:static-client && npm run generate", + "generate": "node dist/static/scripts/exe-prerender.js", + "build": "npm run build:spa && npm run build:ssr && npm run build:static" }, "dependencies": { "@cher-ami/css-flat": "^1.0.1", diff --git a/cli/tasks/setup/modules/setup-package-json.js b/cli/tasks/setup/modules/setup-package-json.js index 739abda..c3cc4fc 100644 --- a/cli/tasks/setup/modules/setup-package-json.js +++ b/cli/tasks/setup/modules/setup-package-json.js @@ -102,32 +102,16 @@ export const setupScriptsFront = async ({ frontPackageJson, mode, fakeMode }) => logs.start("Setup front package.json scripts") switch (mode) { case "SSG": - scripts["build:static-scripts"] = "vite build -c vite.static-scripts.config.ts" - scripts["build:static-client"] = "vite build --outDir dist/static/client" - scripts["build:static"] = - "npm run build:static-scripts && npm run build:static-client && npm run generate" - scripts["generate"] = "node dist/static/scripts/exe-prerender.js" - scripts["build"] = "npm run build:static" + scripts.build = "npm run build:static" break case "SSR": - { - ;(scripts["build:ssr-scripts"] = "vite build -c vite.ssr-scripts.config.ts"), - (scripts["build:ssr-client"] = "vite build --outDir dist/ssr/client"), - (scripts["build:ssr-server"] = - "vite build --ssr src/index-server.tsx --outDir dist/ssr/server"), - (scripts["build:ssr"] = - "npm run build:ssr-scripts && npm run build:ssr-client && npm run build:ssr-server") - scripts["build"] = "npm run build:ssr" - } + scripts.build = "npm run build:ssr" break case "SPA": - scripts["build:spa"] = "cross-env VITE_SPA=true vite build --outDir dist/spa" - scripts["build"] = "npm run build:spa" + scripts.build = "npm run build:spa" break default: - scripts.dev = "npm run dev:front" - scripts.build = "npm run build:front" - scripts.start = "npm run start:front" + scripts.build = "npm run build:spa && npm run build:ssr && npm run build:static" } if (!fakeMode) {