UNPKG

ecus-cli

Version:

A Command line interface of ECUS

105 lines (104 loc) 4.98 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.updateCommand = void 0; const bundle_1 = require("../utils/bundle"); const got_1 = require("got"); const config_1 = require("../utils/config"); const zx_1 = require("zx"); const form_data_1 = __importDefault(require("form-data")); const lodash_1 = __importDefault(require("lodash")); const simple_git_1 = __importDefault(require("simple-git")); const file_1 = require("../utils/file"); exports.updateCommand = { command: "update", describe: "create a update and upload", builder: (yargs) => yargs .option("promote", { description: "promote to channel when uploaded.", }) .option("metadata", { description: "add update metadata info after in deployment. its should be a json string.", }) .option("chunked", { description: "upload update in chunked mode.", boolean: true, }), handler(args) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const git = (0, simple_git_1.default)(); const hash = yield git.revparse("HEAD"); const isClean = (yield git.status()).isClean(); const branch = (yield git.branch()).current; const message = (_a = (yield git.log()).latest) === null || _a === void 0 ? void 0 : _a.message; const promote = args.promote; const metadata = args.metadata; const chunked = args.chunked; const config = yield (0, config_1.getFileConfig)(); if (!config.url || !config.apikey || !config.projectId) { console.log(zx_1.chalk.red("Please run `ecus init` before.")); return; } const zipPath = yield (0, bundle_1.bundleJsPackage)(); console.log("Uploading to remote:", config.url); const startTime = Date.now(); try { let result; if (chunked) { console.log(zx_1.chalk.blue("Using chunked upload mode...")); const updateData = { gitInfo: { hash, isClean, branch, message }, promote: promote, metadata: metadata ? JSON.parse(metadata) : undefined, }; // use chunked upload result = yield (0, file_1.chunkUploadFile)(config.url, zipPath, { Authorization: `Bearer ${config.apikey}`, }, config.projectId, updateData); } else { // Use original upload method const buffer = yield zx_1.fs.readFile(zipPath); const form = new form_data_1.default(); form.append("file", buffer, "tmp.zip"); form.append("gitInfo", JSON.stringify({ hash, isClean, branch, message })); if (promote) { form.append("promote", promote); } if (metadata) { form.append("metadata", metadata); } const res = yield (0, file_1.uploadWithProgress)(`${config.url}/api/${config.projectId}/upload`, { headers: { Authorization: `Bearer ${config.apikey}`, }, body: form, }); result = JSON.parse(res); } const duration = Date.now() - startTime; console.log(`Uploaded completed in ${duration}ms, deployment id:`, chunked ? result.id : lodash_1.default.get(result, "id")); } catch (err) { const duration = Date.now() - startTime; console.log(`Upload failed in ${duration}ms`); if (err instanceof got_1.RequestError) { console.log((_b = err.response) === null || _b === void 0 ? void 0 : _b.body); } throw err; } }); }, };