const { resolve } = require('path')
const { readFileSync } = require('fs')
const { logError, logInfo } = require('../log')
/**
* Platform template definition
* @typedef {Object} PlatformTemplate
* @property {string} platformName - Name of platform
* @property {string} domain - example.com
* @property {string[]} supportedDeploymentTypes - container
* @property {string[]} supportedEnvironments - qa
*
* @property {string} awsAccountId - Aws account id
* @property {string=} awsRegion - ?
* @property {string=} awsKeyPairName - ?
* @property {string} awsRoleToAssume - ?
* @property {string=} awsDeploymentSqsArn - ?
*
* @property {string=} ciSubDomain - ?
* @property {string|mumber=} ciInternalServerPort - ? | number;
* @property {string=} ciServerName - ?
* @property {string=} ciDockerNetworkName - ?
* @property {string} ciGithubActionsRepo - organisation/repoName
*/
/**
* Utility class that helps you read and validate platfor templates from file system
* @class
*/
class PlatformTemplateService {
/**
* Read and validat platform template(s) from file system
*
* @param {string} path - File path to platform templates json
* @return @Async {PlatformTemplate[]} - List of platform templates
*
* @example
* const platformTemplate = PlatformTemplateService.readFromFile('./platforms.json')
*/
static readFromFile(path) {
if (!path) {
logError({ message: '[PlatformTemplateService] No path provided' })
return Promise.reject()
}
const fileData = readFileSync(resolve(path), 'utf8')
if (!fileData && typeof fileData !== 'string') {
logError({ message: `[PlatformTemplateService] Could not find a deployment platform at ${path}` })
return Promise.reject()
}
let potentialTemplates = JSON.parse(fileData)
if (!Array.isArray(potentialTemplates)) {
potentialTemplates = [potentialTemplates]
}
const templates = potentialTemplates.map(PlatformTemplateService.fakeValidateTemplate)
logInfo({ message: `[PlatformTemplateService]: Found platforms [${templates.map(x => x.platformName).join(', ')}]` })
return Promise.resolve(templates)
}
/**
* Validates the template
*
* @param {PlatformTemplate} platformTemplate - File path to platform templates json
* @return @Async {PlatformTemplate} - List of platform templates
*
* @example
* const platformTemplate = PlatformTemplateService.readFromFile('./platforms.json')
*/
static fakeValidateTemplate(obj) {
// todo: implement real one
return obj
}
}
module.exports = {
PlatformTemplateService
}