First development of the deploy action #6

Merged
Philipp merged 14 commits from develop-action into main 2023-11-26 18:31:53 +01:00
1039 changed files with 228399 additions and 0 deletions
Showing only changes of commit 5d6e23178e - Show all commits

View File

@ -1,33 +1,33 @@
name: 'Upload to Screeps' name: Upload to Screeps
description: 'This action uploads code to the Screeps server.' description: This action uploads code to the Screeps server.
inputs: inputs:
protocol: protocol:
description: 'The protocol to use (default: https).' description: 'The protocol to use (default: https).'
required: false required: false
default: 'https' default: https
hostname: hostname:
description: 'The hostname of the Screeps server (default: screeps.com).' description: 'The hostname of the Screeps server (default: screeps.com).'
required: false required: false
default: 'screeps.com' default: screeps.com
port: port:
description: 'The port to use (default: 443).' description: 'The port to use (default: 443).'
required: false required: false
default: '443' default: '443'
path: path:
description: 'The path for the API.' description: The path for the API.
required: false required: false
default: '/' default: /
token: token:
description: 'Authentication token for Screeps.' description: Authentication token for Screeps.
required: true required: true
username: username:
description: 'Username for Screeps account. Used if no token is provided.' description: Username for Screeps account. Used if no token is provided.
required: false required: false
password: password:
description: 'Password for Screeps account. Used if no token is provided.' description: Password for Screeps account. Used if no token is provided.
required: false required: false
prefix: prefix:
description: 'Directory prefix for file paths.' description: Directory prefix for file paths.
required: false required: false
pattern: pattern:
description: 'Glob pattern to match files (default: *.js).' description: 'Glob pattern to match files (default: *.js).'
@ -36,7 +36,7 @@ inputs:
branch: branch:
description: 'Branch in Screeps to which the code will be uploaded (default: default).' description: 'Branch in Screeps to which the code will be uploaded (default: default).'
required: false required: false
default: 'default' default: default
runs: runs:
using: 'node12' using: node12
main: 'index.js' main: index.js

165
index.js
View File

@ -1,8 +1,8 @@
const { ScreepsAPI } = require('screeps-api'); const { ScreepsAPI } = require("screeps-api");
const core = require("@actions/core"); const core = require("@actions/core");
const fs = require('fs'); const fs = require("fs");
const glob = require('glob'); const glob = require("glob");
const path = require('path'); const path = require("path");
/** /**
* Reads files matching a glob pattern into a dictionary. * Reads files matching a glob pattern into a dictionary.
@ -11,40 +11,39 @@ const path = require('path');
* @returns {Promise<Object>} - Promise resolving to a dictionary of file contents keyed by filenames. * @returns {Promise<Object>} - Promise resolving to a dictionary of file contents keyed by filenames.
*/ */
function readFilesIntoDict(pattern, prefix) { function readFilesIntoDict(pattern, prefix) {
// Prepend the prefix to the glob pattern // Prepend the prefix to the glob pattern
const globPattern = prefix ? path.join(prefix, pattern) : pattern; const globPattern = prefix ? path.join(prefix, pattern) : pattern;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
glob(globPattern, (err, files) => { glob(globPattern, (err, files) => {
if (err) { if (err) {
return reject(err); return reject(err);
} }
let fileDict = {}; let fileDict = {};
let readPromises = []; let readPromises = [];
files.forEach(file => { files.forEach((file) => {
let readPromise = fs.promises.readFile(file, 'utf8') let readPromise = fs.promises.readFile(file, "utf8").then((content) => {
.then(content => { // Remove the prefix from the filename and drop the file suffix
// Remove the prefix from the filename and drop the file suffix 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)); // Drop the file suffix
key = path.basename(key, path.extname(key)); // Drop the file suffix
fileDict[key] = content; fileDict[key] = content;
});
readPromises.push(readPromise);
});
// Use Promise.all to ensure all files are read before resolving
Promise.all(readPromises)
.then(() => resolve(fileDict))
.catch(reject);
}); });
readPromises.push(readPromise);
});
// Use Promise.all to ensure all files are read before resolving
Promise.all(readPromises)
.then(() => resolve(fileDict))
.catch(reject);
}); });
});
} }
/** /**
@ -55,72 +54,72 @@ function readFilesIntoDict(pattern, prefix) {
* @returns {string|null} - Returns an error message if validation fails, otherwise null. * @returns {string|null} - Returns an error message if validation fails, otherwise null.
*/ */
function validateAuthentication(token, username, password) { function validateAuthentication(token, username, password) {
if (token) { if (token) {
if (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.";
}
} 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 } 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. * Posts code to Screeps server.
*/ */
async function postCode() { async 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";
const path = core.getInput("path") || "/"; const path = core.getInput("path") || "/";
const token = core.getInput("token") || undefined; const token = core.getInput("token") || undefined;
const username = core.getInput("username") || undefined; const username = core.getInput("username") || undefined;
const password = core.getInput("password") || undefined; const password = core.getInput("password") || undefined;
const prefix = core.getInput("source-prefix"); const prefix = core.getInput("source-prefix");
const pattern = core.getInput("pattern") || "*.js"; const pattern = core.getInput("pattern") || "*.js";
const branch = core.getInput("branch") || "default"; const branch = core.getInput("branch") || "default";
const files_to_push = await readFilesIntoDict(pattern, prefix); const files_to_push = await readFilesIntoDict(pattern, prefix);
Object.keys(files_to_push).forEach(key => { Object.keys(files_to_push).forEach((key) => {
core.info(`Key: ${key}`); core.info(`Key: ${key}`);
}); });
core.info(files_to_push); core.info(files_to_push);
const login_arguments = { const login_arguments = {
"token": token, token: token,
"username": username, username: username,
"password": password, password: password,
"protocol": protocol, protocol: protocol,
"hostname": hostname, hostname: hostname,
"port": port, port: port,
"path": path, path: path,
}; };
core.info(`Trying to upload the following files to ${branch}:`) core.info(`Trying to upload the following files to ${branch}:`);
Object.keys(login_arguments).forEach(key => { Object.keys(login_arguments).forEach((key) => {
core.info(`Key: ${key}`); core.info(`Key: ${key}`);
}); });
const errorMessage = validateAuthentication(token, username, password); const errorMessage = validateAuthentication(token, username, password);
if (errorMessage) { if (errorMessage) {
core.error(errorMessage); core.error(errorMessage);
return; return;
} }
const api = new ScreepsAPI(login_arguments); const api = new ScreepsAPI(login_arguments);
const response = await api.code.set(branch, files_to_push); const response = await api.code.set(branch, files_to_push);
core.info(JSON.stringify(response, null, 2)); core.info(JSON.stringify(response, null, 2));
} }
postCode(); postCode();

1746
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,14 @@
{ {
"name": "screeps-deploy-action", "name": "screeps-deploy-action",
"version": "0.1.0", "version": "0.1.0",
"description": "Deploys screeps code to the official game or an pirvate server.", "description": "Deploys screeps code to the official game or an pirvate server.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js" "start": "node index.js"
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"glob": "^7.1.6", "glob": "^7.1.6",
"screeps-api": "^1.7.2" "screeps-api": "^1.7.2"
} }
} }