import { Command } from 'commander'; import { parse, stringify } from 'yaml'; import fs from 'fs'; import { readUser, useProfile } from '../../helpers/cluserHelper'; import { v4 } from 'uuid'; import { mainYaml } from '../../components/mainYaml'; import { variablesYaml } from '../../components/variables'; import { deploymentYaml } from '../../components/deploymentYaml'; async function writeVariables(path, options) { var clusterUser = readUser(); let variables = parse(variablesYaml()) as any; var templateId = v4(); variables['variables']['**TemplateId**'] = templateId; variables['variables']['**SelfConnectorId**'] = v4(); variables['variables']['**AuthorId**'] = clusterUser.id; variables['variables']['**AuthorFirstName**'] = clusterUser.firstName; variables['variables']['**AuthorLastName**'] = clusterUser.lastName; variables['variables']['**AuthorEmail**'] = clusterUser.email; variables['variables']['**TemplateDatabaseName**'] = `worker_${templateId.replace('-', '_')}`; fs.writeFileSync( `${path}/partials/variables.yaml`, stringify(variables, { keepSourceTokens: true, lineWidth: 0 }), ); } async function initProfile(path, options) { console.log(`Initializing ${path}`, options); if (!fs.existsSync(path)) { fs.mkdirSync(path); } if (!fs.lstatSync(path).isDirectory()) { throw new Error(`The path ${path} is not a directory.`); } if (fs.readdirSync(path).length !== 0) { throw new Error(`The directory ${path} is not empty.`); } fs.mkdirSync(`${path}/partials`); fs.mkdirSync(`${path}/partials/pullers`); fs.mkdirSync(`${path}/partials/pushers`); fs.mkdirSync(`${path}/partials/dependencies`); fs.mkdirSync(`${path}/partials/webhooks`); fs.mkdirSync(`${path}/partials/reports`); fs.mkdirSync(`${path}/partials/objects`); fs.writeFileSync(`${path}/local.env`, ''); fs.writeFileSync(`${path}/partials/main.yaml`, mainYaml()); fs.writeFileSync(`${path}/deployment.yaml`, deploymentYaml()); await writeVariables(path, options); useProfile(path, options); } export function InitProfileCommand(): Command { const cmd = new Command('init') .alias('in') .description('Initializes a HexaSync Integration Project.') .argument('folder', 'Path to the target project') .option( '-c, --create-empty-profile', 'Request HexaSync to create an empty profile', ) .action(initProfile); return cmd; }