actions-hugo/src/installer.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

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';
2019-11-22 02:14:44 +00:00
import * as path from 'path';
let tempDir: string = process.env['RUNNER_TEMPDIRECTORY'] || '';
if (!tempDir) {
let baseTempLocation: string;
if (process.platform === 'win32') {
baseTempLocation = process.env['USERPROFILE'] || 'C:\\';
} else {
baseTempLocation = `${process.env.HOME}`;
}
tempDir = path.join(baseTempLocation, 'tmp');
}
export async function installer(version: string): Promise<void> {
try {
const extended: string = core.getInput('extended');
console.log(`Hugo extended: ${extended}`);
const osName: string = getOS(process.platform);
console.log(`Operating System: ${osName}`);
const hugoURL: string = getURL(osName, extended, version);
core.debug(`hugoURL: ${hugoURL}`);
2019-11-22 02:14:44 +00:00
let baseLocation: string;
if (process.platform === 'win32') {
baseLocation = process.env['USERPROFILE'] || 'C:\\';
} else {
baseLocation = `${process.env.HOME}`;
}
const hugoPath: string = path.join(baseLocation, 'hugobin');
await io.mkdirP(hugoPath);
core.addPath(hugoPath);
// Download and extract Hugo binary
2019-11-22 02:14:44 +00:00
await io.mkdirP(tempDir);
const hugoAssets: string = await tc.downloadTool(hugoURL);
let hugoBin = '';
if (osName === 'Windows') {
const hugoExtractedFolder: string = await tc.extractZip(
hugoAssets,
2019-11-22 02:14:44 +00:00
tempDir
);
hugoBin = `${hugoExtractedFolder}/hugo.exe`;
} else {
const hugoExtractedFolder: string = await tc.extractTar(
hugoAssets,
2019-11-22 02:14:44 +00:00
tempDir
);
hugoBin = `${hugoExtractedFolder}/hugo`;
}
await io.mv(hugoBin, hugoPath);
} catch (error) {
core.setFailed(error.message);
}
}