UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA Functions and Pages.

128 lines (127 loc) 5.24 kB
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()); }); }; import { execSync } from 'child_process'; import https from 'https'; import os from 'os'; import path from 'path'; import util from 'util'; import fs from 'fs-extra'; import t from '../i18n/index.js'; import logger from '../libs/logger.js'; import { checkOS } from './checkOS.js'; import { calculateFileMD5 } from './fileMd5.js'; import { getDirName } from './fileUtils/base.js'; export const EW2DirName = '.ew2'; export const EW2BinName = 'edgeworker2'; export const EW2Path = path.join(os.homedir(), EW2DirName); export const EW2BinPath = path.join(EW2Path, EW2BinName); export const EW2ManifestPath = path.join(EW2Path, 'manifest.json'); export function preCheckEw2() { return __awaiter(this, void 0, void 0, function* () { const fsAccess = util.promisify(fs.access); const manifestRes = yield checkManifest(); function installVersion(manifest) { return __awaiter(this, void 0, void 0, function* () { try { yield installEw2(manifest); return true; } catch (error) { const err = error; logger.error(err.message); return false; } }); } try { yield fsAccess(EW2BinPath, fs.constants.X_OK); if (!manifestRes.isLatest) { logger.warn(t('install_runtime_update_tip').d(`🔔 Runtime must be update to use esa-cli dev. Installing...`)); return yield installVersion(manifestRes.remoteManifest); } return true; } catch (error) { const err = error; if (err.code === 'EACCES') { logger.pathEacces(EW2BinPath); return false; } if (err.code === 'ENOENT' || err.code === 'EPERM') { logger.warn(t('install_runtime_tip').d(`🔔 Runtime must be installed to use esa-cli dev. Installing...`)); return yield installVersion(manifestRes.remoteManifest); } logger.error(err.message); return false; } }); } export function fetchRemoteManifest() { return new Promise((resolve, reject) => { https .get('https://edgestar-cn.oss-cn-beijing.aliyuncs.com/ew2/manifest.json', (res) => { let data = ''; res.on('data', (chunk) => (data += chunk)); res.on('end', () => { try { const json = JSON.parse(data); resolve(json); } catch (err) { reject(null); } }); }) .on('error', () => reject); }); } export function checkManifest() { return __awaiter(this, void 0, void 0, function* () { let isLatest = false; const remoteManifest = yield fetchRemoteManifest(); const localManifestExists = yield fs.pathExists(EW2ManifestPath); logger.info(`Remote version: ${remoteManifest.version}`); if (localManifestExists) { const localManifest = yield fs.readJSON(EW2ManifestPath); isLatest = localManifest.version === remoteManifest.version; logger.info(`Local version: ${localManifest.version}`); if (!isLatest) { logger.log(`Runtime Latest version: ${remoteManifest.version}, Current version: ${localManifest.version}`); } } return { remoteManifest, isLatest }; }); } export function installEw2(manifest) { return __awaiter(this, void 0, void 0, function* () { const __dirname = getDirName(import.meta.url); const p = path.join(__dirname, './install'); const installCommand = `sh ${p}/installEw2.sh ${manifest.version}`; try { execSync(installCommand, { stdio: 'inherit', env: Object.assign({}, process.env) }); const md5 = yield calculateFileMD5(EW2BinPath); const os = checkOS(); // @ts-ignore; if (md5 === manifest[`${os}_checksum`]) { logger.success('Runtime checksum success.'); } yield fs.writeJSON(EW2ManifestPath, manifest); logger.success(t('install_runtime_success').d(`Runtime installed.`)); return true; } catch (error) { logger.error(`Failed to install: ${error.message}`); return false; } }); }