Created
July 3, 2021 12:40
-
-
Save carlos-menezes/16fd299a8ed31df2910e8a3c6451a460 to your computer and use it in GitHub Desktop.
alt:V packaging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path'; | |
import fs from 'fs'; | |
import { BUILD_DIR } from './pkg.js'; | |
const env = process.env.NODE_ENV ?? 'development'; | |
const serverConfig = path.resolve('src', `server-${env}.cfg`); | |
const serverConfigDestination = path.resolve(BUILD_DIR); | |
fs.copyFileSync(serverConfig, path.join(serverConfigDestination, `server.cfg`)); | |
const resourceConfig = path.resolve('src', 'resource.cfg'); | |
const resourceConfigDestination = path.resolve( | |
BUILD_DIR, | |
'resources', | |
'ultimate-roleplay' | |
); | |
fs.copyFileSync( | |
resourceConfig, | |
path.join(resourceConfigDestination, 'resource.cfg') | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import os from 'os'; | |
import https from 'https'; | |
import path, { join } from 'path'; | |
import { BRANCH, BUILD_DIR, CONFIG_KEYS, logger, config } from './pkg.js'; | |
import { exec } from 'child_process'; | |
const platform = os.platform(); | |
const url = `https://cdn.altv.mp/server/${BRANCH}/x64_${platform}/update.json`; | |
https.get(url, (r) => { | |
let body = ''; | |
r.on('data', (chunk) => { | |
body += chunk; | |
}); | |
r.on('end', () => { | |
body = JSON.parse(body); | |
if (config.get(CONFIG_KEYS.VERSION) == undefined) { | |
logger.log('info', 'VERSION key undefined in urp-game-pkg.json'); | |
config.set(CONFIG_KEYS.VERSION, body.version); | |
ensure(); | |
} else { | |
if (body.version != config.get(CONFIG_KEYS.VERSION)) { | |
logger.log('info', 'Found newer version of alt:V server files'); | |
config.set(CONFIG_KEYS.VERSION, body.version); | |
ensure(); | |
} else { | |
if ( | |
!fs.existsSync(BUILD_DIR) || | |
fs.readdirSync(BUILD_DIR).length == 0 | |
) { | |
ensure(); | |
} else { | |
logger.log( | |
'info', | |
'alt:V server files up-to-date, skipping ensuring' | |
); | |
process.exit(0); | |
} | |
} | |
} | |
}); | |
}); | |
const ensure = () => { | |
const files = [ | |
{ | |
url: `https://cdn.altv.mp/server/${BRANCH}/x64_${platform}/altv-server${ | |
platform == 'win32' ? '.exe' : '' | |
}`, | |
dest: '.', | |
}, | |
{ | |
url: `https://cdn.altv.mp/server/${BRANCH}/x64_${platform}/data/clothes.bin`, | |
dest: 'data', | |
}, | |
{ | |
url: `https://cdn.altv.mp/server/${BRANCH}/x64_${platform}/data/vehmodels.bin`, | |
dest: 'data', | |
}, | |
{ | |
url: `https://cdn.altv.mp/server/${BRANCH}/x64_${platform}/data/vehmods.bin`, | |
dest: 'data', | |
}, | |
{ | |
url: `https://cdn.altv.mp/js-module/${BRANCH}/x64_${platform}/modules/js-module/js-module.${ | |
platform == 'win32' ? 'dll' : 'so' | |
}`, | |
dest: 'modules/js-module', | |
}, | |
{ | |
url: `https://cdn.altv.mp/js-module/${BRANCH}/x64_${platform}/modules/js-module/libnode.${ | |
platform == 'win32' ? 'dll' : 'so.83' | |
}`, | |
dest: 'modules/js-module', | |
}, | |
]; | |
files.forEach((f) => { | |
const fileName = path.basename(f.url); | |
const destPath = path.join(BUILD_DIR, f.dest, fileName); | |
const filePath = path.dirname(destPath); | |
if (!fs.existsSync(filePath)) fs.mkdirSync(filePath, { recursive: true }); | |
downloadFile(f.url, destPath); | |
}); | |
if (platform !== 'win32') { | |
logger.log('trace', `Downloading 'start.sh' file for Linux`); | |
downloadFile( | |
'https://cdn.altv.mp/others/start.sh', | |
path.join(BUILD_DIR, 'start.sh') | |
); | |
const cmd = `chmod +x ${path.join(BUILD_DIR, 'start.sh')}`; | |
exec(cmd, (err) => { | |
if (err) logger.log('error', err.message); | |
}); | |
} | |
}; | |
const downloadFile = (url, destinationPath) => { | |
const fileName = path.basename(url); | |
const file = fs.createWriteStream(destinationPath); | |
https.get(url, (r) => { | |
r.pipe(file); | |
r.on('data', () => { | |
if (r.statusCode == 404) { | |
logger.log('error', `Failed to ensure '${fileName}'`); | |
process.exit(1); | |
} | |
}); | |
r.on('end', () => { | |
logger.log('info', `Ensured '${fileName}`); | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import { BUILD_DIR, logger } from './pkg.js'; | |
fs.rmdir(BUILD_DIR, { recursive: true }, (err) => { | |
if (err) logger.error(`Failed to remove the '${BUILD_DIR}' directory`); | |
else logger.info(`Successfully removed the '${BUILD_DIR}' directory`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BUILD_DIR = 'build'; | |
const BRANCH = 'dev'; | |
const MANIFEST_FILE = '_pkg.manifest.json'; | |
const CONFIG_DIR = '~./.config/configstore'; | |
const CONFIG_NAME = 'urp-game-pkg'; | |
const CONFIG_KEYS = { | |
VERSION: 'VERSION', | |
}; | |
import winston from 'winston'; | |
const logger = winston.createLogger({ | |
level: 'info', | |
format: winston.format.json(), | |
defaultMeta: { service: 'pkg' }, | |
transports: [ | |
new winston.transports.File({ filename: './logs/pkg' }), | |
new winston.transports.Console({ format: winston.format.simple() }), | |
], | |
}); | |
import Configstore from 'configstore'; | |
const config = new Configstore(CONFIG_NAME); | |
export { | |
BUILD_DIR, | |
BRANCH, | |
MANIFEST_FILE, | |
CONFIG_KEYS, | |
CONFIG_DIR, | |
CONFIG_NAME, | |
logger, | |
config, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment