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();