Skip to content

Commit

Permalink
🚨 [feat/config] #83 apply linter recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
JAGFx committed Dec 19, 2021
1 parent 7dfa23b commit 5ec9e77
Show file tree
Hide file tree
Showing 93 changed files with 4,898 additions and 5,194 deletions.
22 changes: 13 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
module.exports = {
env: {
env: {
node: true
},
extends: [
extends: [
'eslint:recommended',
'plugin:vue/essential',
"plugin:vue/strongly-recommended",
"plugin:vue/recommended",
'plugin:vue/strongly-recommended',
'plugin:vue/recommended',
'prettier'
],
rules: {
'no-console': 2,
'no-debugger': 2,
'no-mixed-spaces-and-tabs': [ 2, 'smart-tabs' ],
'vue/no-v-html': 'off'
plugins: [ 'prettier' ],
rules: {
'no-console': 1,
'no-debugger': 2,
'no-mixed-spaces-and-tabs': [ 2, 'smart-tabs' ],
'vue/no-v-html': 'off',
'vue/multi-word-component-names': 'off',
'prettier/prettier': 2
}
};
2 changes: 1 addition & 1 deletion .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
run: npm i

- name: Run linter
run: npm run lint:no-fix
run: npm run ci
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 80
}
56 changes: 35 additions & 21 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,49 @@
*/

import { existsSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';

const oldConfigPath = (basePath = null) => path.resolve( basePath ?? process.cwd(), './config/config.ets2-dashboard-skin.json' );
import path from 'path';

const oldConfigPath = (basePath = null) =>
path.resolve(
basePath ?? process.cwd(),
'./config/config.ets2-dashboard-skin.json'
);

export const appConfigPath = (basePath = null) => {
const newAppConfigPath = path.resolve( basePath ?? process.cwd(), `./config/config.json` );

return ( existsSync( newAppConfigPath ) )
? newAppConfigPath
: oldConfigPath(basePath)
}
const newAppConfigPath = path.resolve(
basePath ?? process.cwd(),
`./config/config.json`
);

return existsSync(newAppConfigPath)
? newAppConfigPath
: oldConfigPath(basePath);
};

export const gameConfigPath = (target, basePath = null) => {
const newGameConfigPath = path.resolve( basePath ?? process.cwd(), `./config/config.${ target }.json` );

return ( existsSync( newGameConfigPath ) )
? newGameConfigPath
: oldConfigPath(basePath)
}
const newGameConfigPath = path.resolve(
basePath ?? process.cwd(),
`./config/config.${target}.json`
);

return existsSync(newGameConfigPath)
? newGameConfigPath
: oldConfigPath(basePath);
};

export const appConfig = (basePath = null) => readFileSync( appConfigPath(basePath), 'UTF-8' );
export const appConfig = (basePath = null) =>
readFileSync(appConfigPath(basePath), 'UTF-8');

export const gameConfig = (target, basePath = null) => readFileSync( gameConfigPath(target, basePath), 'UTF-8' );
export const gameConfig = (target, basePath = null) =>
readFileSync(gameConfigPath(target, basePath), 'UTF-8');

export const saveAppConfig = (data, basePath = null) => {
writeFileSync( appConfigPath(basePath), JSON.stringify( data, null, 2 ) );
}
writeFileSync(appConfigPath(basePath), JSON.stringify(data, null, 2));
};

export const saveGameConfig = (target, data, basePath = null) => {
writeFileSync( gameConfigPath(target, basePath), JSON.stringify( data, null, 2 ) );
}
writeFileSync(
gameConfigPath(target, basePath),
JSON.stringify(data, null, 2)
);
};
217 changes: 113 additions & 104 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,124 @@
* Time: 20:39
*/

import bodyParser from 'body-parser';
import express from 'express';
import fs from 'fs';
import path from 'path';
import cors from 'cors';
import { gameConfig, saveAppConfig, saveGameConfig } from './config.js';
import store, { app, initAppConfig, isDevelopment, server, socket } from './store.js';
import { telemetryInterval } from './telemetry.js';
import { logIt } from './utils.js';


const postConfigRequest = ( req, res, basePath = null ) => {
saveAppConfig( req.body.app, basePath );
saveGameConfig( req.params.target, req.body.game, basePath );

store.set( 'config', {
app: req.body.app ,
game: req.body.game
} )
res.send( req.body );
}

