zipy-cli
Version:
CLI for Zipyai
92 lines (91 loc) • 5.89 kB
JavaScript
;
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.uploadFile = void 0;
const utilityFunctions_1 = require("../utils/utilityFunctions");
const Constant_1 = __importDefault(require("../constant/Constant"));
const MessageConstant_1 = __importDefault(require("../constant/MessageConstant"));
const uploadFile = (args) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
try {
console.log(JSON.stringify(args));
const { authKey, dirname, apiKey, releaseVer } = args;
if (!(0, utilityFunctions_1.validateApiKey)(apiKey)) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, MessageConstant_1.default.INVALID_API_KEY);
process.exit(0);
}
if (!(0, utilityFunctions_1.validateAuthKey)(authKey)) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, MessageConstant_1.default.INVALID_AUTH_KEY);
process.exit(0);
}
const files = yield (0, utilityFunctions_1.readFolderContents)(dirname);
if (files.length === 0) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, MessageConstant_1.default.NO_JS_OR_MAP_FILE);
process.exit(0);
}
(0, utilityFunctions_1.cliLog)(Constant_1.default.EXECUTING, MessageConstant_1.default.FILE_IN_FOLDER);
(0, utilityFunctions_1.cliLog)(Constant_1.default.EXECUTING, files.toString());
const filteredFiles = (0, utilityFunctions_1.filterMapsAndJsFiles)(files);
if (filteredFiles.length === 0) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, MessageConstant_1.default.NO_JS_OR_MAP_FILE);
process.exit(0);
}
(0, utilityFunctions_1.cliLog)(Constant_1.default.EXECUTING, MessageConstant_1.default.MAP_FILE_TO_UPLOAD);
(0, utilityFunctions_1.cliLog)(Constant_1.default.EXECUTING, filteredFiles.toString());
if (filteredFiles.length > 2500) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, 'Request size is too large. Please try uploading files in batches. Maximum of 2500 files and 400MB in size is allowed.');
process.exit(0);
}
const uploadApiRequest = (0, utilityFunctions_1.createUploadApiRequest)(apiKey, authKey, releaseVer, filteredFiles);
(0, utilityFunctions_1.cliLog)(Constant_1.default.EXECUTING, MessageConstant_1.default.FILE_UPLOADING);
let resp = yield (0, utilityFunctions_1.callUploadApi)(uploadApiRequest);
if (resp && resp.data == "") {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, MessageConstant_1.default.SOMETHING_WENT_WRONG);
}
else if (resp.data && resp.data.success === true) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.DONE, MessageConstant_1.default.UPLOAD_SUCCESSFUL);
}
else {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, resp.data.message);
}
process.exit(0);
}
catch (err) {
if (((_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, 'Authentication failed. Please try again with right credentials.');
process.exit(1);
}
else if (((_b = err === null || err === void 0 ? void 0 : err.response) === null || _b === void 0 ? void 0 : _b.status) === 400) {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, 'Invalid API Key. Please use the right API Key');
process.exit(1);
}
else if (((_c = err === null || err === void 0 ? void 0 : err.response) === null || _c === void 0 ? void 0 : _c.status) === 413 || err.message === 'Request body larger than maxBodyLength limit') {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, 'Request size is too large. Please try uploading files in batches. Maximum of 100 files and 400MB in size is allowed.');
process.exit(1);
}
else if (err.message === 'Directory not found') {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, 'Directory not found. Please ensure you have provided the correct location for sourcemaps');
process.exit(1);
}
else if (((_e = (_d = err === null || err === void 0 ? void 0 : err.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.message) === 'Unexpected field') {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, 'Error in uploading files. A maximum of 100 files can be uploaded at once. Please try uploading files in batches.');
process.exit(1);
}
else {
(0, utilityFunctions_1.cliLog)(Constant_1.default.ERROR, 'Error in uploading files. You may be using a incorrect API key. Please ensure all parameters are right and try again later.');
process.exit(1);
}
}
});
exports.uploadFile = uploadFile;