diff --git a/index.js b/index.js index 24b864b..ccc8c1b 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const { glob } = require("glob"); 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} 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} [prefix] @@ -41,14 +41,12 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) { return new Promise((resolve, reject) => { const globPattern = prefix ? path.join(prefix, pattern) : pattern; - glob(globPattern, async (err, files) => { - if (err) { - return reject(err); - } + glob(globPattern, (err, files) => { + if (err) return reject(err); if (!files.length) { core.warning( - `No files matched for placeholder replacement with pattern: ${globPattern}`, + `No files matched for placeholder replacement: ${globPattern}`, ); return resolve([]); } @@ -56,71 +54,62 @@ function readReplaceAndWriteFiles(pattern, prefix, hostname) { core.info(`📝 Matched files for replacement:`); files.forEach((file) => core.info(` - ${file}`)); - let processPromises = []; - - files.forEach((file) => { - core.info(`Replacing placeholders in file: ${file}`); - let processPromise = fs.promises + const promises = files.map((file) => + fs.promises .readFile(file, "utf8") .then((content) => { content = replacePlaceholders(content, hostname); return fs.promises.writeFile(file, content); - }); + }) + .then(() => { + core.info(`Replaced placeholders in file: ${file}`); + }), + ); - processPromises.push(processPromise); - }); - - try { - await Promise.all(processPromises); - core.info(`Replaced placeholders in ${files.length} file(s).`); - resolve(files); - } catch (processError) { - reject(processError); - } + Promise.all(promises) + .then(() => { + core.info( + `Successfully replaced placeholders in ${files.length} file(s).`, + ); + resolve(files); + }) + .catch(reject); }); }); } /** - * Reads files matching a glob pattern into a dictionary. + * Reads files into a dictionary of key → content. * * @param {string} pattern * @param {string} prefix * @returns {Promise} */ function readFilesIntoDict(pattern, prefix) { - const globPattern = prefix ? path.join(prefix, pattern) : pattern; - return new Promise((resolve, reject) => { + const globPattern = prefix ? path.join(prefix, pattern) : pattern; + glob(globPattern, (err, files) => { - if (err) { - return reject(err); - } + if (err) return reject(err); if (!files.length) { - core.warning( - `No files matched for upload with pattern: ${globPattern}`, - ); + core.warning(`No files matched for upload: ${globPattern}`); } else { core.info(`📁 Matched files for upload:`); files.forEach((file) => core.info(` - ${file}`)); } - let fileDict = {}; - let readPromises = []; - - files.forEach((file) => { - let readPromise = fs.promises.readFile(file, "utf8").then((content) => { + const fileDict = {}; + const readPromises = files.map((file) => + fs.promises.readFile(file, "utf8").then((content) => { let key = file; if (prefix && file.startsWith(prefix)) { key = key.slice(prefix.length); } key = path.basename(key, path.extname(key)); fileDict[key] = content; - }); - - readPromises.push(readPromise); - }); + }), + ); Promise.all(readPromises) .then(() => resolve(fileDict)) @@ -130,7 +119,7 @@ function readFilesIntoDict(pattern, prefix) { } /** - * Validates the provided authentication credentials. + * Validates provided credentials. * * @param {string} token * @param {string} username @@ -138,26 +127,17 @@ function readFilesIntoDict(pattern, prefix) { * @returns {string|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 && (username || password)) { + return "Token is defined along with username and/or password."; + } + if (!token && (!username || !password)) { + return "Username and password must both be defined if token is not used."; } return null; } /** - * Main execution logic for the Screeps upload action. + * Main logic for uploading code to Screeps. */ function postCode() { core.info("🟢 Starting Screeps upload action..."); @@ -201,10 +181,7 @@ function postCode() { : Promise.resolve(); return replacePromise - .then(() => { - core.info("✅ Placeholder replacement complete."); - return readFilesIntoDict(pattern, prefix); - }) + .then(() => readFilesIntoDict(pattern, prefix)) .then((files_to_push) => { const fileCount = Object.keys(files_to_push).length; core.info(`📦 Files prepared for upload: ${fileCount}`); @@ -219,9 +196,6 @@ function postCode() { core.info(` - ${key}`); }); - core.info("🔐 Authentication:"); - core.info(JSON.stringify(login_arguments, null, 2)); - const api = new ScreepsAPI(login_arguments); if (token) { @@ -230,7 +204,7 @@ function postCode() { core.info(`✅ Code uploaded to branch '${branch}' using token.`); }); } else { - core.info(`Logging in as user '${username}'...`); + core.info(`🔐 Logging in as user '${username}'...`); return api .auth(username, password, login_arguments) .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) => { core.setFailed(err.message || err); });