Skip to content

Commit add1c69

Browse files
committed
✨ Use Typescript
1 parent 2ff2808 commit add1c69

File tree

13 files changed

+6259
-372
lines changed

13 files changed

+6259
-372
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@betahuhn/config').eslint
1+
module.exports = require('@betahuhn/config').eslintTypescript

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
.env
2+
.env
3+
test.js

dist/index.js

Lines changed: 0 additions & 277 deletions
This file was deleted.

lib/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { IOpts, InputValue } from './types';
2+
export declare const getInput: (key: string | IOpts, opts: IOpts) => InputValue;

lib/index.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"use strict";
2+
var __assign = (this && this.__assign) || function () {
3+
__assign = Object.assign || function(t) {
4+
for (var s, i = 1, n = arguments.length; i < n; i++) {
5+
s = arguments[i];
6+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7+
t[p] = s[p];
8+
}
9+
return t;
10+
};
11+
return __assign.apply(this, arguments);
12+
};
13+
var __importDefault = (this && this.__importDefault) || function (mod) {
14+
return (mod && mod.__esModule) ? mod : { "default": mod };
15+
};
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
exports.getInput = void 0;
18+
var dotenv_1 = __importDefault(require("dotenv"));
19+
dotenv_1.default.config();
20+
var VALID_TYPES = ['string', 'array', 'boolean'];
21+
var DEFAULT_OPTIONS = {
22+
required: false,
23+
type: 'string',
24+
disableable: false
25+
};
26+
var getEnvVar = function (key) {
27+
var parsed = process.env["INPUT_" + key.replace(/ /g, '_').toUpperCase()];
28+
var raw = process.env[key];
29+
return parsed || raw || undefined;
30+
};
31+
var parseArray = function (val) {
32+
var array = val.split('\n').join(',').split(',');
33+
var filtered = array.filter(function (n) { return n; });
34+
return filtered.map(function (n) { return n.trim(); });
35+
};
36+
var parseBoolean = function (val) {
37+
var trueValue = ['true', 'True', 'TRUE'];
38+
var falseValue = ['false', 'False', 'FALSE'];
39+
if (trueValue.includes(val))
40+
return true;
41+
if (falseValue.includes(val))
42+
return false;
43+
throw new Error('boolean input has to be one of \`true | True | TRUE | false | False | FALSE\`');
44+
};
45+
var parseValue = function (val, type) {
46+
try {
47+
if (type === 'array') {
48+
return parseArray(val);
49+
}
50+
if (type === 'boolean') {
51+
return parseBoolean(val);
52+
}
53+
return val.trim();
54+
}
55+
catch (err) {
56+
return err;
57+
}
58+
};
59+
var getInput = function (key, opts) {
60+
var parsedOptions;
61+
if (typeof key === 'string') {
62+
parsedOptions = __assign({ key: key }, opts);
63+
}
64+
else if (typeof key === 'object') {
65+
parsedOptions = key;
66+
}
67+
else {
68+
throw new Error('No key for input specified');
69+
}
70+
if (!parsedOptions.key)
71+
throw new Error('No key for input specified');
72+
var options = Object.assign({}, DEFAULT_OPTIONS, parsedOptions);
73+
if (VALID_TYPES.includes(options.type) === false)
74+
throw new Error('option type has to be one of `string | array | boolean`');
75+
var val = getEnvVar(options.key);
76+
if (options.disableable && val === 'false')
77+
return undefined;
78+
var parsed = val !== undefined ? parseValue(val, options.type) : undefined;
79+
if (!parsed) {
80+
if (options.required)
81+
throw new Error("Input `" + options.key + "` is required but was not provided.");
82+
if (options.default)
83+
return options.default;
84+
return undefined;
85+
}
86+
if (options.modifier)
87+
return options.modifier(parsed);
88+
return parsed;
89+
};
90+
exports.getInput = getInput;
91+
module.exports.getInput = exports.getInput;

0 commit comments

Comments
 (0)