UNPKG

node-dependency-injection

Version:

The NodeDependencyInjection component allows you to standardize and centralize the way objects are constructed in your application.

122 lines (111 loc) 2.99 kB
#! /usr/bin/env node import chalk from 'chalk' import path from 'path' import program from 'commander' import util from 'util' import { ContainerBuilder, YamlFileLoader } from '../lib/' import JsAdapter from './Services/File/JsAdapter' import JsonAdapter from './Services/File/JsonAdapter' import XmlAdapter from './Services/File/XmlAdapter' import JsFileLoader from '../lib/Loader/JsFileLoader' import JsonFileLoader from '../lib/Loader/JsonFileLoader' import XmlFileLoader from '../lib/Loader/XmlFileLoader' import 'console.table' program.arguments('<path> <service>').action((dir, service) => { const container = new ContainerBuilder() dir = path.isAbsolute(dir) ? dir : path.join(process.cwd(), dir) let loader switch (path.extname(dir).split('.').pop()) { case JsAdapter.FORMAT: loader = new JsFileLoader(container) break case JsonAdapter.FORMAT: loader = new JsonFileLoader(container) break case XmlAdapter.FORMAT: loader = new XmlFileLoader(container) break default: loader = new YamlFileLoader(container) } console.info(chalk.blue(`Checking ${dir}...\n`)) try { loader.load(dir) } catch (e) { console.info(chalk.bold.red(`ERROR! ${e.message}`)) process.exit(1) } const def = container.definitions.get(service) if (!def) { console.info(chalk.bold.red(`Service \`${service}\` not found`)) process.exit(1) } let argumentText = '' for (const argument of def.args) { argumentText += `\n\t- ${(argument.constructor.name)}:\t${chalk.green(argument.id)}` } console.table([ { Attribute: 'Key', Value: service }, { Attribute: 'Class Name', Value: def.Object.name }, { Attribute: 'Arguments', Value: argumentText }, { Attribute: 'Public', Value: def.public.toString() }, { Attribute: 'Calls', Value: def.calls.length > 0 ? util.inspect(def.calls, false, null) : '[]' }, { Attribute: 'Tags', Value: def.tags.length > 0 ? util.inspect(def.tags, false, null) : '[]' }, { Attribute: 'Properties', Value: def.properties.size > 0 ? util.inspect(def.properties, false, null) : '[]' }, { Attribute: 'Laziness', Value: def.lazy.toString() }, { Attribute: 'Deprecated Message', Value: def.deprecated }, { Attribute: 'Factory', Value: util.inspect(def.factory) }, { Attribute: 'Synthetic', Value: def.synthetic.toString() }, { Attribute: 'Decoration', Value: (def.decoratedService) ? `${def.decoratedService} ${def.decorationPriority}` : 'null' }, { Attribute: 'Shared', Value: def.shared }, { Attribute: 'Parent', Value: def.parent } ]) process.exit(0) }).parse(process.argv) if (!process.argv.slice(2).length) { program.outputHelp((helpText) => { return chalk.bold.red(helpText) }) }