From 37d0a8930165aaa264d849838ea2194c1ab87ab1 Mon Sep 17 00:00:00 2001 From: shimataro Date: Sun, 30 Oct 2022 17:35:51 +0900 Subject: [PATCH] Change code style to 1TBS --- .editorconfig | 7 ++ .eslintrc.yml | 19 +--- lib/index.js | 118 ++++++++++++------------ src/main.ts | 249 +++++++++++++++++++++++--------------------------- 4 files changed, 186 insertions(+), 207 deletions(-) diff --git a/.editorconfig b/.editorconfig index 0d46254..b40d053 100644 --- a/.editorconfig +++ b/.editorconfig @@ -25,6 +25,13 @@ spaces_around_brackets = none indent_brace_style = allman +# JavaScript/TypeScript +[*.{js,ts}] +indent_style = space +curly_bracket_next_line = false +indent_brace_style = K&R + + # JSON/YAML [*.{json,babelrc,code-workspace,yml,yaml}] indent_style = space diff --git a/.eslintrc.yml b/.eslintrc.yml index 7cea633..293fe7b 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -31,7 +31,7 @@ rules: # https://eslint.org/docs/rules/ block-spacing: error brace-style: - error - - allman + - 1tbs callback-return: error capitalized-comments: 'off' class-methods-use-this: error @@ -87,26 +87,14 @@ rules: # https://eslint.org/docs/rules/ - below indent: - error - - tab + - 4 + - SwitchCase: 1 indent-legacy: 'off' init-declarations: error jsx-quotes: error key-spacing: error keyword-spacing: - error - - overrides: - catch: - after: false - for: - after: false - if: - after: false - switch: - after: false - while: - after: false - with: - after: false line-comment-position: 'off' linebreak-style: - error @@ -309,6 +297,7 @@ rules: # https://eslint.org/docs/rules/ # @typescript-eslint plugin "@typescript-eslint/ban-ts-ignore": 'off' "@typescript-eslint/no-empty-interface": 'off' + "@typescript-eslint/no-floating-promises": error "@typescript-eslint/no-use-before-define": - error - functions: false diff --git a/lib/index.js b/lib/index.js index 88bd249..d52da62 100644 --- a/lib/index.js +++ b/lib/index.js @@ -590,66 +590,61 @@ __nccwpck_require__.r(__webpack_exports__); * main function */ function main() { - try { - // parameters - const key = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("key", { - required: true, + // parameters + const key = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("key", { + required: true, + }); + const name = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("name"); + const knownHosts = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("known_hosts", { + required: true, + }); + const config = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("config"); + const ifKeyExists = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("if_key_exists"); + // create ".ssh" directory + const home = getHomeDirectory(); + const dirName = path__WEBPACK_IMPORTED_MODULE_1___default().resolve(home, ".ssh"); + fs__WEBPACK_IMPORTED_MODULE_0___default().mkdirSync(dirName, { + recursive: true, + mode: 0o700, + }); + // files to be created + const files = []; + if (shouldCreateKeyFile(path__WEBPACK_IMPORTED_MODULE_1___default().join(dirName, name), ifKeyExists)) { + files.push({ + name: name, + contents: insertLf(key, false, true), + options: { + mode: 0o400, + flag: "wx", + }, }); - const name = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("name"); - const knownHosts = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("known_hosts", { - required: true, - }); - const config = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("config"); - const ifKeyExists = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("if_key_exists"); - // create ".ssh" directory - const home = getHomeDirectory(); - const dirName = path__WEBPACK_IMPORTED_MODULE_1___default().resolve(home, ".ssh"); - fs__WEBPACK_IMPORTED_MODULE_0___default().mkdirSync(dirName, { - recursive: true, - mode: 0o700, - }); - // files to be created - const files = []; - if (shouldCreateKeyFile(path__WEBPACK_IMPORTED_MODULE_1___default().join(dirName, name), ifKeyExists)) { - files.push({ - name: name, - contents: insertLf(key, false, true), - options: { - mode: 0o400, - flag: "wx", - }, - }); - } - if (knownHosts !== "unnecessary") { - files.push({ - name: "known_hosts", - contents: insertLf(knownHosts, true, true), - options: { - mode: 0o644, - flag: "a", - }, - }); - } - if (config !== "") { - files.push({ - name: "config", - contents: insertLf(config, true, true), - options: { - mode: 0o644, - flag: "a", - }, - }); - } - // create files - for (const file of files) { - const fileName = path__WEBPACK_IMPORTED_MODULE_1___default().join(dirName, file.name); - fs__WEBPACK_IMPORTED_MODULE_0___default().writeFileSync(fileName, file.contents, file.options); - } - console.log(`SSH key has been stored to ${dirName} successfully.`); } - catch (err) { - _actions_core__WEBPACK_IMPORTED_MODULE_2__.setFailed(err.message); + if (knownHosts !== "unnecessary") { + files.push({ + name: "known_hosts", + contents: insertLf(knownHosts, true, true), + options: { + mode: 0o644, + flag: "a", + }, + }); } + if (config !== "") { + files.push({ + name: "config", + contents: insertLf(config, true, true), + options: { + mode: 0o644, + flag: "a", + }, + }); + } + // create files + for (const file of files) { + const fileName = path__WEBPACK_IMPORTED_MODULE_1___default().join(dirName, file.name); + fs__WEBPACK_IMPORTED_MODULE_0___default().writeFileSync(fileName, file.contents, file.options); + } + console.log(`SSH key has been stored to ${dirName} successfully.`); } /** * get home directory @@ -724,7 +719,14 @@ function shouldCreateKeyFile(keyFilePath, ifKeyExists) { throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`); } } -main(); +try { + main(); +} +catch (err) { + if (err instanceof Error) { + _actions_core__WEBPACK_IMPORTED_MODULE_2__.setFailed(err); + } +} })(); diff --git a/src/main.ts b/src/main.ts index 737ec57..c1d5df4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,126 +3,108 @@ import path from "path"; import * as core from "@actions/core"; -interface FileInfo -{ - name: string; - contents: string; - options: fs.WriteFileOptions; +interface FileInfo { + name: string; + contents: string; + options: fs.WriteFileOptions; } /** * main function */ -function main(): void -{ - try - { - // parameters - const key = core.getInput("key", { - required: true, - }); - const name = core.getInput("name"); - const knownHosts = core.getInput("known_hosts", { - required: true, - }); - const config = core.getInput("config"); - const ifKeyExists = core.getInput("if_key_exists"); +function main(): void { + // parameters + const key = core.getInput("key", { + required: true, + }); + const name = core.getInput("name"); + const knownHosts = core.getInput("known_hosts", { + required: true, + }); + const config = core.getInput("config"); + const ifKeyExists = core.getInput("if_key_exists"); - // create ".ssh" directory - const home = getHomeDirectory(); - const dirName = path.resolve(home, ".ssh"); - fs.mkdirSync(dirName, { - recursive: true, - mode: 0o700, - }); + // create ".ssh" directory + const home = getHomeDirectory(); + const dirName = path.resolve(home, ".ssh"); + fs.mkdirSync(dirName, { + recursive: true, + mode: 0o700, + }); - // files to be created - const files: FileInfo[] = []; - if(shouldCreateKeyFile(path.join(dirName, name), ifKeyExists)) - { - files.push({ - name: name, - contents: insertLf(key, false, true), - options: { - mode: 0o400, - flag: "wx", - }, - }); - } - if(knownHosts !== "unnecessary") - { - files.push({ - name: "known_hosts", - contents: insertLf(knownHosts, true, true), - options: { - mode: 0o644, - flag: "a", - }, - }); - } - if(config !== "") - { - files.push({ - name: "config", - contents: insertLf(config, true, true), - options: { - mode: 0o644, - flag: "a", - }, - }); - } + // files to be created + const files: FileInfo[] = []; + if (shouldCreateKeyFile(path.join(dirName, name), ifKeyExists)) { + files.push({ + name: name, + contents: insertLf(key, false, true), + options: { + mode: 0o400, + flag: "wx", + }, + }); + } + if (knownHosts !== "unnecessary") { + files.push({ + name: "known_hosts", + contents: insertLf(knownHosts, true, true), + options: { + mode: 0o644, + flag: "a", + }, + }); + } + if (config !== "") { + files.push({ + name: "config", + contents: insertLf(config, true, true), + options: { + mode: 0o644, + flag: "a", + }, + }); + } - // create files - for(const file of files) - { - const fileName = path.join(dirName, file.name); - fs.writeFileSync(fileName, file.contents, file.options); - } + // create files + for (const file of files) { + const fileName = path.join(dirName, file.name); + fs.writeFileSync(fileName, file.contents, file.options); + } - console.log(`SSH key has been stored to ${dirName} successfully.`); - } - catch(err) - { - core.setFailed(err.message); - } + console.log(`SSH key has been stored to ${dirName} successfully.`); } /** * get home directory * @returns home directory */ -function getHomeDirectory(): string -{ - const homeEnv = getHomeEnv(); - const home = process.env[homeEnv]; - if(home === undefined) - { - throw Error(`${homeEnv} is not defined`); - } +function getHomeDirectory(): string { + const homeEnv = getHomeEnv(); + const home = process.env[homeEnv]; + if (home === undefined) { + throw Error(`${homeEnv} is not defined`); + } - if(home === "/github/home") - { - // Docker container - return "/root"; - } + if (home === "/github/home") { + // Docker container + return "/root"; + } - return home; + return home; } /** * get HOME environment name * @returns HOME environment name */ -function getHomeEnv(): string -{ - if(process.platform === "win32") - { - // Windows - return "USERPROFILE"; - } +function getHomeEnv(): string { + if (process.platform === "win32") { + // Windows + return "USERPROFILE"; + } - // macOS / Linux - return "HOME"; + // macOS / Linux + return "HOME"; } /** @@ -132,25 +114,21 @@ function getHomeEnv(): string * @param append true to append * @returns new value */ -function insertLf(value: string, prepend: boolean, append: boolean): string -{ - let affectedValue = value; +function insertLf(value: string, prepend: boolean, append: boolean): string { + let affectedValue = value; - if(value.length === 0) - { - // do nothing if empty - return ""; - } - if(prepend && !affectedValue.startsWith("\n")) - { - affectedValue = `\n${affectedValue}`; - } - if(append && !affectedValue.endsWith("\n")) - { - affectedValue = `${affectedValue}\n`; - } + if (value.length === 0) { + // do nothing if empty + return ""; + } + if (prepend && !affectedValue.startsWith("\n")) { + affectedValue = `\n${affectedValue}`; + } + if (append && !affectedValue.endsWith("\n")) { + affectedValue = `${affectedValue}\n`; + } - return affectedValue; + return affectedValue; } /** @@ -159,29 +137,32 @@ function insertLf(value: string, prepend: boolean, append: boolean): string * @param ifKeyExists action if SSH key exists * @returns Yes/No */ -function shouldCreateKeyFile(keyFilePath: string, ifKeyExists: string): boolean -{ - if(!fs.existsSync(keyFilePath)) - { - // should create if file does not exist - return true; - } +function shouldCreateKeyFile(keyFilePath: string, ifKeyExists: string): boolean { + if (!fs.existsSync(keyFilePath)) { + // should create if file does not exist + return true; + } - switch(ifKeyExists) - { - case "replace": - // remove file and should create if replace - fs.unlinkSync(keyFilePath); - return true; + switch (ifKeyExists) { + case "replace": + // remove file and should create if replace + fs.unlinkSync(keyFilePath); + return true; - case "ignore": - // should NOT create if ignore - return false; + case "ignore": + // should NOT create if ignore + return false; - default: - // error otherwise - throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`); - } + default: + // error otherwise + throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`); + } } -main(); +try { + main(); +} catch (err) { + if (err instanceof Error) { + core.setFailed(err); + } +}