projex
Version:
A command line to manage the workflow
102 lines (101 loc) • 4.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDirectoryFiles = exports.backup = 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"));
// variable indicating where files are found to employees
let directory = '';
const backup = async (site) => {
if (!site) {
_shared_1.log.error(_api_1.Colors.ERROR('you must specify the site to make the backup'));
process.exit(1);
}
_shared_1.log.info(`Backup files from the site ${_api_1.Colors.PINK(site)}`);
// Selected the current directory to create the backup folder
directory = process.cwd() + '/';
// I create the backup directory
_shared_1.log.info(`Backup directory: ${_api_1.Colors.PINK(directory + 'backup')}`);
await createDirectory(directory);
// 3 Get the vtex and account tocket. put together the url to be used
_shared_1.log.info('Obtaining the token and the account to use');
const token = await (0, _shared_1.runOnlyCommand)(_shared_1.Commands.GET_TOKEN);
const userInfo = await (0, _shared_1.runOnlyCommand)(_shared_1.Commands.GET_ACCOUNT);
const account = (0, _shared_1.getAccountName)(userInfo);
_shared_1.log.debug(`Use the account ${account}`);
const url = _shared_1.Endpoints.GET_DIRECTORY_FILES(account.replace(/\s/g, ''), site);
// I get the files that are in the vtex cms files
_shared_1.log.info('Obtaining the current directories on vtex');
const allDirectories = await (0, exports.getDirectoryFiles)({
token: token.replace(/\s/g, ''),
url,
});
_shared_1.log.verbose('all directories found in vtex');
_shared_1.log.verbose(allDirectories);
if (allDirectories.length) {
const filterDirectory = allDirectories.filter((item) => {
if ((!item.endsWith('.map') && item.endsWith('.css')) || item.endsWith('.js'))
return item;
});
_shared_1.log.verbose('we removed all the directories that cannot be downloaded, Only download files with the extensión .css and js');
_shared_1.log.verbose(filterDirectory);
await downloadFiles(filterDirectory, account.replace(/\s/g, ''));
}
else
_shared_1.log.error(_api_1.Colors.ERROR('no files found'));
};
exports.backup = backup;
const downloadFiles = async (directories, account) => {
_shared_1.log.info('Downloading files');
const response = directories.map(async (item) => {
const urlGetFiles = _shared_1.Endpoints.GET_CONTENT_FILES(account, item);
_shared_1.log.info(`Downloading the file ${item}`);
const request = await (0, axios_1.default)({
method: 'GET',
url: urlGetFiles,
responseType: 'stream',
});
if (request.data) {
request.data.pipe(fs_1.default.createWriteStream('./backup/' + item));
}
else {
_shared_1.log.error(_api_1.Colors.ERROR(`error on download the file ${item} with the url ${urlGetFiles}`));
}
});
return Promise.all(response).then((response) => {
_shared_1.log.info('files downloaded successfully');
return response;
});
};
const createDirectory = async (directory) => {
const existDirectory = await fs_1.default.existsSync(directory + 'backup');
if (!existDirectory) {
fs_1.default.mkdirSync(directory + 'backup');
return true;
}
else {
return false;
}
};
const getDirectoryFiles = async ({ token, url }) => {
const cookie = String(`VtexIdclientAutCookie=${token}`);
const response = await (0, axios_1.default)({
method: 'get',
url,
headers: {
Cookie: cookie,
},
});
if (response.status == 200) {
const directory = response.data;
return directory;
}
else {
return [];
}
};
exports.getDirectoryFiles = getDirectoryFiles;