diff --git a/index.js b/index.js new file mode 100644 index 0000000..102444a --- /dev/null +++ b/index.js @@ -0,0 +1,88 @@ +const { ScreepsAPI } = require('screeps-api'); +const core = require("@actions/core"); + +const fs = require('fs'); +const glob = require('glob'); +const path = require('path'); + +function readFilesIntoDict(pattern, prefix) { + // Prepend the prefix to the glob pattern + const globPattern = prefix ? path.join(prefix, pattern) : pattern; + + return new Promise((resolve, reject) => { + glob(globPattern, (err, files) => { + if (err) { + return reject(err); + } + + let fileDict = {}; + let readPromises = []; + + files.forEach(file => { + let readPromise = fs.promises.readFile(file, 'utf8') + .then(content => { + // Correctly remove the prefix from the filename + const key = prefix && file.startsWith(prefix) ? file.slice(prefix.length) : file; + fileDict[key] = content; + }); + + readPromises.push(readPromise); + }); + + // Use Promise.all to ensure all files are read before resolving + Promise.all(readPromises) + .then(() => resolve(fileDict)) + .catch(reject); + }); + }); +} + + +async function postCode() { + const protocol = core.getInput("protocol") || "https"; + const hostname = core.getInput("hostname") || "screeps.com"; + const port = core.getInput("port") || "443"; + const path = core.getInput("path") || "/"; + + const token = core.getInput("token") || undefined; + const username = core.getInput("username") || undefined; + const password = core.getInput("password") || undefined; + + const prefix = core.getInput("prefix"); + + const pattern = core.getInput("pattern") || "*.js"; + const branch = core.getInput("branch") || "default"; + + const files_to_push = readFilesIntoDict(pattern, prefix); + + const login_arguemnts = { + "token": token, + "username": username, + "passowrd": password, + "protocol": protocol, + "hostname": hostname, + "port": port, + "path": path, + }; + if (!token) { + if (!username && !password) { + core.error("Neither token nor password and username are defined.") + } else if (username && !password) { + core.error("It seems like you would like to login via username but no username seems to be defined."); + } else if (!username && password) { + core.error("It seems like you would like to login via username but no password seems to be defined."); + } + } else if (username && password) { + core.error("In addition to a token both username and password are defined."); + } else if (username) { + core.error("In addition to a token the username is defined."); + } else if (password) { + core.error("In addition to a token the passwrd is defined.") + } + core.log(JSON.stringify(login_arguemnts, null, 2)); + const api = new ScreepsAPI(login_arguemnts); + api.code.set(branch, files_to_push) + +} + +postCode(); diff --git a/package-lock.json b/package-lock.json index 98558b6..8c4b628 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "dependencies": { "@actions/core": "^1.10.1", + "glob": "^7.1.6", "screeps-api": "^1.16.0" } }, diff --git a/package.json b/package.json index 0ff42ff..ed2c03b 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,15 @@ { - "name": "gitea-act-create-pr", + "name": "screeps-deploy-action", "version": "0.1.0", - "description": "Gitea PR Creation Action", + "description": "Deploys screeps code to the official game or an pirvate server.", "main": "index.js", + "scripts": { + "start": "node index.js" + }, "dependencies": { "@actions/core": "^1.10.1", - "screeps-api": "^1.16.0" + "screeps-api": "^1.16.0", + "glob": "^7.1.6" } } \ No newline at end of file