Skip to content

Instantly share code, notes, and snippets.

@nevercast
Created January 23, 2024 20:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevercast/04f617940eaf799ca7ea76d659cbb379 to your computer and use it in GitHub Desktop.
Save nevercast/04f617940eaf799ca7ea76d659cbb379 to your computer and use it in GitHub Desktop.
Push code to Screeps
const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const util = require('util');
const process = require('process');
const os = require('os');
const TEXT_EXTENSIONS = ['.js', '.json'];
const servers = {
persistent: { name: 'Screeps', host: 'screeps.com', path: '/api/user/code' },
ptr: { name: 'PTR', host: 'screeps.com', path: '/ptr/api/user/code' },
season: { name: 'Season', host: 'screeps.com', path: '/season/api/user/code' }
};
function findConfigFile() {
let dir = process.cwd();
while (dir !== path.dirname(dir)) {
const configPath = path.join(dir, '.screepsrc');
if (fs.existsSync(configPath)) {
return configPath;
}
dir = path.dirname(dir);
}
return path.join(os.homedir(), '.screepsrc');
}
function readConfig() {
try {
const configPath = findConfigFile();
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
}
catch (error) {
console.error('Error reading .screepsrc file:', error.message);
process.exit(1);
}
}
function pushToScreeps(options, sourcePath) {
let server = options.server || (options.ptr ? 'ptr' : 'persistent');
if(servers[server]) {
server = servers[server];
}
let modules = {};
fs.readdirSync(sourcePath).forEach(file => {
const filePath = path.join(sourcePath, file);
const ext = path.extname(file);
const name = path.basename(file, ext);
if(TEXT_EXTENSIONS.indexOf(ext) !== -1) {
console.log('Pushing ' + file + '...');
modules[name] = fs.readFileSync(filePath, {encoding: 'utf8'});
}
else {
console.log('Pushing ' + file + ' (binary)...');
modules[name] = {binary: fs.readFileSync(filePath).toString('base64')};
}
});
const requestOptions = {
hostname: server.host || 'screeps.com',
port: server.port || (server.http ? 80 : 443),
path: server.path || '/api/user/code',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
if(options.token) {
requestOptions.headers['X-Token'] = options.token;
} else {
requestOptions.auth = options.email + ':' + options.password;
}
const proto = server.http ? http : https,
req = proto.request(requestOptions, function(res) {
res.setEncoding('utf8');
let data = '';
if(res.statusCode < 200 || res.statusCode >= 300) {
console.error('Screeps server returned error code ' + res.statusCode);
return;
}
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
const serverText = server && (server.name || server.host) || 'Screeps';
try {
const parsed = JSON.parse(data);
if(parsed.ok) {
let msg = 'Committed to ' + serverText;
if(options.branch) {
msg += ' branch "' + options.branch+'"';
}
msg += '.';
console.log(msg);
}
else {
console.error('Error while committing to ' + serverText + ': '+util.inspect(parsed));
}
} catch (e) {
console.error('Error while processing ' + serverText + ' json: '+e.message);
}
});
});
req.on('error', function(error) {
console.error('Error writing to Screeps server:', error.message);
process.exit(1);
});
const postData = {modules: modules};
if(options.branch) {
postData.branch = options.branch;
}
req.write(JSON.stringify(postData));
req.end();
}
function main(path) {
const config = readConfig();
try {
pushToScreeps(config, path);
} catch (error) {
console.error('Error while pushing to Screeps: ', error.message);
process.exit(1);
}
}
if (process.argv.length < 3) {
console.error('Usage: push.js <path>');
process.exit(1);
}
main(process.argv[2]);
@nevercast
Copy link
Author

nevercast commented Jan 23, 2024

Screeps Simple Code Push (SCP)

Invoke with:

node push.js path/to/your/files

You should have a .screepsrc file in the same directory as your current directory, or one of the parent directories. This file contains your API token and other configuration. The file format is as follows:

{
  "token": "token here",
  "server": "persistent",
  "branch": "default"
}

Username and Password are also supported, but I personally don't recommend that.

{
  "email": "email here",
  "password": "password here",
  "server": "persistent",
  "branch": "default"
}

This script was adapted from the Grunt script, and a few other ideas I found, and then adapted to my own bot environment. You're welcome to use it.

An example project layout may be the following:

/
├── src/
│   ├── main.js
│   └── library.js
├── tools/
│   └── push.js
└── .screepsrc

You would run:

node tools/push.js src/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment