Skip to content

Commit

Permalink
Fix inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
NekitCorp committed Apr 12, 2023
1 parent f3fc171 commit c57a937
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 19 deletions.
94 changes: 85 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47719,19 +47719,19 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(42186));
const aws_s3_client_1 = __nccwpck_require__(75807);
const s3_uploader_1 = __nccwpck_require__(42134);
const files_manager_1 = __nccwpck_require__(21069);
const getBooleanFromString = (str) => (str === "true" ? true : false);
const inputs_1 = __nccwpck_require__(7063);
const s3_uploader_1 = __nccwpck_require__(42134);
const inputs = {
// required
accessKeyId: core.getInput("access-key-id", { required: true }),
secretAccessKey: core.getInput("secret-access-key", { required: true }),
bucket: core.getInput("bucket", { required: true }),
accessKeyId: (0, inputs_1.getString)({ name: "access-key-id", required: true }),
secretAccessKey: (0, inputs_1.getString)({ name: "secret-access-key", required: true }),
bucket: (0, inputs_1.getString)({ name: "bucket", required: true }),
// optional
workingDirectory: core.getInput("working-directory", { required: false }),
include: core.getMultilineInput("include", { required: false }) || ["**/*"],
exclude: core.getMultilineInput("exclude", { required: false }) || [],
clear: getBooleanFromString(core.getInput("clear", { required: false })),
workingDirectory: (0, inputs_1.getString)({ name: "working-directory", required: false, defaultValue: "" }),
include: (0, inputs_1.getMultiline)({ name: "include", required: false, defaultValue: ["**/*"] }),
exclude: (0, inputs_1.getMultiline)({ name: "exclude", required: false, defaultValue: [] }),
clear: (0, inputs_1.getBoolean)({ name: "clear", required: false, defaultValue: false }),
};
const s3Uploader = new s3_uploader_1.S3Uploader(new aws_s3_client_1.AWSS3Client({
accessKeyId: inputs.accessKeyId,
Expand Down Expand Up @@ -47837,6 +47837,82 @@ class FilesManager {
exports.FilesManager = FilesManager;


/***/ }),

/***/ 7063:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getMultiline = exports.getBoolean = exports.getString = void 0;
const core = __importStar(__nccwpck_require__(42186));
function getString(options) {
const { name, required } = options;
if (required) {
return core.getInput(name, { required });
}
else {
return core.getInput(name, { required }) || options.defaultValue;
}
}
exports.getString = getString;
function getBoolean(options) {
const { name, required } = options;
if (required) {
return core.getBooleanInput(name, { required });
}
else {
try {
return core.getBooleanInput(name, { required });
}
catch (error) {
return options.defaultValue;
}
}
}
exports.getBoolean = getBoolean;
function getMultiline(options) {
const { name, required } = options;
if (required) {
return core.getMultilineInput(name, { required });
}
else {
const value = core.getMultilineInput(name, { required });
if (value.length === 0) {
return options.defaultValue;
}
else {
return value;
}
}
}
exports.getMultiline = getMultiline;


/***/ }),

/***/ 42134:
Expand Down
19 changes: 9 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import * as core from "@actions/core";
import { AWSS3Client } from "./src/aws-s3-client";
import { S3Uploader } from "./src/s3-uploader";
import { FilesManager } from "./src/files-manager";

const getBooleanFromString = (str: string): boolean => (str === "true" ? true : false);
import { getBoolean, getMultiline, getString } from "./src/inputs";
import { S3Uploader } from "./src/s3-uploader";

const inputs: Inputs = {
// required
accessKeyId: core.getInput("access-key-id", { required: true }),
secretAccessKey: core.getInput("secret-access-key", { required: true }),
bucket: core.getInput("bucket", { required: true }),
accessKeyId: getString({ name: "access-key-id", required: true }),
secretAccessKey: getString({ name: "secret-access-key", required: true }),
bucket: getString({ name: "bucket", required: true }),
// optional
workingDirectory: core.getInput("working-directory", { required: false }),
include: core.getMultilineInput("include", { required: false }) || ["**/*"],
exclude: core.getMultilineInput("exclude", { required: false }) || [],
clear: getBooleanFromString(core.getInput("clear", { required: false })),
workingDirectory: getString({ name: "working-directory", required: false, defaultValue: "" }),
include: getMultiline({ name: "include", required: false, defaultValue: ["**/*"] }),
exclude: getMultiline({ name: "exclude", required: false, defaultValue: [] }),
clear: getBoolean({ name: "clear", required: false, defaultValue: false }),
};

const s3Uploader = new S3Uploader(
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/inputs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { beforeEach, describe, expect, it } from "vitest";
import { getBoolean, getMultiline, getString } from "../inputs";

const testEnvVars = {
// Set inputs
INPUT_STRING: "string value",
INPUT_BOOLEAN_TRUE: "true",
INPUT_BOOLEAN_FALSE: "false",
INPUT_LIST: "val1\nval2\nval3",
};

describe("@actions/core", () => {
beforeEach(() => {
for (const key in testEnvVars) {
process.env[key] = testEnvVars[key as keyof typeof testEnvVars];
}
});

describe("get inputs", () => {
it("getString", async () => {
expect(getString({ name: "string", required: true })).toBe("string value");
expect(() => getString({ name: "string required", required: true })).toThrowError();
expect(getString({ name: "string optional", required: false, defaultValue: "default" })).toBe("default");
});

it("getBooleanInput", async () => {
expect(getBoolean({ name: "boolean true", required: true })).toBe(true);
expect(getBoolean({ name: "boolean false", required: true })).toBe(false);
expect(() => getBoolean({ name: "boolean required", required: true })).toThrowError();
expect(getBoolean({ name: "boolean optional", required: false, defaultValue: false })).toBe(false);
});

it("getMultiline", async () => {
expect(getMultiline({ name: "list", required: true })).toMatchObject(["val1", "val2", "val3"]);
expect(() => getMultiline({ name: "list required", required: true })).toThrowError();
expect(getMultiline({ name: "list optional", required: false, defaultValue: ["default"] })).toMatchObject(["default"]);
});
});
});
47 changes: 47 additions & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as core from "@actions/core";

type GetStringOptions = { name: string; required: true } | { name: string; required: false; defaultValue: string };

export function getString(options: GetStringOptions): string {
const { name, required } = options;

if (required) {
return core.getInput(name, { required });
} else {
return core.getInput(name, { required }) || options.defaultValue;
}
}

type GetBooleanOptions = { name: string; required: true } | { name: string; required: false; defaultValue: boolean };

export function getBoolean(options: GetBooleanOptions): boolean {
const { name, required } = options;

if (required) {
return core.getBooleanInput(name, { required });
} else {
try {
return core.getBooleanInput(name, { required });
} catch (error) {
return options.defaultValue;
}
}
}

type GetMultilineOptions = { name: string; required: true } | { name: string; required: false; defaultValue: string[] };

export function getMultiline(options: GetMultilineOptions): string[] {
const { name, required } = options;

if (required) {
return core.getMultilineInput(name, { required });
} else {
const value = core.getMultilineInput(name, { required });

if (value.length === 0) {
return options.defaultValue;
} else {
return value;
}
}
}

0 comments on commit c57a937

Please sign in to comment.