@bouygues-telecom/staticjs 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,8 +4,11 @@ import { readPages } from "./readPages.js";
4
4
 
5
5
  const pagesDir = path.resolve(process.cwd(), "src/pages");
6
6
  const args = process.argv.slice(2);
7
+ const cacheDir = path.resolve(process.cwd(), "cache");
8
+ const cacheFilePath = path.resolve(cacheDir, "pagesCache.json");
7
9
 
8
10
  let entries;
11
+
9
12
  if (args.length > 0) {
10
13
  entries = args
11
14
  .filter((arg) => arg.endsWith(".tsx"))
@@ -20,10 +23,6 @@ if (args.length > 0) {
20
23
  entries = readPages(pagesDir);
21
24
  }
22
25
 
23
- const cacheDir = path.resolve(process.cwd(), "cache");
24
- const cacheFilePath = path.resolve(cacheDir, "pagesCache.json");
25
-
26
26
  if (!fs.existsSync(cacheDir)) fs.mkdirSync(cacheDir, { recursive: true });
27
-
28
27
  fs.writeFileSync(cacheFilePath, JSON.stringify(entries, null, 2), "utf8");
29
28
  console.log("Pages cached successfully.");
@@ -14,7 +14,7 @@ export const createPage = ({
14
14
  pageName,
15
15
  JSfileName,
16
16
  }) => {
17
- const template = `<link rel='stylesheet' href='https://assets.bouyguestelecom.fr/TRILOGY/trilogy-styles@4/default/trilogy.css'/>
17
+ const template = `
18
18
  <div id=app-{{rootId}}>{{html}}
19
19
  ${data ? `<script id=initial-data-{{initialDatasId}} type="application/json">${JSON.stringify(data)}</script>` : ""}
20
20
  ${JSfileName ? `<script type="module" src="{{scriptPath}}"></script>` : ""}
package/package.json CHANGED
@@ -1,11 +1,22 @@
1
1
  {
2
2
  "name": "@bouygues-telecom/staticjs",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
+ "files": [
6
+ "config",
7
+ "helpers",
8
+ "scripts",
9
+ "package.json",
10
+ "README.md"
11
+ ],
5
12
  "bin": {
6
13
  "create-staticjs-app": "./scripts/create-static-app.js",
7
14
  "bt-staticjs": "./scripts/cli.js"
8
15
  },
16
+ "scripts": {
17
+ "build": "cd templates/react && rimraf dist && rimraf cache && node ../../helpers/cachePages.js && vite build --config ../../vite.config.js && tsx ../../scripts/build-html.js",
18
+ "start": "node templates/react/server.js"
19
+ },
9
20
  "dependencies": {
10
21
  "cli-spinner": "^0.2.10",
11
22
  "commander": "^14.0.0",
@@ -18,11 +29,7 @@
18
29
  "volta": {
19
30
  "node": "24.1.0"
20
31
  },
21
- "files": [
22
- "config",
23
- "helpers",
24
- "scripts",
25
- "package.json",
26
- "README.md"
32
+ "workspaces": [
33
+ "templates/react"
27
34
  ]
28
35
  }
@@ -1,41 +1,41 @@
1
- import express from 'express';
2
- import { exec } from 'child_process';
1
+ import { exec } from "child_process";
2
+ import { dirname } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import path from "path";
3
5
 
4
- const app = express();
5
- app.use(express.json());
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
6
7
 
7
- const PORT = process.env.PORT || 3000;
8
+ export const revalidate = (req, res) => {
9
+ try {
10
+ const paths = req?.body?.paths || [];
11
+ const pathsArg = paths.length > 0 ? paths.join(" ") : "";
12
+ const cachePages = path.resolve(__dirname, "../helpers/cachePages.js");
13
+ const buildHtmlConfig = path.resolve(__dirname, "./build-html.js");
8
14
 
9
- const rebuildApplication = (paths) => {
10
- console.log('Rebuilding application...');
11
-
12
- console.log(`Building paths: ${paths}`);
13
-
14
- const pathsArg = paths.length > 0 ? paths.join(' ') : '';
15
-
16
- const buildCommand = `NODE_TLS_REJECT_UNAUTHORIZED=0 node helpers/cachePages.js ${pathsArg && `-- ${pathsArg}`} && tsx build-html.js`;
15
+ const buildCommand = `NODE_TLS_REJECT_UNAUTHORIZED=0 node ${cachePages} ${
16
+ pathsArg && `${pathsArg}`
17
+ } && npx tsx ${buildHtmlConfig}`;
17
18
 
18
19
  exec(buildCommand, (error, stdout, stderr) => {
19
- if (error) {
20
- console.error(`Exec error: ${error}`);
21
- return;
22
- }
20
+ if (error) {
21
+ console.error(`Exec error: ${error}`);
22
+ return;
23
+ }
24
+ if (!error) {
23
25
  console.log(`stdout: ${stdout}`);
24
26
  console.error(`stderr: ${stderr}`);
27
+ }
25
28
  });
26
- };
27
-
28
- app.post('/revalidate', (req, res) => {
29
- try {
30
- const paths = req?.body?.paths || [];
31
- rebuildApplication(paths);
32
- res.status(200).send(`Revalidation triggered, paths: ${paths.length > 0 ? paths.join(', ') : 'all pages'} built!`);
33
- } catch (error) {
34
- console.error('Revalidation error:', error);
35
- res.status(500).send('Error during revalidation.');
36
- }
37
- });
38
29
 
39
- app.listen(PORT, () => {
40
- console.log(`Server running on port ${PORT}`);
41
- });
30
+ return res
31
+ .status(200)
32
+ .send(
33
+ `Revalidation triggered, paths: ${
34
+ paths.length > 0 ? paths.join(", ") : "all pages"
35
+ } built!`
36
+ );
37
+ } catch (error) {
38
+ console.error("Revalidation error:", error);
39
+ res.status(500).send("Error during revalidation.");
40
+ }
41
+ };