Skip to content

Commit

Permalink
feat: add production mode
Browse files Browse the repository at this point in the history
  • Loading branch information
robzarel committed Apr 26, 2023
1 parent 65788cf commit b4e0e1c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -19,7 +19,8 @@
"scripts": {
"start": "react-scripts start",
"serve": "json-server --watch ./src/server/db/index.js --routes ./src/server/routes.json --port 3001",
"build": "react-scripts build",
"build": "react-scripts build && npm run save-json-api",
"save-json-api": "node ./src/server/scripts/save-json-api.js",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Expand All @@ -42,6 +43,7 @@
]
},
"devDependencies": {
"json-server": "^0.17.3"
"json-server": "^0.17.3",
"node-fs": "^0.1.7"
}
}
6 changes: 5 additions & 1 deletion src/api/index.ts
Expand Up @@ -8,7 +8,11 @@ type RESPONSE_DATA = {
};

const getJson = async <T>(endpoint: ENDPOINTS): Promise<T> => {
const path = `http://localhost:3001/api/${endpoint}`;
const path =
process.env.NODE_ENV === 'development'
? `http://localhost:3001/api/${endpoint}`
: `https://raw.githubusercontent.com/robzarel/gh-pages-demo/gh-pages/static/db/${endpoint}.json`;

const response = await fetch(path);

return await response.json();
Expand Down
16 changes: 16 additions & 0 deletions src/server/scripts/save-json-api.js
@@ -0,0 +1,16 @@
const fs = require('node-fs');
const getDb = require('../db');

const db = getDb();

fs.mkdir('./build/static/db', () => {
for (let [key, value] of Object.entries(db)) {
fs.writeFile(
`./build/static/db/${key}.json`,
JSON.stringify(value),
(err) => {
if (err) throw err;
}
);
}
});

0 comments on commit b4e0e1c

Please sign in to comment.