Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cli setup #186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions apps/front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"type": "module",
"scripts": {
"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: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",
Expand All @@ -14,10 +17,7 @@
"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:spa && npm run build:ssr && 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"
}
38 changes: 37 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,39 @@ 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 = "npm run build:static"
break
case "SSR":
scripts.build = "npm run build:ssr"
break
case "SPA":
scripts.build = "npm run build:spa"
break
default:
scripts.build = "npm run build:spa && npm run build:ssr && npm run build:static"
}

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
Loading