@nimbella/postman-api
Version:
Postman Collection to Nimbella Project: Take your APIs seamlessly into Serverless world with this API
134 lines (133 loc) • 5.7 kB
JavaScript
;
/*
* Copyright (c) 2019 - present Nimbella Corp.
*
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendEnvFile = exports.writeReadMe = exports.writeEnvFile = exports.appendFile = exports.writeOrUpdateFile = exports.writeFile = exports.saveCollection = exports.upgrade = void 0;
const tslib_1 = require("tslib");
const fs_1 = require("fs");
const postman_collection_transformer_1 = require("postman-collection-transformer");
const path_1 = require("path");
const postman_collection_1 = require("postman-collection");
const chalk_1 = require("chalk");
const mkdirp_1 = require("mkdirp");
const logger_1 = tslib_1.__importDefault(require("./logger"));
const utils_1 = require("./utils");
function upgrade(fileLocation, version = '2.1.0') {
const transformOptions = {
inputVersion: '1.0.0',
outputVersion: version,
retainIds: true,
};
return postman_collection_transformer_1.convert(JSON.parse(fs_1.readFileSync(fileLocation).toString()), transformOptions, (error, result) => {
if (error) {
logger_1.default.error(error);
console.log(`Couldn't convert collection: ${fileLocation}`);
}
const extension = path_1.extname(fileLocation);
const fileName = `${path_1.basename(fileLocation, extension)}_converted_to_v${version}.json`;
const newFileLoc = path_1.join(path_1.dirname(fileLocation), fileName);
fs_1.writeFileSync(newFileLoc, JSON.stringify(result, null, 4));
console.log(`the converted version has been written at \n\t ${chalk_1.green(newFileLoc)}`);
return new postman_collection_1.Collection(result);
});
}
exports.upgrade = upgrade;
async function saveCollection(location, data) {
try {
fs_1.writeFileSync(location, JSON.stringify(data, null, 4));
console.log(`collection with updated endpoints and tests, has been written at \n\t ${chalk_1.green(location)}`);
return true;
}
catch (error) {
logger_1.default.error(error);
console.log(`Couldn't save collection at: ${location}`);
return false;
}
}
exports.saveCollection = saveCollection;
function writeFile(fileMeta, content) {
try {
if (!fs_1.existsSync(fileMeta.location)) {
mkdirp_1.sync(fileMeta.location);
}
fs_1.writeFileSync(path_1.join(fileMeta.location, `${fileMeta.name}.${fileMeta.ext}`), content);
if (fileMeta.verbose)
console.log(`${chalk_1.blue('created :')} ${fileMeta.location}/${fileMeta.name}.${fileMeta.ext}`);
}
catch (error) {
logger_1.default.error(error);
console.log(`Couldn't create ${fileMeta.name}.${fileMeta.ext} at ${fileMeta.location}`);
}
}
exports.writeFile = writeFile;
function writeOrUpdateFile(fileMeta, content, update) {
try {
if (!fs_1.existsSync(fileMeta.location)) {
mkdirp_1.sync(fileMeta.location);
}
const filePath = path_1.join(fileMeta.location, `${fileMeta.name}.${fileMeta.ext}`);
if (update) {
if (utils_1.canBeUpdated(filePath, content)) {
fs_1.writeFileSync(filePath, content);
if (fileMeta.verbose)
console.log(`${chalk_1.blue('updated :')} ${filePath}`);
}
}
else {
fs_1.writeFileSync(filePath, content);
if (fileMeta.verbose)
console.log(`${chalk_1.blue('created :')} ${filePath}`);
}
}
catch (error) {
logger_1.default.error(error);
console.log(`Couldn't write ${fileMeta.name}.${fileMeta.ext} at ${fileMeta.location}`);
}
}
exports.writeOrUpdateFile = writeOrUpdateFile;
function appendFile(fileMeta, content) {
try {
if (!fs_1.existsSync(fileMeta.location)) {
mkdirp_1.sync(fileMeta.location);
}
fs_1.appendFileSync(path_1.join(fileMeta.location, `${fileMeta.name}.${fileMeta.ext}`), content);
console.log(`${chalk_1.blue('updated :')} ${fileMeta.location}/${fileMeta.name}.${fileMeta.ext}`);
}
catch (error) {
logger_1.default.error(error);
console.log(`Couldn't update ${fileMeta.name}.${fileMeta.ext} at ${fileMeta.location}`);
}
}
exports.appendFile = appendFile;
async function writeEnvFile(location, data) {
if (data) {
writeFile({ location, name: '', ext: 'env', verbose: true }, data
.filter(e => e && e.key)
.map(e => `${e.key.toUpperCase()}=${e.value} #${e.description}`)
.join('\n'));
}
}
exports.writeEnvFile = writeEnvFile;
async function writeReadMe(location, content) {
writeFile({ location, name: 'readme', ext: 'md', verbose: true }, content);
}
exports.writeReadMe = writeReadMe;
async function appendEnvFile(location, data) {
if (data) {
fs_1.appendFileSync(path_1.join(location, '.env'), data
.filter(e => e && e.key)
.map(e => `${e.key.toUpperCase()}=${e.value.trim()}${e.description ? ` # ${e.description}` : ''}`)
.join('\n'));
}
}
exports.appendEnvFile = appendEnvFile;