UNPKG

cloudscript-uploader

Version:
67 lines (62 loc) 2.63 kB
const CodeReader = require("./CodeReader.js"); const playfabSdk = require("playfab-sdk"); const path = require("path"); const chalk = require("chalk"); const fs = require("fs"); class PlayFabCodeUploader { constructor(title, key, hasToGoLive, pathToCode) { this._title = title; this._key = key; this._hasToGoLive = hasToGoLive; this._pathToCode = pathToCode; } CheckCodeFile() { const extension = path.extname(this._pathToCode); if (extension != ".js") { let errorMessage = "Error: File has no .js extension, given extension was: " + extension + "\n" errorMessage += "Try using single or double quotes on path argument as follows: -P='./folder/file.js'\n"; console.log(chalk.red(errorMessage)); process.exit(1); return; } const isGoodPath = fs.existsSync(this._pathToCode); if (!isGoodPath) { const absolutePath = path.resolve(this._pathToCode); let errorMessage = "Error: File doesn't exists, given path was: " + absolutePath + "\n" errorMessage += "Try using single or double quotes on path argument as follows: -P='./folder/file.js'\n"; console.log(chalk.red(errorMessage)); process.exit(1); return; } } UploadWithConfig() { this.CheckCodeFile(this._pathToCode); const codeReader = new CodeReader(this._pathToCode); const code = codeReader.Read(); playfabSdk.PlayFab.settings.developerSecretKey = this._key; playfabSdk.PlayFab.settings.titleId = this._title; this.UploadCode(code, codeReader); } UploadCode(code) { const fileName = path.basename(this._pathToCode); const cloudScriptFileRequest = { FileContents: code, Filename: fileName }; const request = { Files: [cloudScriptFileRequest], Publish: this._hasToGoLive }; playfabSdk.PlayFabAdmin.UpdateCloudScript(request, (error, result) => { if (error != null) { const errorMessage = "CloudScript Request Error:\n" + JSON.stringify(error, 2, 2) + "\n" console.log(chalk.red(errorMessage)); process.exit(1); return; } const successMessage = "CloudScript Request Succeeded:\n" + JSON.stringify(result, 2, 2) + "\n"; console.log(chalk.green(successMessage)); }); } } module.exports = PlayFabCodeUploader;