Awaiting some functions
All checks were successful
Lint / pre-commit Linting (push) Successful in 22s
All checks were successful
Lint / pre-commit Linting (push) Successful in 22s
This commit is contained in:
parent
7c788de362
commit
63d91e18f8
107
index.js
107
index.js
@ -5,7 +5,7 @@ const { glob } = require("glob");
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces specific placeholder strings within the provided content with corresponding dynamic values.
|
* Replaces placeholder strings in content with dynamic values.
|
||||||
*
|
*
|
||||||
* @param {string} content
|
* @param {string} content
|
||||||
* @param {string} hostname
|
* @param {string} hostname
|
||||||
@ -30,7 +30,7 @@ function replacePlaceholders(content, hostname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces placeholders in files matching a glob pattern.
|
* Replaces placeholders in files matched by glob.
|
||||||
*
|
*
|
||||||
* @param {string} pattern
|
* @param {string} pattern
|
||||||
* @param {string} [prefix]
|
* @param {string} [prefix]
|
||||||
@ -41,14 +41,12 @@ 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;
|
||||||
|
|
||||||
glob(globPattern, async (err, files) => {
|
glob(globPattern, (err, files) => {
|
||||||
if (err) {
|
if (err) return reject(err);
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!files.length) {
|
if (!files.length) {
|
||||||
core.warning(
|
core.warning(
|
||||||
`No files matched for placeholder replacement with pattern: ${globPattern}`,
|
`No files matched for placeholder replacement: ${globPattern}`,
|
||||||
);
|
);
|
||||||
return resolve([]);
|
return resolve([]);
|
||||||
}
|
}
|
||||||
@ -56,71 +54,62 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) {
|
|||||||
core.info(`📝 Matched files for replacement:`);
|
core.info(`📝 Matched files for replacement:`);
|
||||||
files.forEach((file) => core.info(` - ${file}`));
|
files.forEach((file) => core.info(` - ${file}`));
|
||||||
|
|
||||||
let processPromises = [];
|
const promises = files.map((file) =>
|
||||||
|
fs.promises
|
||||||
files.forEach((file) => {
|
|
||||||
core.info(`Replacing placeholders in file: ${file}`);
|
|
||||||
let processPromise = fs.promises
|
|
||||||
.readFile(file, "utf8")
|
.readFile(file, "utf8")
|
||||||
.then((content) => {
|
.then((content) => {
|
||||||
content = replacePlaceholders(content, hostname);
|
content = replacePlaceholders(content, hostname);
|
||||||
return fs.promises.writeFile(file, content);
|
return fs.promises.writeFile(file, content);
|
||||||
});
|
})
|
||||||
|
.then(() => {
|
||||||
|
core.info(`Replaced placeholders in file: ${file}`);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
processPromises.push(processPromise);
|
Promise.all(promises)
|
||||||
});
|
.then(() => {
|
||||||
|
core.info(
|
||||||
try {
|
`Successfully replaced placeholders in ${files.length} file(s).`,
|
||||||
await Promise.all(processPromises);
|
);
|
||||||
core.info(`Replaced placeholders in ${files.length} file(s).`);
|
resolve(files);
|
||||||
resolve(files);
|
})
|
||||||
} catch (processError) {
|
.catch(reject);
|
||||||
reject(processError);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads files matching a glob pattern into a dictionary.
|
* Reads files into a dictionary of key → content.
|
||||||
*
|
*
|
||||||
* @param {string} pattern
|
* @param {string} pattern
|
||||||
* @param {string} prefix
|
* @param {string} prefix
|
||||||
* @returns {Promise<Object>}
|
* @returns {Promise<Object>}
|
||||||
*/
|
*/
|
||||||
function readFilesIntoDict(pattern, prefix) {
|
function readFilesIntoDict(pattern, prefix) {
|
||||||
const globPattern = prefix ? path.join(prefix, pattern) : pattern;
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
const globPattern = prefix ? path.join(prefix, pattern) : pattern;
|
||||||
|
|
||||||
glob(globPattern, (err, files) => {
|
glob(globPattern, (err, files) => {
|
||||||
if (err) {
|
if (err) return reject(err);
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!files.length) {
|
if (!files.length) {
|
||||||
core.warning(
|
core.warning(`No files matched for upload: ${globPattern}`);
|
||||||
`No files matched for upload with pattern: ${globPattern}`,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
core.info(`📁 Matched files for upload:`);
|
core.info(`📁 Matched files for upload:`);
|
||||||
files.forEach((file) => core.info(` - ${file}`));
|
files.forEach((file) => core.info(` - ${file}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
let fileDict = {};
|
const fileDict = {};
|
||||||
let readPromises = [];
|
const readPromises = files.map((file) =>
|
||||||
|
fs.promises.readFile(file, "utf8").then((content) => {
|
||||||
files.forEach((file) => {
|
|
||||||
let readPromise = fs.promises.readFile(file, "utf8").then((content) => {
|
|
||||||
let key = file;
|
let key = file;
|
||||||
if (prefix && file.startsWith(prefix)) {
|
if (prefix && file.startsWith(prefix)) {
|
||||||
key = key.slice(prefix.length);
|
key = key.slice(prefix.length);
|
||||||
}
|
}
|
||||||
key = path.basename(key, path.extname(key));
|
key = path.basename(key, path.extname(key));
|
||||||
fileDict[key] = content;
|
fileDict[key] = content;
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
readPromises.push(readPromise);
|
|
||||||
});
|
|
||||||
|
|
||||||
Promise.all(readPromises)
|
Promise.all(readPromises)
|
||||||
.then(() => resolve(fileDict))
|
.then(() => resolve(fileDict))
|
||||||
@ -130,7 +119,7 @@ function readFilesIntoDict(pattern, prefix) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the provided authentication credentials.
|
* Validates provided credentials.
|
||||||
*
|
*
|
||||||
* @param {string} token
|
* @param {string} token
|
||||||
* @param {string} username
|
* @param {string} username
|
||||||
@ -138,26 +127,17 @@ function readFilesIntoDict(pattern, prefix) {
|
|||||||
* @returns {string|null}
|
* @returns {string|null}
|
||||||
*/
|
*/
|
||||||
function validateAuthentication(token, username, password) {
|
function validateAuthentication(token, username, password) {
|
||||||
if (token) {
|
if (token && (username || password)) {
|
||||||
if (username || password) {
|
return "Token is defined along with username and/or password.";
|
||||||
return "Token is defined along with username and/or password.";
|
}
|
||||||
}
|
if (!token && (!username || !password)) {
|
||||||
} else {
|
return "Username and password must both be defined if token is not used.";
|
||||||
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main execution logic for the Screeps upload action.
|
* Main logic for uploading code to Screeps.
|
||||||
*/
|
*/
|
||||||
function postCode() {
|
function postCode() {
|
||||||
core.info("🟢 Starting Screeps upload action...");
|
core.info("🟢 Starting Screeps upload action...");
|
||||||
@ -201,10 +181,7 @@ function postCode() {
|
|||||||
: Promise.resolve();
|
: Promise.resolve();
|
||||||
|
|
||||||
return replacePromise
|
return replacePromise
|
||||||
.then(() => {
|
.then(() => readFilesIntoDict(pattern, prefix))
|
||||||
core.info("✅ Placeholder replacement complete.");
|
|
||||||
return readFilesIntoDict(pattern, prefix);
|
|
||||||
})
|
|
||||||
.then((files_to_push) => {
|
.then((files_to_push) => {
|
||||||
const fileCount = Object.keys(files_to_push).length;
|
const fileCount = Object.keys(files_to_push).length;
|
||||||
core.info(`📦 Files prepared for upload: ${fileCount}`);
|
core.info(`📦 Files prepared for upload: ${fileCount}`);
|
||||||
@ -219,9 +196,6 @@ function postCode() {
|
|||||||
core.info(` - ${key}`);
|
core.info(` - ${key}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
core.info("🔐 Authentication:");
|
|
||||||
core.info(JSON.stringify(login_arguments, null, 2));
|
|
||||||
|
|
||||||
const api = new ScreepsAPI(login_arguments);
|
const api = new ScreepsAPI(login_arguments);
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
@ -230,7 +204,7 @@ function postCode() {
|
|||||||
core.info(`✅ Code uploaded to branch '${branch}' using token.`);
|
core.info(`✅ Code uploaded to branch '${branch}' using token.`);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
core.info(`Logging in as user '${username}'...`);
|
core.info(`🔐 Logging in as user '${username}'...`);
|
||||||
return api
|
return api
|
||||||
.auth(username, password, login_arguments)
|
.auth(username, password, login_arguments)
|
||||||
.then(() => api.code.set(branch, files_to_push))
|
.then(() => api.code.set(branch, files_to_push))
|
||||||
@ -239,10 +213,9 @@ function postCode() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
core.info("✅ Done Screeps upload action...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the action and catch any errors
|
// Kick off the process and ensure failures are surfaced
|
||||||
postCode().catch((err) => {
|
postCode().catch((err) => {
|
||||||
core.setFailed(err.message || err);
|
core.setFailed(err.message || err);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user