github-pages-deploy-action/lib/util.js

72 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2020-03-02 22:38:52 +08:00
"use strict";
2020-10-18 03:22:15 +08:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2020-03-02 22:38:52 +08:00
Object.defineProperty(exports, "__esModule", { value: true });
2020-10-18 03:22:15 +08:00
exports.suppressSensitiveInformation = exports.checkParameters = exports.generateFolderPath = exports.generateRepositoryPath = exports.generateTokenType = exports.isNullOrUndefined = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
2020-03-02 22:38:52 +08:00
const core_1 = require("@actions/core");
2020-10-18 03:22:15 +08:00
/* Replaces all instances of a match in a string. */
2020-09-13 06:19:45 +08:00
const replaceAll = (input, find, replace) => input.split(find).join(replace);
2020-03-02 22:38:52 +08:00
/* Utility function that checks to see if a value is undefined or not. */
2020-03-07 11:45:40 +08:00
exports.isNullOrUndefined = (value) => typeof value === 'undefined' || value === null || value === '';
2020-03-02 22:38:52 +08:00
/* Generates a token type used for the action. */
exports.generateTokenType = (action) => action.ssh
2020-03-07 11:45:40 +08:00
? 'SSH Deploy Key'
2020-03-02 22:38:52 +08:00
: action.accessToken
2020-03-07 11:45:40 +08:00
? 'Access Token'
2020-03-02 22:38:52 +08:00
: action.gitHubToken
2020-03-07 11:45:40 +08:00
? 'GitHub Token'
2020-05-15 05:33:08 +08:00
: '…';
2020-03-02 22:38:52 +08:00
/* Generates a the repository path used to make the commits. */
exports.generateRepositoryPath = (action) => action.ssh
? `git@github.com:${action.repositoryName}`
2020-05-25 00:39:12 +08:00
: `https://${action.accessToken || `x-access-token:${action.gitHubToken}`}@github.com/${action.repositoryName}.git`;
2020-10-18 03:22:15 +08:00
/* Genetate absolute folder path by the provided folder name */
exports.generateFolderPath = (action) => {
const folderName = action['folder'];
return path_1.default.isAbsolute(folderName)
? folderName
: folderName.startsWith('~')
? folderName.replace('~', process.env.HOME)
: path_1.default.join(action.workspace, folderName);
};
2020-03-02 22:38:52 +08:00
/* Checks for the required tokens and formatting. Throws an error if any case is matched. */
2020-10-18 03:22:15 +08:00
const hasRequiredParameters = (action, params) => {
const nonNullParams = params.filter(param => !exports.isNullOrUndefined(action[param]));
return Boolean(nonNullParams.length);
};
/* Verifies the action has the required parameters to run, otherwise throw an error. */
exports.checkParameters = (action) => {
if (!hasRequiredParameters(action, ['accessToken', 'gitHubToken', 'ssh'])) {
2020-03-07 11:45:40 +08:00
throw new Error('No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.');
2020-03-02 22:38:52 +08:00
}
2020-10-18 03:22:15 +08:00
if (!hasRequiredParameters(action, ['branch'])) {
2020-03-07 11:45:40 +08:00
throw new Error('Branch is required.');
2020-03-02 22:38:52 +08:00
}
2020-10-18 03:22:15 +08:00
if (!hasRequiredParameters(action, ['folder'])) {
2020-03-07 11:45:40 +08:00
throw new Error('You must provide the action with a folder to deploy.');
2020-03-02 22:38:52 +08:00
}
2020-10-18 03:22:15 +08:00
if (!fs_1.existsSync(action.folderPath)) {
throw new Error(`The directory you're trying to deploy named ${action.folderPath} doesn't exist. Please double check the path and any prerequisite build scripts and try again. ❗`);
2020-03-02 22:38:52 +08:00
}
};
/* Suppresses sensitive information from being exposed in error messages. */
exports.suppressSensitiveInformation = (str, action) => {
let value = str;
2020-03-31 20:48:28 +08:00
if (core_1.isDebug()) {
2020-03-02 22:38:52 +08:00
// Data is unmasked in debug mode.
return value;
}
2020-09-13 06:19:45 +08:00
const orderedByLength = [
action.accessToken,
action.gitHubToken,
action.repositoryPath
].filter(Boolean).sort((a, b) => b.length - a.length);
for (const find of orderedByLength) {
value = replaceAll(value, find, '***');
2020-03-02 22:38:52 +08:00
}
return value;
};