/** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2020-08-25 15:20:02 * @modify date 2020-08-25 15:36:41 * @desc [description] */ import { readFile, writeFile } from 'fs/promises'; import { interfaces } from 'inversify'; import { join, resolve } from 'path'; import "reflect-metadata"; import { createPath, exists, listFiles } from '../../../lib/helpers/fileMethods'; import { SystemLogger } from '../../mod-Logger/src/Unique.Logger'; import { DEFAULT_CONFIG_FILE, DEFAULT_SEARCHPATH, FILE_ENDING } from '../constants/constants'; import { IDescriptor } from '../type/interfaces'; /** * Function to List the Assemblies. * @param path */ export async function listAssemblies(path: string = DEFAULT_SEARCHPATH) { const _loadedAssemblieFiles = new Array(); /** Scan for assembly Files and add them if necessary */ for (const _fileName of await listFiles(path, FILE_ENDING)) { /** Load the Assembly to Verify whether it was already manual loaded into the Container */ try { const _assembly = await import(resolve(_fileName)); _loadedAssemblieFiles.push({ name: _assembly.NAME, path: resolve(_fileName), requires: _assembly.REQUIRE || [] } ); } catch (e) { SystemLogger.logger.getLogger('cotainer-builder', 'debug').error(e, 'Failed Loading the Assembly ' + _fileName); } } return _loadedAssemblieFiles; } /** * Function to Load the Assemblies. * @param _assemblies */ export async function loadAssemblies(_assemblies: Array<{ name: string, path: string, requires: Array }>): Promise< Array<{ name: string, path: string, requires: Array, export: Array, handlers: [(context: interfaces.Context, instance: any) => any] }> > { for (const _assembly of _assemblies) { const _load = (await import(resolve(_assembly.path))); _assembly['export'] = _load.EXPORT; _assembly['handlers'] = _load.REQUIREDACTIVATIONHANDLERS || []; } //@ts-ignore Members are added return _assemblies; } /** * Function to write the default Config. * * @export * @returns */ export async function writeDefaultConfig(_pathToFile: string = '') { /** List all Files which are used to describe an assembly */ const _assemblies = await listAssemblies(DEFAULT_SEARCHPATH); /** Create the File Name */ const _dirName = join(resolve(process.cwd()), 'config'); let _fileName = join(await createPath(_dirName), DEFAULT_CONFIG_FILE); /** If a Path is given use this Path instead*/ if (_pathToFile) { _fileName = _pathToFile; } let _config = {}; /** Check if a Config exists => If So Extend the given config */ if (await exists(_fileName)) { _config = JSON.parse(await readFile(_fileName, { encoding: 'utf-8' })); } /** Store the Config */ _config['assemblies'] = _assemblies; /** Write the new Configuration */ await writeFile(_fileName, JSON.stringify(_config, undefined, 4)); SystemLogger.logger.getLogger('cotainer-builder', 'debug').info('Writing new Assembly config in' + _fileName); /** Return the created Filename */ return _fileName; } /** * Function to write the User Config */ export async function writeUserConfig() { /** List all Files which are used to describe an assembly */ const _assemblies = await listAssemblies(DEFAULT_SEARCHPATH); // Import Inquirer and the Fuzzy-Path Module const inquirer = require('inquirer'); inquirer.registerPrompt('path', require('inquirer-fuzzy-path')) const nameInput = { type: 'input', name: 'fileName', message: "Enter the Configuration File Name", default: DEFAULT_CONFIG_FILE, validate: function (value: string) { if (value.endsWith('.json')) { return true; } return 'Please add the Enter a Valid Filename (including .json)'; } }; const files = { type: 'checkbox', name: 'elements', message: "Select/Deselect the Assemblies", choices: [] }; _assemblies.map(element => { files.choices.push({ name: element.name, checked: true }) }) const result = await inquirer.prompt( [ nameInput, files ] ) /** Create the File Name */ const _dirName = join(resolve(process.cwd()), 'config'); let _fileName = join(await createPath(_dirName), result.fileName); let _config = {}; /** Check if a Config exists => If So Extend the given config */ if (await exists(_fileName)) { _config = JSON.parse(await readFile(_fileName, { encoding: 'utf-8' })); } /** Store the Config */ _config['assemblies'] = _assemblies.filter(element => { if (result.elements.includes(element.name)) { return element; } }); /** Write the new Configuration */ await writeFile(_fileName, JSON.stringify(_config, undefined, 4)); SystemLogger.logger.getLogger('cotainer-builder', 'debug').info('Writing new Assembly config in' + _fileName); /** Return the created Filename */ return _fileName; }