Changed initial files creation process

This commit is contained in:
unknown 2021-10-06 14:17:31 +02:00
parent 591824dd0c
commit 459523dfd2
9 changed files with 109 additions and 38 deletions

View file

@ -5,10 +5,11 @@
"main": "index.js",
"scripts": {
"start": "node server.js",
"init-server": "echo Instaling server dependencies && npm install && mkdir public && touch public/flame.css",
"init-server": "echo Instaling server dependencies && npm install",
"init-client": "cd client && echo Instaling client dependencies && npm install",
"dev-init": "npm run init-server && npm run init-client",
"dev-server": "nodemon server.js",
"dir-init": "npx mkdirp data public && touch public/flame.css public/customQueries.json",
"dev-init": "npm run dir-init && npm run init-server && npm run init-client",
"dev-server": "nodemon server.js -e js",
"dev-client": "npm start --prefix client",
"dev": "concurrently \"npm run dev-server\" \"npm run dev-client\"",
"skaffold": "concurrently \"npm run init-client\" \"npm run dev-server\""

View file

@ -1,23 +1,28 @@
require('dotenv').config();
const http = require('http');
// Database
const { connectDB } = require('./db');
const associateModels = require('./models/associateModels');
// Server
const api = require('./api');
const jobs = require('./utils/jobs');
const Socket = require('./Socket');
const Sockets = require('./Sockets');
const associateModels = require('./models/associateModels');
const initConfig = require('./utils/initConfig');
const findCss = require('./utils/findCss');
// Utils
const initApp = require('./utils/init');
const Logger = require('./utils/Logger');
const logger = new Logger();
const PORT = process.env.PORT || 5005;
(async () => {
const PORT = process.env.PORT || 5005;
// Init app
await connectDB();
await associateModels();
await initConfig();
findCss();
await initApp();
// Create server for Express API and WebSockets
const server = http.createServer();
@ -28,6 +33,8 @@ const PORT = process.env.PORT || 5005;
Sockets.registerSocket('weather', weatherSocket);
server.listen(PORT, () => {
logger.log(`Server is running on port ${PORT} in ${process.env.NODE_ENV} mode`);
})
})();
logger.log(
`Server is running on port ${PORT} in ${process.env.NODE_ENV} mode`
);
});
})();

View file

@ -1,22 +0,0 @@
const fs = require('fs');
const { join } = require('path');
const Logger = require('./Logger');
const logger = new Logger();
// Check if flame.css exists in mounted docker volume. Create new file if not
const findCss = () => {
const srcPath = join(__dirname, '../data/flame.css');
const destPath = join(__dirname, '../public/flame.css');
if (fs.existsSync(srcPath)) {
fs.copyFileSync(srcPath, destPath);
logger.log('Custom CSS file found');
return;
}
logger.log('Creating empty CSS file');
fs.writeFileSync(destPath, '');
}
module.exports = findCss;

32
utils/init/createFile.js Normal file
View file

@ -0,0 +1,32 @@
const fs = require('fs');
const { join } = require('path');
const Logger = require('../Logger');
const logger = new Logger();
const createFile = async (file) => {
const { name, msg, template, isJSON, paths } = file;
const srcPath = join(__dirname, paths.src, name);
const destPath = join(__dirname, paths.dest, name);
// Check if file exists
if (fs.existsSync(srcPath)) {
fs.copyFileSync(srcPath, destPath);
if (process.env.NODE_ENV == 'development') {
logger.log(msg.found);
}
return;
}
// Create file if not
fs.writeFileSync(destPath, isJSON ? JSON.stringify(template) : template);
if (process.env.NODE_ENV == 'development') {
logger.log(msg.created);
}
};
module.exports = createFile;

9
utils/init/index.js Normal file
View file

@ -0,0 +1,9 @@
const initConfig = require('./initConfig');
const initFiles = require('./initFiles');
const initApp = async () => {
await initConfig();
await initFiles();
};
module.exports = initApp;

View file

@ -1,7 +1,8 @@
const { Op } = require('sequelize');
const Config = require('../models/Config');
const Config = require('../../models/Config');
const { config } = require('./initialConfig.json');
const Logger = require('./Logger');
const Logger = require('../Logger');
const logger = new Logger();
const initConfig = async () => {
@ -28,7 +29,10 @@ const initConfig = async () => {
}
});
logger.log('Initial config created');
if (process.env.NODE_ENV == 'development') {
logger.log('Initial config created');
}
return;
};

8
utils/init/initFiles.js Normal file
View file

@ -0,0 +1,8 @@
const createFile = require('./createFile');
const { files } = require('./initialFiles.json');
const initFiles = async () => {
files.forEach(async (file) => await createFile(file));
};
module.exports = initFiles;

View file

@ -0,0 +1,32 @@
{
"files": [
{
"name": "flame.css",
"msg": {
"created": "Created empty CSS file",
"found": "Custom CSS file found"
},
"paths": {
"src": "../../data",
"dest": "../../public"
},
"template": "",
"isJSON": false
},
{
"name": "customQueries.json",
"msg": {
"created": "Created empty queries file",
"found": "Custom queries file found"
},
"paths": {
"src": "../../data",
"dest": "../../data"
},
"template": {
"queries": []
},
"isJSON": true
}
]
}