Skip to content

Commit

Permalink
Add configuration mode to front scripts cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo committed May 2, 2024
1 parent 1bf9cb8 commit 0a4aa0a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions apps/front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion cli/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export default {
// setup
taskSetupFolder: resolve("cli/tasks/setup"),
installFile: resolve("cli/install"),
setupFakeMode: false
setupFakeMode: false,
mode: "SSG"
}
54 changes: 53 additions & 1 deletion cli/tasks/setup/modules/setup-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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()
})
}
10 changes: 9 additions & 1 deletion cli/tasks/setup/setup.js
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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`)
Expand All @@ -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

Expand All @@ -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`,
Expand Down

0 comments on commit 0a4aa0a

Please sign in to comment.