Rework the index.js
All checks were successful
Lint / pre-commit Linting (push) Successful in 24s

This commit is contained in:
2025-04-22 00:53:28 +02:00
parent d6c9360dcf
commit 751fd07d45

View File

@ -43,7 +43,7 @@ function replacePlaceholders(content, hostname) {
* @param {string} hostname
* @returns {Promise<string[]>}
*/
async function readReplaceAndWriteFiles(pattern, prefix, hostname) {
function readReplaceAndWriteFiles(pattern, prefix, hostname) {
return new Promise((resolve, reject) => {
const globPattern = prefix ? path.join(prefix, pattern) : pattern;
@ -143,7 +143,7 @@ function validateAuthentication(token, username, password) {
/**
* Posts code to Screeps server.
*/
async function postCode() {
function postCode() {
const protocol = core.getInput("protocol") || "https";
const hostname = core.getInput("hostname") || "screeps.com";
const port = core.getInput("port") || "443";
@ -158,21 +158,6 @@ async function postCode() {
const gitReplace = core.getInput("git-replace") || null;
if (gitReplace) {
await readReplaceAndWriteFiles(gitReplace, prefix, hostname);
}
const files_to_push = await readFilesIntoDict(pattern, prefix);
core.info(
`Uploading ${
Object.keys(files_to_push).length
} file(s) to branch '${branch}':`,
);
Object.keys(files_to_push).forEach((key) => {
core.info(` File: ${key}`);
});
const login_arguments = {
token,
username,
@ -183,35 +168,48 @@ async function postCode() {
path,
};
const errorMessage = validateAuthentication(token, username, password);
if (errorMessage) {
core.setFailed(errorMessage);
return Promise.reject(new Error(errorMessage));
}
const replacePromise = gitReplace
? readReplaceAndWriteFiles(gitReplace, prefix, hostname)
: Promise.resolve();
return replacePromise
.then(() => readFilesIntoDict(pattern, prefix))
.then((files_to_push) => {
core.info(
`Uploading ${
Object.keys(files_to_push).length
} file(s) to branch '${branch}':`,
);
Object.keys(files_to_push).forEach((key) => {
core.info(` File: ${key}`);
});
core.info("Authentication arguments:");
core.info(JSON.stringify(login_arguments, null, 2));
const errorMessage = validateAuthentication(token, username, password);
if (errorMessage) {
core.error(errorMessage);
return;
}
let api = new ScreepsAPI(login_arguments);
const api = new ScreepsAPI(login_arguments);
if (token) {
const response = await api.code.set(branch, files_to_push);
return api.code.set(branch, files_to_push).then((response) => {
core.info(JSON.stringify(response, null, 2));
console.log(`Code set successfully to ${branch}`);
});
} else {
core.info(`Logging in as user ${username}`);
await Promise.resolve()
.then(() => api.auth(username, password, login_arguments))
.then(() => {
return api.code.set(branch, files_to_push);
})
return api
.auth(username, password, login_arguments)
.then(() => api.code.set(branch, files_to_push))
.then(() => {
console.log(`Code set successfully to ${branch}`);
})
.catch((err) => {
console.error("Error:", err);
});
}
});
}
postCode().catch((err) => {