diff --git a/package.json b/package.json index 628069b..fe62020 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "webpack --config webpack.config.js --optimize-minimize --devtool source-map", "develop": "webpack-dev-server", - "test": "karma start" + "test": "karma start", + "deploy": "node scripts/deploy.js" }, "repository": { "type": "git", @@ -24,7 +25,10 @@ "homepage": "https://github.com/Cardshifter/HTML-Client#readme", "private": true, "devDependencies": { + "bluebird": "^2.10.0", "css-loader": "^0.15.6", + "ftp-deploy": "^1.0.0", + "temp": "^0.8.3", "html-loader": "^0.3.0", "jasmine-core": "^2.3.4", "karma": "^0.13.8", @@ -33,14 +37,16 @@ "karma-webpack": "^1.7.0", "ng-annotate-webpack-plugin": "^0.1.2", "phantomjs": "^1.9.17", + "recursive-copy": "^1.0.10", + "request-promise": "^0.4.3", "style-loader": "^0.12.3", "webpack": "^1.11.0", "webpack-dev-server": "^1.10.1" }, "dependencies": { "angular": "1.4.3", - "angular-route": "1.4.3", + "angular-animate": "1.4.3", "angular-bootstrap-npm": "0.13.3", - "angular-animate": "1.4.3" + "angular-route": "1.4.3" } } diff --git a/scripts/deploy.js b/scripts/deploy.js new file mode 100644 index 0000000..874f6ff --- /dev/null +++ b/scripts/deploy.js @@ -0,0 +1,119 @@ +/* Deploy the client to play.cardshifter.com + * + * How to use: + * 1. Build the project and make sure that everything is in order. + * 2. Set up environment variables (see below). + * 3. Run `npm run deploy`. + * 4. Profit! + * + * Environment variables used: + * - DEPLOY_FTP_USERNAME: Username used to log in through FTP. + * - DEPLOY_FTP_PASSWORD: Password used to log in through FTP. + * - DEPLOY_DUGA_KEY: Duga API key. If not set the chat bot post is skipped. + */ +'use strict'; + +var copy = require('recursive-copy'); +var FtpDeploy = require('ftp-deploy'); +var path = require('path'); +var Promise = require('bluebird'); +var request = require('request-promise'); +var temp = require('temp').track(); + +var deployAddress = "play.cardshifter.com"; + +function ftpConfig(local, remote) { + return { + username: process.env.DEPLOY_FTP_USERNAME, + password: process.env.DEPLOY_FTP_PASSWORD, + host: deployAddress, + port: 21, // Standard FTP port + localRoot: local, + remoteRoot: remote + }; +}; + +var chatBotRequest = { + // http://chat.stackexchange.com/rooms/info/16134/cardshifter-tcg + roomId: 16134, + apiKey: process.env.DEPLOY_DUGA_KEY, + text: "New web client version uploaded to [" + deployAddress + "](http://" + deployAddress +"/)." +}; + +var chatBotConfig = { + url: "http://stats.zomis.net/GithubHookSEChatService/bot/jsonPost", + method: "POST", + headers: { + "Content-Type": "application/json" + } +} + +function postToChat(config, botRequest) { + return new Promise(function(resolve, reject) { + var json = JSON.stringify(botRequest, ["apiKey", "roomId", "text"]); + config.headers["Content-Length"] = json.length; + config.body = json; + + request(config) + .then(function(body) { + resolve(body); + }) + }); +} + +function setupFiles() { + // ftp-deploy doesn't handle uploading from multiple directories well + return new Promise(function(resolve, reject) { + temp.mkdir("cardshifter-deploy", function (err, tempDir) { + if (err) { + reject(err); + } + + Promise.all([ + copy(path.join(__dirname, "..", "www"), tempDir), + copy(path.join(__dirname, "..", "dist"), path.join(tempDir, "assets")) + ]) + .then(function() { + resolve(tempDir); + }) + .catch(function(err) { + reject(err); + }); + }); + }); +} + +function deployFtp(config) { + return new Promise(function(resolve, reject) { + new FtpDeploy().deploy(config, function(err) { + if (err) { + reject(err); + } else { + resolve(); + } + }); + }); +} + +setupFiles() +.then(function(dir) { + var config = ftpConfig(dir, "/"); + console.log("Deploying to ftp://" + config.host + ":" + config.port + "..."); + return deployFtp(config); +}) +.then(function() { + console.log("FTP deployment successful."); + if (chatBotRequest.apiKey) { + console.log("Posting message to " + chatBotConfig.url + "..."); + return postToChat(chatBotConfig, chatBotRequest); + } +}) +.then(function(responseBody) { + if (responseBody) { + console.log(responseBody); + } +}) +.catch(function(err) { + console.error("Error: " + err); +}); +