UNPKG

@jtff/miztemplate-lib

Version:
269 lines (243 loc) 11.5 kB
"use strict"; const fs = require("fs"); const path = require("path"); const { google } = require("googleapis"); const ftpClient = require('ftp'); const Mizlib = require("./mizlib"); const { getInstalledPathSync } = require('get-installed-path') class MizTemplateCI{ constructor(config) { this.config = config; this.mizlib = new Mizlib(); } getVersion() { return this.getVersionObject(this.config.general.missionVersion); } setVersionfromString(strVersion, configFilePath) { return this.setVersion(this.getVersionObject(strVersion), configFilePath) } setVersion(versionObject, configFilePath) { this.config.general.missionVersion = this.displayVersion(versionObject); let data = JSON.stringify(this.config, null, 4); fs.writeFileSync(configFilePath, data); } displayVersion(versionObject) { return [versionObject.major, versionObject.minor, versionObject.releaseSuffix ? versionObject.patch + '-' + versionObject.releaseSuffix : versionObject.patch].join('.') } getVersionObject(strVersion) { const [major, minor, interimPatch] = strVersion.split('.', 3); const [patch, releaseSuffix] = interimPatch.split('-', 2); return { major: parseInt(major), minor: parseInt(minor), patch: parseInt(patch), releaseSuffix: releaseSuffix ? releaseSuffix : null }; } nextMajor(versionObject) { const newVersionObject = versionObject; newVersionObject.major = newVersionObject.major + 1; return newVersionObject; } nextMinor(versionObject) { const newVersionObject = versionObject; newVersionObject.minor = newVersionObject.minor + 1; return newVersionObject; } nextPatch(versionObject) { const newVersionObject = versionObject; newVersionObject.patch = newVersionObject.patch + 1; return newVersionObject; } getDestinationMizFilePathArrayFromConfig() { const returnArray = this.config.missionTemplates.map(missionTemplate => { return this.config.general.missionFolder + '/' + this.config.general.missionPrefix + '_' + missionTemplate.theatre + '_' + this.config.general.missionSuffix + '_' + this.config.general.missionVersion + '.miz' }); return returnArray; } getDestinationMizFilePathFromConfigMissionTemplate(missionTemplate) { const destinationMizPath = [this.config.general.missionFolder + '/', this.config.general.missionPrefix + '_', missionTemplate.theatre + '_', this.config.general.missionSuffix.length > 0 ? this.config.general.missionSuffix + '_' : '', this.config.general.missionVersion + '.miz'].join('') ; return destinationMizPath; } getGeneratedMizFilePaths() { return fs.readdirSync(this.config.general.missionFolder + '/') .filter(file => file.endsWith((".miz"))) .map(mizFile => { return this.config.general.missionFolder + '/' + mizFile; }); } uploadMizFiles(credentials) { const destFtpServer = new ftpClient(); const configObject = this.config; destFtpServer.connect({ host: credentials.host, secure: false, user: credentials.user, password: credentials.password }); destFtpServer.on('ready', function () { destFtpServer.cwd(credentials.folder, function (err, curDir) { if (err) throw err; Promise.all( fs.readdirSync(configObject.general.missionFolder) .filter(file => file.endsWith('.pub.json')) .map(file => { return new Promise((resolve, reject) => { // console.log(configObject.general.missionFolder+'/'+file); fs.readFile(configObject.general.missionFolder + '/' + file, function (err, data) { if (err) reject(err); data = JSON.parse(data.toString()); // console.log(data); destFtpServer.put(data.mizFiles, path.basename(data.mizFiles), false, function (err) { if (err) reject(err); console.log('file ' + path.basename(data.mizFiles) + ' uploaded on DCS Server'); resolve(path.basename(data.mizFiles)); }); }); }) }) ).then(() => { destFtpServer.end(); }); }); }); } publishMizFiles(credentials) { const configObject = this.config; const jwtClient = new google.auth.JWT( credentials.client_email, null, credentials.private_key, configObject.google.scopes ); jwtClient.authorize(function (err, tokens) { if (err) { console.log(err); throw err; } else { console.log("Successfully connected!"); const drive = google.drive({ version: 'v3', jwtClient }); fs.readdirSync(configObject.general.missionFolder) .filter(file => file.endsWith('.pub.json')) .map(file => { // console.log(configObject.general.missionFolder+'/'+file); fs.readFile(configObject.general.missionFolder + '/' + file, function (err, data) { if (err) throw err; data = JSON.parse(data.toString()); // console.log(data); drive.files.list({ q: `'${data.gdriveFolder}' in parents and trashed = false and name = '${path.basename(data.mizFiles)}'`, auth: jwtClient }).then(rsp => { // console.log(rsp.data.files); // console.log(path.basename(data.mizFiles)); const fileMetadata = { 'name': path.basename(data.mizFiles), parents: [data.gdriveFolder] }; const media = { mimeType: 'application/zip', body: fs.createReadStream(data.mizFiles) }; if (rsp.data.files.length > 0) { drive.files.update({ fileId: rsp.data.files[0].id, auth: jwtClient, media: media }, (err, file) => { if (err) { // Handle error console.error(err); } else { console.log('File updated Id: ', file.id); } }); } else { drive.files.create({ resource: fileMetadata, media: media, auth: jwtClient, fields: 'id' }, (err, file) => { if (err) { // Handle error console.error(err); } else { console.log('File created Id: ', file.id); } }); } }); }); }); } }); } async generateConfigJsonFileFromZipObject(zip,filename) { let theatreString = (await this.mizlib.getMissionObjectFromZipObject(zip)).theatre; return { "general": { "missionFolder": "dist", "missionPrefix": path.parse(filename).name, "missionSuffix": "", "missionVersion": "1.0.0-snapshot" }, "google": { "scopes": [ "https://www.googleapis.com/auth/drive" ], "tokenPath": "dist/gtoken.json" }, "missionTemplates": [ { "theatre": theatreString, "filename": filename, "destFolderId": "1pK_0I2iEzhEiqWicu8hsX7VPZuKGnKoW" } ] }; } async generatePackageJsonFile(missionName) { return { "name": missionName, "version": "1.0.0-snapshot", "description": "build jtff-style missions", "main": "none", "scripts": { "build": "node .workspace/src/nodejs/build.js", "clean": "node .workspace/src/nodejs/clean.js", "template-update": "node .workspace/src/nodejs/template-update.js", "nextversion-prepare": "node .workspace/src/nodejs/prepare-nextversion.js", "version": "node .workspace/src/nodejs/getset-version.js", "release": "node .workspace/src/nodejs/release.js", "getmiz": "node .workspace/src/nodejs/get-mizfiles.js", "gdrive-upload": "node .workspace/src/nodejs/gdrive-upload.js", "ftp-upload": "node .workspace/src/nodejs/ftpupload.js" }, "author": "Sebastien LONGO", "license": "GPL-3.0-or-later", "dependencies": { "@jtff/miztemplate-lib": "latest" } }; } async InjectCiScriptsIntoWorkspace(workspacePath) { this.mizlib.copyRecursiveSync(getInstalledPathSync('@jtff/miztemplate-lib',{local: true}) + '/ci', workspacePath + '/.workspace/src/nodejs'); // fs.cpSync(getInstalledPathSync('@jtff/miztemplate-lib',{local: true}) + '/ci',workspacePath + '/.workspace/src/nodejs',{recursive: true}); } async deployCIScriptsFromZipObjectIntoWorkspace(workspacePath, mizObject, mizFilePath) { fs.writeFileSync(workspacePath + '/config.json', JSON.stringify(await this.generateConfigJsonFileFromZipObject(mizObject, path.basename(mizFilePath)), null, 4)); fs.writeFileSync(workspacePath + '/package.json', JSON.stringify(await this.generatePackageJsonFile(path.parse(mizFilePath).name), null, 4)); await this.InjectCiScriptsIntoWorkspace(workspacePath); } } module.exports = MizTemplateCI;