From dd678328a032dac1ec3c01dafed4615315fee0f3 Mon Sep 17 00:00:00 2001
From: Shohei Ueda <30958501+peaceiris@users.noreply.github.com>
Date: Sun, 5 Jan 2020 23:20:46 +0900
Subject: [PATCH] refactor: Split installer into some functions (#33)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* refactor: Add createTempDir()
* refactor: Print tempDir to debug log
* refactor: Add getBaseLocation()
* refactor: Enhance OS type checking
* refactor: Prune core.debug
* refactor: Prune extra async
* refactor: Reduce call count of getBaseLocation()
* refactor: Prune extra initialization
* refactor: Use node-fetch and test with nock
* chore: Add Makefile
* docs: Add make command (Maintainer Notes)
* ci: Enhance YAML list
* style: 💄 reindent
* deps: update
* test: Add nock.cleanAll() to afterEach()
---
.github/workflows/test.yml | 87 +++++++++++++++-------------
Makefile | 22 +++++++
README.md | 15 ++---
__tests__/get-latest-version.test.ts | 68 ++++++++++++++++------
__tests__/get-os.test.ts | 2 +-
__tests__/get-url.test.ts | 2 +-
__tests__/main.test.ts | 2 +-
package-lock.json | 74 +++++++++++++++--------
package.json | 14 ++---
src/get-latest-version.ts | 61 +++++--------------
src/get-os.ts | 2 +-
src/get-url.ts | 2 +-
src/installer.ts | 45 ++++++++------
src/main.ts | 12 ++--
14 files changed, 235 insertions(+), 173 deletions(-)
create mode 100644 Makefile
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 48a42c5..8eecb2c 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -2,7 +2,9 @@ name: 'Test'
on:
pull_request:
- types: [opened, synchronize]
+ types:
+ - opened
+ - synchronize
paths-ignore:
- '**.md'
push:
@@ -54,45 +56,48 @@ jobs:
needs: code-health
strategy:
matrix:
- os: ['ubuntu-18.04', 'macos-latest', 'windows-latest']
+ os:
+ - 'ubuntu-18.04'
+ - 'macos-latest'
+ - 'windows-latest'
steps:
- - uses: actions/checkout@v2
-
- - name: Read .nvmrc
- run: echo "::set-output name=NVMRC::$(cat .nvmrc)"
- id: nvm
-
- - name: Setup Node
- uses: actions/setup-node@v1
- with:
- node-version: '${{ steps.nvm.outputs.NVMRC }}'
-
- - name: Get npm cache directory
- id: npm-cache
- run: |
- echo "::set-output name=dir::$(npm config get cache)"
-
- - name: Cache npm
- uses: actions/cache@v1
- with:
- path: ${{ steps.npm-cache.outputs.dir }}
- key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
- restore-keys: |
- ${{ runner.os }}-node-
-
- - run: npm ci
-
- - run: npm test
-
- - name: Upload test coverage as artifact
- uses: actions/upload-artifact@v1
- with:
- name: coverage
- path: coverage
-
- - name: Upload test coverage to Coveralls
- if: startsWith(matrix.os, 'ubuntu')
- uses: coverallsapp/github-action@master
- with:
- github-token: ${{ secrets.GITHUB_TOKEN }}
+ - uses: actions/checkout@v2
+
+ - name: Read .nvmrc
+ run: echo "::set-output name=NVMRC::$(cat .nvmrc)"
+ id: nvm
+
+ - name: Setup Node
+ uses: actions/setup-node@v1
+ with:
+ node-version: '${{ steps.nvm.outputs.NVMRC }}'
+
+ - name: Get npm cache directory
+ id: npm-cache
+ run: |
+ echo "::set-output name=dir::$(npm config get cache)"
+
+ - name: Cache npm
+ uses: actions/cache@v1
+ with:
+ path: ${{ steps.npm-cache.outputs.dir }}
+ key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
+ restore-keys: |
+ ${{ runner.os }}-node-
+
+ - run: npm ci
+
+ - run: npm test
+
+ - name: Upload test coverage as artifact
+ uses: actions/upload-artifact@v1
+ with:
+ name: coverage
+ path: coverage
+
+ - name: Upload test coverage to Coveralls
+ if: startsWith(matrix.os, 'ubuntu')
+ uses: coverallsapp/github-action@master
+ with:
+ github-token: ${{ secrets.GITHUB_TOKEN }}
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e95e961
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,22 @@
+cmd := "bash"
+msg := ""
+IMAGE_NAME := actions-mdbook-dev:latest
+DOCKER_BUILD := docker build . -t $(IMAGE_NAME) --file ./__tests__/Dockerfile
+DOCKER_RUN := docker run --rm -i -t -v ${PWD}:/repo -v ~/.gitconfig:/etc/gitconfig $(IMAGE_NAME)
+
+
+.PHONY: build
+build:
+ $(DOCKER_BUILD)
+
+.PHONY: run
+run:
+ $(DOCKER_RUN) $(cmd)
+
+.PHONY: test
+test:
+ $(DOCKER_RUN) npm test
+
+.PHONY: commit
+commit:
+ $(DOCKER_RUN) git commit -m "$(msg)"
diff --git a/README.md b/README.md
index 0a7c5d3..2aadd0a 100644
--- a/README.md
+++ b/README.md
@@ -192,21 +192,16 @@ Run `npm` and `git commit` commands on a container.
```sh
# Docker
-npm run docker:build
-npm run docker:run
+make build
+make run
+make run cmd="env"
+make test
+make commit msg="chore: Add Makefile"
# Release
./release.sh
```
-**On Container**
-
-```sh
-npm test
-git add ./__tests__
-git commit -m "test: Add something"
-```
-
diff --git a/__tests__/get-latest-version.test.ts b/__tests__/get-latest-version.test.ts
index 2ffacf1..03d9365 100644
--- a/__tests__/get-latest-version.test.ts
+++ b/__tests__/get-latest-version.test.ts
@@ -1,31 +1,63 @@
-import * as target from '../src/get-latest-version';
+import {getURL, getLatestVersion} from '../src/get-latest-version';
+const nock = require('nock');
+import {FetchError} from 'node-fetch';
import jsonTestBrew from './data/brew.json';
import jsonTestGithub from './data/github.json';
-describe('Test', () => {
- test('getURL()', () => {
- const org: string = 'rust-lang';
- const repo: string = 'mdbook';
- const urlBrewExpected: string = `https://formulae.brew.sh/api/formula/${repo}.json`;
- const urlGithubExpected: string = `https://api.github.com/repos/${org}/${repo}/releases/latest`;
- const urlBrew: string = target.getURL(org, repo, 'brew');
- const urlGithub: string = target.getURL(org, repo, 'github');
+beforeEach(() => {
+ jest.resetModules();
+});
+
+afterEach(() => {
+ nock.cleanAll();
+});
+
+const org: string = 'rust-lang';
+const repo: string = 'mdbook';
+const urlBrewExpected: string = `https://formulae.brew.sh/api/formula/${repo}.json`;
+const urlGithubExpected: string = `https://api.github.com/repos/${org}/${repo}/releases/latest`;
+
+describe('getURL()', () => {
+ test('should return expected URL', () => {
+ const urlBrew: string = getURL(org, repo, 'brew');
+ const urlGithub: string = getURL(org, repo, 'github');
expect(urlBrew).toMatch(urlBrewExpected);
expect(urlGithub).toMatch(urlGithubExpected);
});
+});
- let versionLatest: string = '0.3.5';
+describe('getLatestVersion()', () => {
+ let versionLatestExpected: string = '0.3.5';
- test('getLatestBrew()', () => {
- const jsonBrew: target.JsonBrew = jsonTestBrew;
- const versionBrew: string = target.getLatestBrew(jsonBrew);
- expect(versionBrew).toMatch(versionLatest);
+ test('should return latest version via brew', async () => {
+ nock('https://formulae.brew.sh')
+ .get(`/api/formula/${repo}.json`)
+ .reply(200, jsonTestBrew);
+
+ const versionLatest: string = await getLatestVersion(org, repo, 'brew');
+ expect(versionLatest).toMatch(versionLatestExpected);
});
- test('getLatestGithub()', () => {
- const jsonGithub: target.JsonGithub = jsonTestGithub;
- const versionGithub: string = target.getLatestGithub(jsonGithub);
- expect(versionGithub).toMatch(versionLatest);
+ test('should return latest version via GitHub', async () => {
+ nock('https://api.github.com')
+ .get(`/repos/${org}/${repo}/releases/latest`)
+ .reply(200, jsonTestGithub);
+
+ const versionLatest: string = await getLatestVersion(org, repo, 'github');
+ expect(versionLatest).toMatch(versionLatestExpected);
+ });
+
+ test('should return exception 404', async () => {
+ nock('https://formulae.brew.sh')
+ .get(`/api/formula/${repo}.json`)
+ .reply(404);
+
+ try {
+ const versionLatest: string = await getLatestVersion(org, repo, 'brew');
+ console.debug(versionLatest);
+ } catch (e) {
+ expect(e).toThrow(FetchError);
+ }
});
});
diff --git a/__tests__/get-os.test.ts b/__tests__/get-os.test.ts
index 0f1874f..b2e79bb 100644
--- a/__tests__/get-os.test.ts
+++ b/__tests__/get-os.test.ts
@@ -1,4 +1,4 @@
-import getOS from '../src/get-os';
+import {getOS} from '../src/get-os';
describe('getOS', () => {
test('test', () => {
diff --git a/__tests__/get-url.test.ts b/__tests__/get-url.test.ts
index 125fb70..82a6cbe 100644
--- a/__tests__/get-url.test.ts
+++ b/__tests__/get-url.test.ts
@@ -1,4 +1,4 @@
-import getURL from '../src/get-url';
+import {getURL} from '../src/get-url';
describe('getURL()', () => {
test('test', () => {
diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts
index d1215c9..4514722 100644
--- a/__tests__/main.test.ts
+++ b/__tests__/main.test.ts
@@ -1,6 +1,6 @@
import * as main from '../src/main';
-jest.setTimeout(20000);
+jest.setTimeout(30000);
beforeEach(() => {
jest.resetModules();
diff --git a/package-lock.json b/package-lock.json
index 4e0313e..0362d26 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -589,10 +589,17 @@
"dev": true
},
"@types/node": {
- "version": "13.1.1",
- "resolved": "https://registry.npmjs.org/@types/node/-/node-13.1.1.tgz",
- "integrity": "sha512-hx6zWtudh3Arsbl3cXay+JnkvVgCKzCWKv42C9J01N2T2np4h8w5X8u6Tpz5mj38kE3M9FM0Pazx8vKFFMnjLQ==",
- "dev": true
+ "version": "13.1.4",
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-13.1.4.tgz",
+ "integrity": "sha512-Lue/mlp2egZJoHXZr4LndxDAd7i/7SQYhV0EjWfb/a4/OZ6tuVwMCVPiwkU5nsEipxEf7hmkSU7Em5VQ8P5NGA=="
+ },
+ "@types/node-fetch": {
+ "version": "2.5.4",
+ "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.4.tgz",
+ "integrity": "sha512-Oz6id++2qAOFuOlE1j0ouk1dzl3mmI1+qINPNBhi9nt/gVOz0G+13Ao6qjhdF0Ys+eOkhu6JnFmt38bR3H0POQ==",
+ "requires": {
+ "@types/node": "*"
+ }
},
"@types/normalize-package-data": {
"version": "2.4.0",
@@ -622,32 +629,32 @@
"dev": true
},
"@typescript-eslint/experimental-utils": {
- "version": "2.13.0",
- "resolved": "https://registry.npmjs.org/@typescript-eslint/experimental-utils/-/experimental-utils-2.13.0.tgz",
- "integrity": "sha512-+Hss3clwa6aNiC8ZjA45wEm4FutDV5HsVXPl/rDug1THq6gEtOYRGLqS3JlTk7mSnL5TbJz0LpEbzbPnKvY6sw==",
+ "version": "2.14.0",
+ "resolved": "https://registry.npmjs.org/@typescript-eslint/experimental-utils/-/experimental-utils-2.14.0.tgz",
+ "integrity": "sha512-KcyKS7G6IWnIgl3ZpyxyBCxhkBPV+0a5Jjy2g5HxlrbG2ZLQNFeneIBVXdaBCYOVjvGmGGFKom1kgiAY75SDeQ==",
"dev": true,
"requires": {
"@types/json-schema": "^7.0.3",
- "@typescript-eslint/typescript-estree": "2.13.0",
+ "@typescript-eslint/typescript-estree": "2.14.0",
"eslint-scope": "^5.0.0"
}
},
"@typescript-eslint/parser": {
- "version": "2.13.0",
- "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-2.13.0.tgz",
- "integrity": "sha512-vbDeLr5QRJ1K7x5iRK8J9wuGwR9OVyd1zDAY9XFAQvAosHVjSVbDgkm328ayE6hx2QWVGhwvGaEhedcqAbfQcA==",
+ "version": "2.14.0",
+ "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-2.14.0.tgz",
+ "integrity": "sha512-haS+8D35fUydIs+zdSf4BxpOartb/DjrZ2IxQ5sR8zyGfd77uT9ZJZYF8+I0WPhzqHmfafUBx8MYpcp8pfaoSA==",
"dev": true,
"requires": {
"@types/eslint-visitor-keys": "^1.0.0",
- "@typescript-eslint/experimental-utils": "2.13.0",
- "@typescript-eslint/typescript-estree": "2.13.0",
+ "@typescript-eslint/experimental-utils": "2.14.0",
+ "@typescript-eslint/typescript-estree": "2.14.0",
"eslint-visitor-keys": "^1.1.0"
}
},
"@typescript-eslint/typescript-estree": {
- "version": "2.13.0",
- "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-2.13.0.tgz",
- "integrity": "sha512-t21Mg5cc8T3ADEUGwDisHLIubgXKjuNRbkpzDMLb7/JMmgCe/gHM9FaaujokLey+gwTuLF5ndSQ7/EfQqrQx4g==",
+ "version": "2.14.0",
+ "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-2.14.0.tgz",
+ "integrity": "sha512-pnLpUcMNG7GfFFfNQbEX6f1aPa5fMnH2G9By+A1yovYI4VIOK2DzkaRuUlIkbagpAcrxQHLqovI1YWqEcXyRnA==",
"dev": true,
"requires": {
"debug": "^4.1.1",
@@ -660,9 +667,9 @@
}
},
"@zeit/ncc": {
- "version": "0.20.5",
- "resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.20.5.tgz",
- "integrity": "sha512-XU6uzwvv95DqxciQx+aOLhbyBx/13ky+RK1y88Age9Du3BlA4mMPCy13BGjayOrrumOzlq1XV3SD/BWiZENXlw==",
+ "version": "0.21.0",
+ "resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.21.0.tgz",
+ "integrity": "sha512-RUMdvVK/w78oo+yBjruZltt0kJXYar2un/1bYQ2LuHG7GmFVm+QjxzEmySwREctaJdEnBvlMdUNWd9hXHxEI3g==",
"dev": true
},
"JSONStream": {
@@ -6198,6 +6205,24 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true
},
+ "nock": {
+ "version": "11.7.1",
+ "resolved": "https://registry.npmjs.org/nock/-/nock-11.7.1.tgz",
+ "integrity": "sha512-fW+dlMyquAEvgtmGoRHftKrNnJ7yZcbINQDTW7OKRoTYa+11GA0u0UokWHuvCyZrG5TXkplnEv2jmj6R3vvImg==",
+ "dev": true,
+ "requires": {
+ "debug": "^4.1.0",
+ "json-stringify-safe": "^5.0.1",
+ "lodash": "^4.17.13",
+ "mkdirp": "^0.5.0",
+ "propagate": "^2.0.0"
+ }
+ },
+ "node-fetch": {
+ "version": "2.6.0",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
+ "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
+ },
"node-int64": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz",
@@ -6666,6 +6691,12 @@
"sisteransi": "^1.0.3"
}
},
+ "propagate": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/propagate/-/propagate-2.0.1.tgz",
+ "integrity": "sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==",
+ "dev": true
+ },
"psl": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.4.0.tgz",
@@ -8410,11 +8441,6 @@
"integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==",
"dev": true
},
- "xmlhttprequest": {
- "version": "1.8.0",
- "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz",
- "integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw="
- },
"xtend": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
diff --git a/package.json b/package.json
index fea98dc..c8e34d5 100644
--- a/package.json
+++ b/package.json
@@ -11,9 +11,7 @@
"tsc": "tsc",
"format": "prettier --write **/*.ts",
"format:check": "prettier --check **/*.ts",
- "release": "standard-version",
- "docker:build": "docker build . -t actions-mdbook-dev:latest --file ./__tests__/Dockerfile",
- "docker:run": "docker run --rm -i -t -v ${PWD}:/repo -v ~/.gitconfig:/etc/gitconfig actions-mdbook-dev:latest"
+ "release": "standard-version"
},
"husky": {
"skipCI": true,
@@ -52,18 +50,20 @@
"@actions/exec": "^1.0.2",
"@actions/io": "^1.0.1",
"@actions/tool-cache": "^1.1.2",
- "xmlhttprequest": "^1.8.0"
+ "@types/node-fetch": "^2.5.4",
+ "node-fetch": "^2.6.0"
},
"devDependencies": {
"@types/jest": "^24.0.25",
- "@types/node": "^13.1.1",
- "@typescript-eslint/parser": "^2.13.0",
- "@zeit/ncc": "^0.20.5",
+ "@types/node": "^13.1.4",
+ "@typescript-eslint/parser": "^2.14.0",
+ "@zeit/ncc": "^0.21.0",
"eslint": "^6.8.0",
"husky": "^3.1.0",
"jest": "^24.9.0",
"jest-circus": "^24.9.0",
"lint-staged": "^9.5.0",
+ "nock": "^11.7.1",
"prettier": "1.19.1",
"standard-version": "^7.0.1",
"ts-jest": "^24.2.0",
diff --git a/src/get-latest-version.ts b/src/get-latest-version.ts
index 2c5f3bc..f0031a3 100644
--- a/src/get-latest-version.ts
+++ b/src/get-latest-version.ts
@@ -1,16 +1,4 @@
-const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
-
-interface BrewVersions {
- stable: string;
-}
-
-export interface JsonBrew {
- versions: BrewVersions;
-}
-
-export interface JsonGithub {
- tag_name: string;
-}
+import fetch from 'node-fetch';
export function getURL(org: string, repo: string, api: string): string {
let url: string = '';
@@ -24,42 +12,23 @@ export function getURL(org: string, repo: string, api: string): string {
return url;
}
-export function getLatestBrew(data: JsonBrew): string {
- let latestVersion: string = '';
- latestVersion = data.versions.stable;
- return latestVersion;
-}
-
-export function getLatestGithub(data: JsonGithub): string {
- let latestVersion: string = '';
- latestVersion = data.tag_name;
- latestVersion = latestVersion.replace('v', '');
- return latestVersion;
-}
-
-export default async function getLatestVersion(
+export async function getLatestVersion(
org: string,
repo: string,
api: string
): Promise {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
+ try {
const url = getURL(org, repo, api);
- xhr.open('GET', url);
- xhr.send();
- xhr.onload = () => {
- let latestVersion: string = '';
- if (api === 'brew') {
- const result: JsonBrew = JSON.parse(xhr.responseText);
- latestVersion = getLatestBrew(result);
- } else if (api === 'github') {
- const result: JsonGithub = JSON.parse(xhr.responseText);
- latestVersion = getLatestGithub(result);
- }
- resolve(latestVersion);
- };
- xhr.onerror = () => {
- reject(`ERROR: got status ${xhr.status} of ${url}`);
- };
- });
+ const response = await fetch(url);
+ const json = await response.json();
+ let latestVersion: string = '';
+ if (api === 'brew') {
+ latestVersion = json.versions.stable;
+ } else if (api === 'github') {
+ latestVersion = json.tag_name;
+ }
+ return latestVersion;
+ } catch (e) {
+ return e;
+ }
}
diff --git a/src/get-os.ts b/src/get-os.ts
index 7358af5..43070e6 100644
--- a/src/get-os.ts
+++ b/src/get-os.ts
@@ -1,4 +1,4 @@
-export default function getOS(platform: string) {
+export function getOS(platform: string) {
if (platform === 'linux') {
return 'unknown-linux-gnu';
} else if (platform === 'darwin') {
diff --git a/src/get-url.ts b/src/get-url.ts
index 4213b6f..ee3e94d 100644
--- a/src/get-url.ts
+++ b/src/get-url.ts
@@ -1,4 +1,4 @@
-export default function getURL(os: string, version: string): string {
+export function getURL(os: string, version: string): string {
const ext = (os: string) => {
if (os === 'pc-windows-msvc') {
return 'zip';
diff --git a/src/installer.ts b/src/installer.ts
index f50b0a9..991df4e 100644
--- a/src/installer.ts
+++ b/src/installer.ts
@@ -1,43 +1,54 @@
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import * as io from '@actions/io';
-import getOS from './get-os';
-import getURL from './get-url';
+import {getOS} from './get-os';
+import {getURL} from './get-url';
import * as path from 'path';
-let tempDir: string = process.env['RUNNER_TEMPDIRECTORY'] || '';
-if (!tempDir) {
- let baseTempLocation: string;
+export function getBaseLocation(): string {
+ let baseLocation: string = '';
+
if (process.platform === 'win32') {
- baseTempLocation = process.env['USERPROFILE'] || 'C:\\';
+ baseLocation = process.env['USERPROFILE'] || 'C:\\';
} else {
- baseTempLocation = `${process.env.HOME}`;
+ baseLocation = `${process.env.HOME}`;
}
- tempDir = path.join(baseTempLocation, 'tmp');
+
+ core.debug(`tempDir: ${baseLocation}`);
+
+ return baseLocation;
}
-export default async function installer(version: string) {
+export async function createTempDir(baseLocation: string): Promise {
+ let tempDir: string = process.env['RUNNER_TEMPDIRECTORY'] || '';
+
+ if (tempDir === '') {
+ tempDir = path.join(baseLocation, 'tmp');
+ }
+
+ await io.mkdirP(tempDir);
+ core.debug(`tempDir: ${tempDir}`);
+
+ return tempDir;
+}
+
+export async function installer(version: string) {
const osName: string = getOS(process.platform);
core.info(`Operating System: ${osName}`);
const toolURL: string = getURL(osName, version);
core.info(`toolURL: ${toolURL}`);
- let baseLocation: string;
- if (process.platform === 'win32') {
- baseLocation = process.env['USERPROFILE'] || 'C:\\';
- } else {
- baseLocation = `${process.env.HOME}`;
- }
+ const baseLocation: string = getBaseLocation();
const toolPath: string = path.join(baseLocation, 'toolbin');
await io.mkdirP(toolPath);
core.addPath(toolPath);
// Download and extract mdbook binary
- await io.mkdirP(tempDir);
+ const tempDir: string = await createTempDir(baseLocation);
const toolAssets: string = await tc.downloadTool(toolURL);
let toolBin: string = '';
- if (osName === 'pc-windows-msvc') {
+ if (process.platform === 'win32') {
const toolExtractedFolder: string = await tc.extractZip(
toolAssets,
tempDir
diff --git a/src/main.ts b/src/main.ts
index 6447a32..5aff9dd 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
-import getLatestVersion from './get-latest-version';
-import installer from './installer';
+import {getLatestVersion} from './get-latest-version';
+import {installer} from './installer';
export interface actionResult {
exitcode: number;
@@ -31,9 +31,11 @@ export async function showVersion(
};
result.exitcode = await exec.exec(cmd, args, options);
- core.debug(`exit code: ${result.exitcode}`);
- core.debug(`stdout: ${result.output}`);
- core.debug(`stderr: ${result.error}`);
+ core.debug(`
+ exit code: ${result.exitcode}
+ stdout: ${result.output}
+ stderr: ${result.error}
+ `);
return result;
}