UNPKG

shellcheck

Version:

Wrapper to download shellcheck

83 lines (82 loc) 4.18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.download = void 0; const promises_1 = __importDefault(require("node:fs/promises")); const node_process_1 = __importDefault(require("node:process")); const node_path_1 = __importDefault(require("node:path")); const node_os_1 = __importDefault(require("node:os")); const decompress_1 = __importDefault(require("decompress")); const configs_1 = require("../configs"); const logger_1 = require("../logger"); const utils_1 = require("../utils"); /** * Download ShellCheck. * * @param args - Arguments. */ function download(args) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; let tmpDir; const { destination } = args; const platform = (_a = args.platform) !== null && _a !== void 0 ? _a : node_process_1.default.platform; const architecture = (_b = args.architecture) !== null && _b !== void 0 ? _b : node_process_1.default.arch; const binArchive = `shellcheck${platform === 'win32' ? '.exe' : ''}`; try { // Check destination yield promises_1.default.access(node_path_1.default.dirname(destination), promises_1.default.constants.W_OK); // Temporary directory logger_1.logger.debug(`Creating temporary directory`); tmpDir = yield promises_1.default.mkdtemp(`${node_os_1.default.tmpdir()}${node_path_1.default.sep}`); const archive = node_path_1.default.normalize(`${tmpDir}/shellcheck.download`); const shellcheck = node_path_1.default.normalize(`${tmpDir}/${binArchive}`); // Build URL logger_1.logger.debug(`Building download URL`); const downloadURL = (_c = args.url) !== null && _c !== void 0 ? _c : (yield (0, utils_1.buildURL)({ token: args.token, platform, architecture })); // Download logger_1.logger.info(`Downloading '${downloadURL}' to '${archive}'`); yield (0, utils_1.requestDownload)({ url: downloadURL, token: args.token, destination: archive }); // Extract logger_1.logger.info(`Extracting '${archive}' to '${node_path_1.default.dirname(shellcheck)}'`); yield (0, decompress_1.default)(archive, node_path_1.default.dirname(shellcheck), { strip: 1, filter: (file) => file.path === binArchive }); // Permissions logger_1.logger.debug(`Changing permissions '${configs_1.config.mode.toString(8)}' of '${shellcheck}'`); yield promises_1.default.chmod(shellcheck, configs_1.config.mode); // TODO Move instead of copy, fs.rename cause problems in Windows // Copy logger_1.logger.info(`Copying '${shellcheck}' to '${destination}'`); yield promises_1.default.copyFile(shellcheck, destination); } finally { if (tmpDir) { try { logger_1.logger.debug(`Removing temporary directory '${tmpDir}'`); yield promises_1.default.rm(tmpDir, { recursive: true, force: true }); } catch (_d) { /* empty */ } } } }); } exports.download = download;