Minor refactoring and typo fixing.
Some checks failed
Lint / pre-commit Linting (push) Failing after 22s
Some checks failed
Lint / pre-commit Linting (push) Failing after 22s
This commit is contained in:
parent
3c9c228e9b
commit
11c30e71ca
75
index.js
75
index.js
@ -1,10 +1,15 @@
|
||||
const { ScreepsAPI } = require('screeps-api');
|
||||
const core = require("@actions/core");
|
||||
|
||||
const fs = require('fs');
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Reads files matching a glob pattern into a dictionary.
|
||||
* @param {string} pattern - Glob pattern to match files.
|
||||
* @param {string} prefix - Directory prefix for file paths.
|
||||
* @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;
|
||||
@ -37,7 +42,35 @@ function readFilesIntoDict(pattern, prefix) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the provided authentication credentials.
|
||||
* @param {string} token - The authentication token.
|
||||
* @param {string} username - The username.
|
||||
* @param {string} password - The password.
|
||||
* @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.";
|
||||
}
|
||||
}
|
||||
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";
|
||||
@ -47,42 +80,34 @@ async function postCode() {
|
||||
const token = core.getInput("token") || undefined;
|
||||
const username = core.getInput("username") || undefined;
|
||||
const password = core.getInput("password") || undefined;
|
||||
|
||||
const prefix = core.getInput("prefix");
|
||||
|
||||
const prefix = core.getInput("source-prefix");
|
||||
const pattern = core.getInput("pattern") || "*.js";
|
||||
const branch = core.getInput("branch") || "default";
|
||||
|
||||
const files_to_push = readFilesIntoDict(pattern, prefix);
|
||||
const files_to_push = await readFilesIntoDict(pattern, prefix);
|
||||
|
||||
const login_arguemnts = {
|
||||
const login_arguments = {
|
||||
"token": token,
|
||||
"username": username,
|
||||
"passowrd": password,
|
||||
"password": 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)
|
||||
|
||||
// TODO: Refactor error handling for cleaner code
|
||||
const errorMessage = validateAuthentication(token, username, password);
|
||||
if (errorMessage) {
|
||||
core.error(errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Consider moving log statement after error checks
|
||||
core.log(JSON.stringify(login_arguments, null, 2));
|
||||
|
||||
const api = new ScreepsAPI(login_arguments);
|
||||
await api.code.set(branch, files_to_push);
|
||||
}
|
||||
|
||||
postCode();
|
||||
|
Loading…
x
Reference in New Issue
Block a user