All files / src index.js

46.67% Statements 7/15
0% Branches 0/2
50% Functions 2/4
46.67% Lines 7/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63              1x                 1x 1x 1x                                           1x 1x                       1x                  
import fs from 'fs'
import get from 'lodash/fp/get'
import express from 'express'
import { Builder } from 'postman-sdk'
import buildCollection from './lib/build-collection'
import buildEnvironment from './lib/build-environment'
import save from './lib/save'
const { collection } = Builder
 
/**
 * Builds a postman collection
 *
 * @param {Object} app The models object
 * @param {Object} config The config object
 * @returns {Object} The collection file
 */
const collectionGenerator = (app, config) => {
	const router = express.Router()
	const meta = checkVersion()
 
	router.route('/generate-collection').get(async (req, res, next) => {
		const newCollection = await buildCollection(
			collection(meta.name, meta.version),
			app._router,
			config
		)
 
		const newEnvironments = buildEnvironment(
			newCollection.collection,
			config.environments
		)
 
		return res.json(
			await save(req.query.postman || '', newCollection, newEnvironments)
		)
	})
 
	return router
}
 
const checkVersion = () => {
    console.log(fs.realpathSync())
	//const packageJson = JSON.parse(String(fs.readFileSync(__dirname)))
	//console.log(packageJson)
}
 
/**
 * Response helper
 * @param {object} req The request object
 * @param {object} res The response object
 * @param {function} next The callback function
 * @returns {undefined} Nothing
 */
const error = (req, res, next) => {
	req.fulfilled = true
	res.data = {
		message: 'The postman collection generator is disabled!'
	}
	return next()
}
 
export default collectionGenerator