EdenPlus / screeps

0 stars 1 forks source link

Automated Code Transfers #15

Open EdenPlus opened 6 years ago

EdenPlus commented 6 years ago

Dropping this link here for later use. Take note that there is an easier way to update the live codebase! Screeps External Commit

EdenPlus commented 6 years ago

Based on testing it appears that by linking your GitHub account, screeps will auto pull from the master whenever it is updated. Ways to work with this without losing many hours of work:

  1. Put all code changes onto their respective branch in preparation for a code merger
  2. Disable the sync whenever it's not needed
EdenPlus commented 6 years ago

I have solved the issue of moving code from Screeps to GitHub, below is the code for this solution. The tested environment is RunKit in browser.

/* Start Requirements */
const Promise = require("es6-promise").Promise;
const fs = require('fs');
const express = require('express');
const app = express();
const { ScreepsAPI } = require('screeps-api');
const Octokit = require("octokit");
/* End Requirements */

/* Start ScreepsAPI stuff */
// All options are optional
const api = new ScreepsAPI({
  token: '<Screeps-Token>',
  protocol: 'https',
  hostname: 'screeps.com',
  port: 443,
  path: '/' // Do no include '/api', it will be added automatically
});

// You can overwrite parameters if needed
api.auth('screeps@email.com','notMyPass',{
  protocol: 'https',
  hostname: 'screeps.com',
  port: 443,
  path: '/' // Do no include '/api', it will be added automatically
});
/* End ScreepsAPI stuff */

/* Start GitHub Stuff */
var gh = Octokit.new({
  token: "<GitHub-Token>"
});

var repoowner = "EdenPlus";
var reponame = "screeps";
var branchname = "prototype";
var repo = gh.getRepo(repoowner, reponame);
var branch = repo.getBranch(branchname);

api.code.get('default').then(function (data) {
    var contents = {};
    for (var a in data.modules) {
        contents["src/" + a] = data.modules[a];
    }
    var message = "Automated Update";
    branch.writeMany(contents, message)
    .then(function() {
        console.log("Code posted");
    })
    .catch(function(err) {
        console.log(err);
    });
});
/* End GitHub Stuff */