From a31de8476de035268f10afae0c67e7bba50d0c82 Mon Sep 17 00:00:00 2001 From: Paul Merlin Date: Sat, 13 Jun 2020 13:44:30 +0200 Subject: [PATCH] Format --- .prettierrc.json | 2 +- __tests__/main.test.ts | 4 +- package.json | 2 +- src/execution.ts | 32 ++++--- src/gradlew.ts | 8 +- src/main.ts | 58 ++++++------ src/post.ts | 6 +- src/provision.ts | 195 +++++++++++++++++++++-------------------- 8 files changed, 148 insertions(+), 159 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index bbf4311..c7cf5c8 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,6 +1,6 @@ { "printWidth": 80, - "tabWidth": 2, + "tabWidth": 4, "useTabs": false, "semi": false, "singleQuote": true, diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 48f15ff..9228bf9 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,3 +1,3 @@ describe('TODO - Add a test suite', () => { - it('TODO - Add a test', async () => {}); -}); + it('TODO - Add a test', async () => {}) +}) diff --git a/package.json b/package.json index 370c5b7..bba3478 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint": "eslint src/**/*.ts", "build": "ncc build src/main.ts --out dist/main --minify && ncc build src/post.ts --out dist/post --minify", "test": "jest", - "all": "npm run build && npm test" + "all": "npm run format && npm run build && npm test" }, "repository": { "type": "git", diff --git a/src/execution.ts b/src/execution.ts index dc1364d..021b266 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -1,31 +1,33 @@ -import * as exec from "@actions/exec"; +import * as exec from '@actions/exec' - -export async function execute(executable: string, root: string, argv: string[]): Promise { - - let publishing = false; - let buildScanUrl: string | undefined; +export async function execute( + executable: string, + root: string, + argv: string[] +): Promise { + let publishing = false + let buildScanUrl: string | undefined const status: number = await exec.exec(executable, argv, { cwd: root, ignoreReturnCode: true, listeners: { stdline: (line: string) => { - if (line.startsWith("Publishing build scan...")) { - publishing = true; + if (line.startsWith('Publishing build scan...')) { + publishing = true } if (publishing && line.length == 0) { publishing = false } - if (publishing && line.startsWith("http")) { - buildScanUrl = line.trim(); + if (publishing && line.startsWith('http')) { + buildScanUrl = line.trim() publishing = false } } } - }); + }) - return new BuildResultImpl(status, buildScanUrl); + return new BuildResultImpl(status, buildScanUrl) } export interface BuildResult { @@ -34,9 +36,5 @@ export interface BuildResult { } class BuildResultImpl implements BuildResult { - constructor( - readonly status: number, - readonly buildScanUrl?: string - ) { - } + constructor(readonly status: number, readonly buildScanUrl?: string) {} } diff --git a/src/gradlew.ts b/src/gradlew.ts index bd11935..2542f07 100644 --- a/src/gradlew.ts +++ b/src/gradlew.ts @@ -1,11 +1,9 @@ -const IS_WINDOWS = process.platform === "win32"; - +const IS_WINDOWS = process.platform === 'win32' export function wrapperFilename(): string { - return IS_WINDOWS ? "gradlew.bat" : "gradlew"; + return IS_WINDOWS ? 'gradlew.bat' : 'gradlew' } - export function installScriptFilename(): string { - return IS_WINDOWS ? "gradle.bat" : "gradle"; + return IS_WINDOWS ? 'gradle.bat' : 'gradle' } diff --git a/src/main.ts b/src/main.ts index c0ff113..cac298b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,80 +1,72 @@ -import * as core from "@actions/core"; -import * as path from "path"; -import {parseArgsStringToArgv} from "string-argv"; - -import * as execution from "./execution"; -import * as gradlew from "./gradlew"; -import * as provision from "./provision"; +import * as core from '@actions/core' +import * as path from 'path' +import {parseArgsStringToArgv} from 'string-argv' +import * as execution from './execution' +import * as gradlew from './gradlew' +import * as provision from './provision' // Invoked by GitHub Actions export async function run() { try { - - const baseDirectory = process.env[`GITHUB_WORKSPACE`] || ""; + const baseDirectory = process.env[`GITHUB_WORKSPACE`] || '' let result = await execution.execute( await resolveGradleExecutable(baseDirectory), resolveBuildRootDirectory(baseDirectory), parseCommandLineArguments() - ); + ) if (result.buildScanUrl) { - core.setOutput("build-scan-url", result.buildScanUrl); + core.setOutput('build-scan-url', result.buildScanUrl) } if (result.status != 0) { core.setFailed(`Gradle process exited with status ${result.status}`) } - } catch (error) { - core.setFailed(error.message); + core.setFailed(error.message) } } - -run(); - +run() async function resolveGradleExecutable(baseDirectory: string): Promise { - - const gradleVersion = inputOrNull("gradle-version"); - if (gradleVersion != null && gradleVersion != "wrapper") { + const gradleVersion = inputOrNull('gradle-version') + if (gradleVersion != null && gradleVersion != 'wrapper') { return path.resolve(await provision.gradleVersion(gradleVersion)) } - const gradleExecutable = inputOrNull("gradle-executable"); + const gradleExecutable = inputOrNull('gradle-executable') if (gradleExecutable != null) { return path.resolve(baseDirectory, gradleExecutable) } - const wrapperDirectory = inputOrNull("wrapper-directory"); - const executableDirectory = wrapperDirectory != null - ? path.join(baseDirectory, wrapperDirectory) - : baseDirectory; + const wrapperDirectory = inputOrNull('wrapper-directory') + const executableDirectory = + wrapperDirectory != null + ? path.join(baseDirectory, wrapperDirectory) + : baseDirectory - return path.resolve(executableDirectory, gradlew.wrapperFilename()); + return path.resolve(executableDirectory, gradlew.wrapperFilename()) } - function resolveBuildRootDirectory(baseDirectory: string): string { - let buildRootDirectory = inputOrNull("build-root-directory"); + let buildRootDirectory = inputOrNull('build-root-directory') return buildRootDirectory == null ? path.resolve(baseDirectory) - : path.resolve(baseDirectory, buildRootDirectory); + : path.resolve(baseDirectory, buildRootDirectory) } - function parseCommandLineArguments(): string[] { - const input = inputOrNull("arguments"); + const input = inputOrNull('arguments') return input == null ? [] : parseArgsStringToArgv(input) } - function inputOrNull(name: string): string | null { - const inputString = core.getInput(name); + const inputString = core.getInput(name) if (inputString.length == 0) { - return null; + return null } return inputString } diff --git a/src/post.ts b/src/post.ts index 85db455..625b65b 100644 --- a/src/post.ts +++ b/src/post.ts @@ -1,8 +1,8 @@ -import * as core from "@actions/core"; +import * as core from '@actions/core' // Invoked by GitHub Actions export async function run() { - core.info("POST Gradle Command Action") + core.info('POST Gradle Command Action') } -run(); +run() diff --git a/src/provision.ts b/src/provision.ts index 454eecb..ea199d5 100644 --- a/src/provision.ts +++ b/src/provision.ts @@ -1,163 +1,164 @@ -import * as fs from "fs"; -import * as path from "path"; -import * as httpm from 'typed-rest-client/HttpClient'; -import * as unzip from "unzipper" -import * as core from "@actions/core"; -import * as io from '@actions/io'; -import * as toolCache from "@actions/tool-cache"; +import * as fs from 'fs' +import * as path from 'path' +import * as httpm from 'typed-rest-client/HttpClient' +import * as unzip from 'unzipper' +import * as core from '@actions/core' +import * as io from '@actions/io' +import * as toolCache from '@actions/tool-cache' -import * as gradlew from "./gradlew"; - - -const httpc = new httpm.HttpClient("eskatos/gradle-command-action"); -const gradleVersionsBaseUrl = "https://services.gradle.org/versions"; +import * as gradlew from './gradlew' +const httpc = new httpm.HttpClient('eskatos/gradle-command-action') +const gradleVersionsBaseUrl = 'https://services.gradle.org/versions' /** * @return Gradle executable path */ export async function gradleVersion(gradleVersion: string): Promise { switch (gradleVersion) { - case "current": - return gradleCurrent(); - case "rc": - return gradleReleaseCandidate(); - case "nightly": - return gradleNightly(); - case "release-nightly": - return gradleReleaseNightly(); + case 'current': + return gradleCurrent() + case 'rc': + return gradleReleaseCandidate() + case 'nightly': + return gradleNightly() + case 'release-nightly': + return gradleReleaseNightly() default: - return gradle(gradleVersion); + return gradle(gradleVersion) } } - async function gradleCurrent(): Promise { - const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`); - return provisionGradle(json.version, json.downloadUrl); + const json = await gradleVersionDeclaration( + `${gradleVersionsBaseUrl}/current` + ) + return provisionGradle(json.version, json.downloadUrl) } - async function gradleReleaseCandidate(): Promise { - const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`); + const json = await gradleVersionDeclaration( + `${gradleVersionsBaseUrl}/release-candidate` + ) if (json) { - return provisionGradle(json.version, json.downloadUrl); + return provisionGradle(json.version, json.downloadUrl) } - return gradleCurrent(); + return gradleCurrent() } - async function gradleNightly(): Promise { - const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`); - return provisionGradle(json.version, json.downloadUrl); + const json = await gradleVersionDeclaration( + `${gradleVersionsBaseUrl}/nightly` + ) + return provisionGradle(json.version, json.downloadUrl) } - async function gradleReleaseNightly(): Promise { - const json = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`); - return provisionGradle(json.version, json.downloadUrl); + const json = await gradleVersionDeclaration( + `${gradleVersionsBaseUrl}/release-nightly` + ) + return provisionGradle(json.version, json.downloadUrl) } - async function gradle(version: string): Promise { - const declaration = await findGradleVersionDeclaration(version); + const declaration = await findGradleVersionDeclaration(version) if (!declaration) { - throw new Error(`Gradle version ${version} does not exists`); + throw new Error(`Gradle version ${version} does not exists`) } - return provisionGradle(declaration.version, declaration.downloadUrl); + return provisionGradle(declaration.version, declaration.downloadUrl) } - async function gradleVersionDeclaration(url: string): Promise { - const json: any = await httpGetJson(url); - return (json.version && json.version.length > 0) ? json : undefined + const json: any = await httpGetJson(url) + return json.version && json.version.length > 0 ? json : undefined } - -async function findGradleVersionDeclaration(version: string): Promise { - const json: any = await httpGetJson(`${gradleVersionsBaseUrl}/all`); +async function findGradleVersionDeclaration( + version: string +): Promise { + const json: any = await httpGetJson(`${gradleVersionsBaseUrl}/all`) const found: any = json.find((entry: any) => { - return entry.version === version; - }); + return entry.version === version + }) return found ? found : undefined } - async function provisionGradle(version: string, url: string): Promise { - - const cachedInstall: string = toolCache.find("gradle", version); + const cachedInstall: string = toolCache.find('gradle', version) if (cachedInstall.length > 0) { - const cachedExecutable = executableFrom(cachedInstall); - core.info(`Provisioned Gradle executable ${cachedExecutable}`); - return cachedExecutable; + const cachedExecutable = executableFrom(cachedInstall) + core.info(`Provisioned Gradle executable ${cachedExecutable}`) + return cachedExecutable } - const home = process.env["HOME"] || ""; - const tmpdir = path.join(home, "gradle-provision-tmpdir"); - const downloadsDir = path.join(tmpdir, "downloads"); - const installsDir = path.join(tmpdir, "installs"); - await io.mkdirP(downloadsDir); - await io.mkdirP(installsDir); + const home = process.env['HOME'] || '' + const tmpdir = path.join(home, 'gradle-provision-tmpdir') + const downloadsDir = path.join(tmpdir, 'downloads') + const installsDir = path.join(tmpdir, 'installs') + await io.mkdirP(downloadsDir) + await io.mkdirP(installsDir) - core.info(`Downloading ${url}`); + core.info(`Downloading ${url}`) - const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`); - await httpDownload(url, downloadPath); - core.info(`Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}`); + const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`) + await httpDownload(url, downloadPath) + core.info( + `Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}` + ) - await extractZip(downloadPath, installsDir); - const installDir = path.join(installsDir, `gradle-${version}`); - core.info(`Extracted in ${installDir}`); + await extractZip(downloadPath, installsDir) + const installDir = path.join(installsDir, `gradle-${version}`) + core.info(`Extracted in ${installDir}`) - const executable = executableFrom(installDir); - fs.chmodSync(executable, "755"); - core.info(`Provisioned Gradle executable ${executable}`); + const executable = executableFrom(installDir) + fs.chmodSync(executable, '755') + core.info(`Provisioned Gradle executable ${executable}`) - toolCache.cacheDir(installDir, "gradle", version); + toolCache.cacheDir(installDir, 'gradle', version) - return executable; + return executable } - function executableFrom(installDir: string): string { - return path.join(installDir, "bin", `${gradlew.installScriptFilename()}`); + return path.join(installDir, 'bin', `${gradlew.installScriptFilename()}`) } - async function httpGetJson(url: string): Promise { - const response = await httpc.get(url); - const body = await response.readBody(); - return JSON.parse(body); + const response = await httpc.get(url) + const body = await response.readBody() + return JSON.parse(body) } - async function httpDownload(url: string, path: string): Promise { return new Promise(function (resolve, reject) { - const writeStream = fs.createWriteStream(path); - httpc.get(url).then(response => { - response.message.pipe(writeStream) - .on("close", () => { - resolve(); - }) - .on("error", err => { - reject(err) - }); - }).catch(reason => { - reject(reason); - }); - }); + const writeStream = fs.createWriteStream(path) + httpc + .get(url) + .then(response => { + response.message + .pipe(writeStream) + .on('close', () => { + resolve() + }) + .on('error', err => { + reject(err) + }) + }) + .catch(reason => { + reject(reason) + }) + }) } - async function extractZip(zip: string, destination: string): Promise { return new Promise(function (resolve, reject) { fs.createReadStream(zip) - .pipe(unzip.Extract({"path": destination})) - .on("close", () => { - resolve(); + .pipe(unzip.Extract({path: destination})) + .on('close', () => { + resolve() }) - .on("error", err => { + .on('error', err => { reject(err) - }); - }); + }) + }) }