This commit is contained in:
171
index.js
171
index.js
@@ -1,8 +1,8 @@
|
||||
const { ScreepsAPI } = require('screeps-api');
|
||||
const { ScreepsAPI } = require("screeps-api");
|
||||
const core = require("@actions/core");
|
||||
const fs = require('fs');
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
const fs = require("fs");
|
||||
const glob = require("glob");
|
||||
const path = require("path");
|
||||
|
||||
/**
|
||||
* Reads files matching a glob pattern into a dictionary.
|
||||
@@ -11,40 +11,39 @@ const path = require('path');
|
||||
* @returns {Promise<Object>} - Promise resolving to a dictionary of file contents keyed by filenames.
|
||||
*/
|
||||
function readFilesIntoDict(pattern, prefix) {
|
||||
// Prepend the prefix to the glob pattern
|
||||
const globPattern = prefix ? path.join(prefix, pattern) : pattern;
|
||||
// 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);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
glob(globPattern, (err, files) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
let fileDict = {};
|
||||
let readPromises = [];
|
||||
let fileDict = {};
|
||||
let readPromises = [];
|
||||
|
||||
files.forEach(file => {
|
||||
let readPromise = fs.promises.readFile(file, 'utf8')
|
||||
.then(content => {
|
||||
// Remove the prefix from the filename and drop the file suffix
|
||||
let key = file;
|
||||
if (prefix && file.startsWith(prefix)) {
|
||||
key = key.slice(prefix.length);
|
||||
}
|
||||
key = path.basename(key, path.extname(key)); // Drop the file suffix
|
||||
files.forEach((file) => {
|
||||
let readPromise = fs.promises.readFile(file, "utf8").then((content) => {
|
||||
// Remove the prefix from the filename and drop the file suffix
|
||||
let key = file;
|
||||
if (prefix && file.startsWith(prefix)) {
|
||||
key = key.slice(prefix.length);
|
||||
}
|
||||
key = path.basename(key, path.extname(key)); // Drop the file suffix
|
||||
|
||||
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);
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,72 +54,72 @@ function readFilesIntoDict(pattern, prefix) {
|
||||
* @returns {string|null} - Returns an error message if validation fails, otherwise null.
|
||||
*/
|
||||
function validateAuthentication(token, username, password) {
|
||||
if (token) {
|
||||
if (username || password) {
|
||||
return "Token is defined along with username and/or password.";
|
||||
}
|
||||
} else {
|
||||
if (!username && !password) {
|
||||
return "Neither token nor password and username are defined.";
|
||||
}
|
||||
if (username && !password) {
|
||||
return "Username is defined but no password is provided.";
|
||||
}
|
||||
if (!username && password) {
|
||||
return "Password is defined but no username is provided.";
|
||||
}
|
||||
if (token) {
|
||||
if (username || password) {
|
||||
return "Token is defined along with username and/or password.";
|
||||
}
|
||||
return null; // No errors found
|
||||
} else {
|
||||
if (!username && !password) {
|
||||
return "Neither token nor password and username are defined.";
|
||||
}
|
||||
if (username && !password) {
|
||||
return "Username is defined but no password is provided.";
|
||||
}
|
||||
if (!username && password) {
|
||||
return "Password is defined but no username is provided.";
|
||||
}
|
||||
}
|
||||
return null; // No errors found
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts code to Screeps server.
|
||||
*/
|
||||
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 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("source-prefix");
|
||||
const pattern = core.getInput("pattern") || "*.js";
|
||||
const branch = core.getInput("branch") || "default";
|
||||
|
||||
const files_to_push = await readFilesIntoDict(pattern, prefix);
|
||||
|
||||
Object.keys(files_to_push).forEach(key => {
|
||||
core.info(`Key: ${key}`);
|
||||
});
|
||||
|
||||
core.info(files_to_push);
|
||||
const token = core.getInput("token") || undefined;
|
||||
const username = core.getInput("username") || undefined;
|
||||
const password = core.getInput("password") || undefined;
|
||||
const prefix = core.getInput("source-prefix");
|
||||
const pattern = core.getInput("pattern") || "*.js";
|
||||
const branch = core.getInput("branch") || "default";
|
||||
|
||||
const login_arguments = {
|
||||
"token": token,
|
||||
"username": username,
|
||||
"password": password,
|
||||
"protocol": protocol,
|
||||
"hostname": hostname,
|
||||
"port": port,
|
||||
"path": path,
|
||||
};
|
||||
const files_to_push = await readFilesIntoDict(pattern, prefix);
|
||||
|
||||
core.info(`Trying to upload the following files to ${branch}:`)
|
||||
Object.keys(login_arguments).forEach(key => {
|
||||
core.info(`Key: ${key}`);
|
||||
});
|
||||
Object.keys(files_to_push).forEach((key) => {
|
||||
core.info(`Key: ${key}`);
|
||||
});
|
||||
|
||||
const errorMessage = validateAuthentication(token, username, password);
|
||||
if (errorMessage) {
|
||||
core.error(errorMessage);
|
||||
return;
|
||||
}
|
||||
core.info(files_to_push);
|
||||
|
||||
const api = new ScreepsAPI(login_arguments);
|
||||
const response = await api.code.set(branch, files_to_push);
|
||||
core.info(JSON.stringify(response, null, 2));
|
||||
const login_arguments = {
|
||||
token: token,
|
||||
username: username,
|
||||
password: password,
|
||||
protocol: protocol,
|
||||
hostname: hostname,
|
||||
port: port,
|
||||
path: path,
|
||||
};
|
||||
|
||||
core.info(`Trying to upload the following files to ${branch}:`);
|
||||
Object.keys(login_arguments).forEach((key) => {
|
||||
core.info(`Key: ${key}`);
|
||||
});
|
||||
|
||||
const errorMessage = validateAuthentication(token, username, password);
|
||||
if (errorMessage) {
|
||||
core.error(errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
const api = new ScreepsAPI(login_arguments);
|
||||
const response = await api.code.set(branch, files_to_push);
|
||||
core.info(JSON.stringify(response, null, 2));
|
||||
}
|
||||
|
||||
postCode();
|
||||
|
||||
Reference in New Issue
Block a user