source: trip-planner-front/node_modules/karma/lib/plugin.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1'use strict'
2
3const fs = require('graceful-fs')
4const path = require('path')
5const helper = require('./helper')
6
7const log = require('./logger').create('plugin')
8
9const IGNORED_PACKAGES = ['karma-cli', 'karma-runner.github.com']
10
11function resolve (plugins, emitter) {
12 const modules = []
13
14 function requirePlugin (name) {
15 log.debug(`Loading plugin ${name}.`)
16 try {
17 modules.push(require(name))
18 } catch (e) {
19 if (e.code === 'MODULE_NOT_FOUND' && e.message.includes(name)) {
20 log.error(`Cannot find plugin "${name}".\n Did you forget to install it?\n npm install ${name} --save-dev`)
21 } else {
22 log.error(`Error during loading "${name}" plugin:\n ${e.message}`)
23 }
24 emitter.emit('load_error', 'plug_in', name)
25 }
26 }
27
28 plugins.forEach(function (plugin) {
29 if (helper.isString(plugin)) {
30 if (!plugin.includes('*')) {
31 requirePlugin(plugin)
32 return
33 }
34 const pluginDirectory = path.normalize(path.join(__dirname, '/../..'))
35 const regexp = new RegExp(`^${plugin.replace(/\*/g, '.*').replace(/\//g, '[/\\\\]')}`)
36
37 log.debug(`Loading ${plugin} from ${pluginDirectory}`)
38 fs.readdirSync(pluginDirectory)
39 .map((e) => {
40 const modulePath = path.join(pluginDirectory, e)
41 if (e[0] === '@') {
42 return fs.readdirSync(modulePath).map((e) => path.join(modulePath, e))
43 }
44 return modulePath
45 })
46 .reduce((a, x) => a.concat(x), [])
47 .map((modulePath) => path.relative(pluginDirectory, modulePath))
48 .filter((moduleName) => !IGNORED_PACKAGES.includes(moduleName) && regexp.test(moduleName))
49 .forEach((pluginName) => requirePlugin(path.join(pluginDirectory, pluginName)))
50 } else if (helper.isObject(plugin)) {
51 log.debug(`Loading inline plugin defining ${Object.keys(plugin).join(', ')}.`)
52 modules.push(plugin)
53 } else {
54 log.error(`Invalid plugin ${plugin}`)
55 emitter.emit('load_error', 'plug_in', plugin)
56 }
57 })
58
59 return modules
60}
61
62/**
63 Create a function to handle errors in plugin loading.
64 @param {Object} injector, the dict of dependency injection objects.
65 @return function closed over injector, which reports errors.
66*/
67function createInstantiatePlugin (injector) {
68 const emitter = injector.get('emitter')
69 // Cache to avoid report errors multiple times per plugin.
70 const pluginInstances = new Map()
71 return function instantiatePlugin (kind, name) {
72 if (pluginInstances.has(name)) {
73 return pluginInstances.get(name)
74 }
75
76 let p
77 try {
78 p = injector.get(`${kind}:${name}`)
79 if (!p) {
80 log.error(`Failed to instantiate ${kind} ${name}`)
81 emitter.emit('load_error', kind, name)
82 }
83 } catch (e) {
84 if (e.message.includes(`No provider for "${kind}:${name}"`)) {
85 log.error(`Cannot load "${name}", it is not registered!\n Perhaps you are missing some plugin?`)
86 } else {
87 log.error(`Cannot load "${name}"!\n ` + e.stack)
88 }
89 emitter.emit('load_error', kind, name)
90 }
91 pluginInstances.set(name, p, `${kind}:${name}`)
92 return p
93 }
94}
95
96createInstantiatePlugin.$inject = ['injector']
97
98module.exports = { resolve, createInstantiatePlugin }
Note: See TracBrowser for help on using the repository browser.