79 lines
2.2 KiB
TypeScript
Raw Normal View History

import { getInput } from "@actions/core";
2019-09-09 21:20:59 +09:00
import * as glob from "glob";
import { lstatSync, readFileSync } from "fs";
2019-09-09 17:10:07 +09:00
export interface Config {
2019-09-09 21:20:59 +09:00
github_token: string;
github_ref: string;
github_repository: string;
// user provided
input_name?: string;
input_tag_name?: string;
2020-12-20 14:44:30 -05:00
input_repository?: string;
2019-09-09 21:20:59 +09:00
input_body?: string;
input_body_path?: string;
input_files?: string[];
input_draft?: boolean;
2019-09-17 23:30:36 +09:00
input_prerelease?: boolean;
input_fail_on_unmatched_files?: boolean;
2019-09-09 17:10:07 +09:00
}
export const releaseBody = (config: Config): string | undefined => {
return (
(config.input_body_path &&
readFileSync(config.input_body_path).toString("utf8")) ||
config.input_body
);
};
2019-09-09 17:10:07 +09:00
type Env = { [key: string]: string | undefined };
export const parseInputFiles = (files: string): string[] => {
return files.split(/\r?\n/).reduce<string[]>(
(acc, line) =>
acc
.concat(line.split(","))
.filter(pat => pat)
.map(pat => pat.trim()),
[]
);
};
2019-09-09 17:10:07 +09:00
export const parseConfig = (env: Env): Config => {
return {
github_token: getInput("token") || env.GITHUB_TOKEN || "",
2019-09-09 21:20:59 +09:00
github_ref: env.GITHUB_REF || "",
2020-12-20 14:44:30 -05:00
github_repository: env.INPUT_REPOSITORY || env.GITHUB_REPOSITORY || "",
2019-09-09 17:10:07 +09:00
input_name: env.INPUT_NAME,
input_tag_name: env.INPUT_TAG_NAME,
2019-09-09 17:10:07 +09:00
input_body: env.INPUT_BODY,
input_body_path: env.INPUT_BODY_PATH,
input_files: parseInputFiles(env.INPUT_FILES || ""),
2019-09-17 23:30:36 +09:00
input_draft: env.INPUT_DRAFT === "true",
input_prerelease: env.INPUT_PRERELEASE == "true",
input_fail_on_unmatched_files: env.INPUT_FAIL_ON_UNMATCHED_FILES == "true"
2019-09-09 21:20:59 +09:00
};
};
2019-09-09 17:10:07 +09:00
export const paths = (patterns: string[]): string[] => {
return patterns.reduce((acc: string[], pattern: string): string[] => {
2019-09-09 21:20:59 +09:00
return acc.concat(
glob.sync(pattern).filter(path => lstatSync(path).isFile())
);
}, []);
};
2019-09-09 17:10:07 +09:00
export const unmatchedPatterns = (patterns: string[]): string[] => {
return patterns.reduce((acc: string[], pattern: string): string[] => {
return acc.concat(
glob.sync(pattern).filter(path => lstatSync(path).isFile()).length == 0
? [pattern]
: []
);
}, []);
};
2019-09-09 17:10:07 +09:00
export const isTag = (ref: string): boolean => {
2019-09-09 21:20:59 +09:00
return ref.startsWith("refs/tags/");
};