projex
Version:
A command line to manage the workflow
85 lines (84 loc) • 3.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeployUtils = void 0;
const _api_1 = require("../../../../../api/index");
const _shared_1 = require("../../../../../shared/index");
const axios_1 = __importDefault(require("axios"));
const fs_1 = __importDefault(require("fs"));
class DeployUtils {
directoryUtils = new _shared_1.DirectoryUtils();
promptUtils = new _shared_1.PromptsUtils();
token;
account;
userInfo;
site;
preConfirm;
constructor(site, preConfirm) {
this.site = site;
this.preConfirm = preConfirm;
this.token = '';
this.account = '';
this.userInfo = '';
}
getLocalInformation = async () => {
// 3 Get the vtex and account tocket. put together the url to be used
this.token = await (0, _shared_1.runOnlyCommand)(_shared_1.Commands.GET_TOKEN);
this.userInfo = await (0, _shared_1.runOnlyCommand)(_shared_1.Commands.GET_ACCOUNT);
this.account = (0, _shared_1.getAccountName)(this.userInfo);
_shared_1.log.debug(`Use the account ${_api_1.Colors.PINK(this.account)}`);
};
prepare = async (files) => {
!this.preConfirm &&
(await this.promptUtils.continuePrompt(`you are about to upload the files in the account ${_api_1.Colors.WARNING(this.account)} with de site configuration ${_api_1.Colors.WARNING(this.site)}. Do you want to continue?`));
_shared_1.log.info('uploading files...');
const results = files.map(async (item) => {
const url = _shared_1.Endpoints.UPLOAD_FILE(this.account.replace(/\s/g, ''), item.name, this.site);
const data = await fs_1.default.readFileSync(item.path, 'utf8');
_shared_1.log.info(`Uploading the file ${item.name}`);
await this.uploadFile({
data,
url,
token: this.token.replace(/\s/g, ''),
name: item.name,
});
});
Promise.all(results).then(() => {
_shared_1.log.info('files uploaded successfully to the account', this.account);
});
};
getFilesToUpload = async (extension, preConfirm) => {
// 1 Get the files from the current directory.
let files = await this.directoryUtils.getFilesInDirectory(extension);
if (!files.length) {
_shared_1.log.error(_api_1.Colors.ERROR('no files found'));
throw new Error('No files found');
}
// 2 Select the files to use
if (!preConfirm) {
const selected = await this.directoryUtils.promptSelectElements(Object.assign([], files), 'Select the files to upload', 'Choose the files to upload');
files = await this.directoryUtils.getSelectedElements(files, selected);
}
_shared_1.log.info('files to upload:', files.map((item) => item.name));
return files;
};
/* The `uploadFile` function is a method of the `DeployUtils` class. It takes in an object with
properties `token`, `url`, `data`, and `name`, all of which are of type `string`. */
uploadFile = ({ token, url, data, name }) => {
const cookie = String(`VtexIdclientAutCookie=${token}`);
return (0, axios_1.default)({
method: 'put',
url,
data: {
path: name,
text: data,
},
headers: {
Cookie: cookie,
},
});
};
}
exports.DeployUtils = DeployUtils;