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