const getConfigRequest = ( req, res ) => {
res.send( store.get('config') );
}
import bodyParser from 'body-parser';
import express from 'express';
import fs from 'fs';
import path from 'path';
import cors from 'cors';
import { gameConfig, saveAppConfig, saveGameConfig } from './config.js';
import store, {
app,
initAppConfig,
isDevelopment,
server,
socket
} from './store.js';
import { telemetryInterval } from './telemetry.js';
import { logIt } from './utils.js';

const postConfigRequest = (req, res, basePath = null) => {
saveAppConfig(req.body.app, basePath);
saveGameConfig(req.params.target, req.body.game, basePath);

store.set('config', {
app: req.body.app,
game: req.body.game
});
res.send(req.body);
};

const getConfigRequest = (req, res) => {
res.send(store.get('config'));
};

export const initApp = () => {
return new Promise( resolve => {
app.use( bodyParser.json() );
app.use(cors())

resolve()
} );
}

export const initConfig = ( basePath = null) => {
return new Promise(resolve => {
initAppConfig( basePath )
.then( () => {
app.post( '/config/:target', ( req, res) => {
postConfigRequest( req, res, basePath )
} );
app.get( '/config', getConfigRequest);
const config = store.get('config.app');
const port = (config.hasOwnProperty('general_port'))
? parseInt(config.general_port)
: 3000;

store.set('port', port);

resolve()
} )
})
}
return new Promise((resolve) => {
app.use(bodyParser.json());
app.use(cors());

resolve();
});
};

export const initConfig = (basePath = null) => {
return new Promise((resolve) => {
initAppConfig(basePath).then(() => {
app.post('/config/:target', (req, res) => {
postConfigRequest(req, res, basePath);
});
app.get('/config', getConfigRequest);
const config = store.get('config.app');
const port = Object.hasOwnProperty.call(config, 'general_port')
? parseInt(config.general_port)
: 3000;

store.set('port', port);

resolve();
});
});
};

export const initMap = (basePath = null) => {
return new Promise( (resolve) => {
const pathMap = path.resolve( basePath, './maps' );
if ( fs.existsSync( pathMap ) )
app.use( '/maps', express.static( pathMap ) );

resolve()
} )
}
return new Promise((resolve) => {
const pathMap = path.resolve(basePath, './maps');
if (fs.existsSync(pathMap)) app.use('/maps', express.static(pathMap));

resolve();
});
};

export const initSocket = () => {
return new Promise(resolve => {
socket.on( 'connection', socket => {
console.log( 'Connection: ' + socket.id );

if( isDevelopment() ){
const dataFileName = path.resolve( process.cwd(), '../../src/data/scs_sdk_plugin_parsed_data.json' );
const data = JSON.parse(fs.readFileSync( dataFileName ).toString());
let lastGameName = null;

setInterval( () => {
socket.emit( 'update', data)
const currentGameName = data.game.game.name;

if( currentGameName !== lastGameName ){
const config = JSON.parse( gameConfig( currentGameName ) );
store.set( 'config.game', config);
}

lastGameName = currentGameName;
//}, 1000 );
}, telemetryInterval() );
}

} );
resolve()
})
}
return new Promise((resolve) => {
socket.on('connection', (socket) => {
console.log('Connection: ' + socket.id);

if (isDevelopment()) {
const dataFileName = path.resolve(
process.cwd(),
'../../src/data/scs_sdk_plugin_parsed_data.json'
);
const data = JSON.parse(fs.readFileSync(dataFileName).toString());
let lastGameName = null;

setInterval(() => {
socket.emit('update', data);
const currentGameName = data.game.game.name;

if (currentGameName !== lastGameName) {
const config = JSON.parse(gameConfig(currentGameName));
store.set('config.game', config);
}

lastGameName = currentGameName;
//}, 1000 );
}, telemetryInterval());
}
});
resolve();
});
};

export const initServer = () => {
return new Promise(resolve => {
const port = store.get('port')

server.listen( port, () => {
const url = `localhost:${ port }`;
const data = {
url: url,
port: port
};

logIt( 'server.listen', data, `Euro Truck Simulator 2 dashboard is running at http://${ url }/` );
resolve()
} );
})
}

export default {};
return new Promise((resolve) => {
const port = store.get('port');

server.listen(port, () => {
const url = `localhost:${port}`;
const data = {
url: url,
port: port
};

logIt(
'server.listen',
data,
`Euro Truck Simulator 2 dashboard is running at http://${url}/`
);
resolve();
});
});
};

export default {};
Loading

0 comments on commit 5ec9e77

Please sign in to comment.