Skip to content

Instantly share code, notes, and snippets.

@laverdet
Last active September 7, 2021 05:02
Show Gist options
  • Save laverdet/e761440e0918ab1cf21e to your computer and use it in GitHub Desktop.
Save laverdet/e761440e0918ab1cf21e to your computer and use it in GitHub Desktop.
Screeps git sync script

To use first you need to create two branches, master and sim using the in-game editor. Then just run node sync.js from your local machine. When you're on your local master branch it will push to master in game, any other branch (including detached states) will push to sim. Note that it will push unstaged changes, so be careful when switching to branches with modified code.

There is support included for compressing your JS files on the server side via UglifyJS. Some users reported some speedups by compressing their code, however I did not notice any significant gains. Furthermore, UglifyJS does not yet support ES6 features, so it's disabled by default.

"use strict";
var https = require('https');
var fs = require('fs');
var uglify = false; // Set to true to uglify files before upload. ES6 is *not* supported-- https://github.com/mishoo/UglifyJS2/issues/448
// Watch git
function parseBranch(HEAD) {
return HEAD === 'ref: refs/heads/master\n' ? 'master' : 'dev';
}
var branch = parseBranch(fs.readFileSync('.git/HEAD', 'utf8'));
fs.watch('.git', function(ev, file) {
if (file === 'HEAD') {
var tmp = parseBranch(fs.readFileSync('.git/HEAD', 'utf8'));
if (tmp !== branch) {
branch = tmp;
console.log('Switching to: '+ branch);
}
}
});
// Crush code if master
function crush(branch, file, code) {
if (!uglify) {
return code;
}
var ujs = require('uglify-js');
console.log('Parsing', file);
var ast = ujs.parse(code);
ast.figure_out_scope();
var compressor = ujs.Compressor();
var compressed = ast.transform(compressor);
compressed.figure_out_scope();
compressed.compute_char_frequency();
compressed.mangle_names({ toplevel: true, sort: true });
return compressed.print_to_string();
}
// Read local code from disk
var modules = {};
function refreshLocalBranch() {
modules = {};
fs.readdirSync('.').forEach(function(file) {
if (file !== 'sync.js' && /\.js$/.test(file)) {
modules[file.replace( /\.js$/, '')] = crush(branch, file, fs.readFileSync(file, 'utf8'));
}
});
}
refreshLocalBranch();
schedulePush();
// Watch for local changes
var pushTimeout;
fs.watch('.', function(ev, file) {
if (file !== 'sync.js' && /\.js$/.test(file)) {
try {
modules[file.replace(/\.js$/, '')] = crush(branch, file, fs.readFileSync(file, 'utf8'));
} catch (err) {
delete modules[file.replace(/\.js$/, '')];
}
schedulePush();
}
});
// Push changes to screeps.com
function schedulePush() {
if (pushTimeout) {
clearTimeout(pushTimeout);
}
pushTimeout = setTimeout(function() {
pushTimeout = undefined;
var req = https.request({
hostname: 'screeps.com',
port: 443,
path: '/api/user/code',
method: 'POST',
auth: process.argv[2],
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
});
req.end(JSON.stringify({ branch: branch, modules: modules }));
req.on('response', function(res) {
console.log('HTTP Status '+ res.statusCode);
});
}, 50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment