forked from anvilresearch/connect-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (88 loc) · 2.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* global __dirname, process */
// Ensure running a compatible version of Node before getting too far...
var semver = require('semver')
var packageJson = require('./package.json')
if (packageJson.engines &&
packageJson.engines.node &&
!semver.satisfies(process.versions.node, packageJson.engines.node)) {
console.error('Incompatible version of node - running [%s] but require [%s]',
process.versions.node, packageJson.engines.node)
process.exit(1)
}
/**
* Dependencies
*/
var pkg = require('./package.json')
var fs = require('fs')
var path = require('path')
var nash = require('nash')
var cli = nash()
/**
* Settings
*/
cli.set({
pkg: pkg,
logo: fs.readFileSync(
path.join(__dirname, 'lib', 'logo.txt'), 'ascii'
),
website: 'http://anvil.io',
description: 'A modern authorization server built to\n' +
'authenticate your users and protect your APIs.'
})
/**
* Register modules
*
* Requires and registers plugins.
*/
cli.registerModules = function (modules, callback) {
var plugins = []
modules.forEach(function (jsModule) {
var isDirectory = false
try {
isDirectory = fs.lstatSync(jsModule).isDirectory()
} catch (e) {}
// Directory was passed; enumerate through and register contents
if (isDirectory) {
var modules = fs.readdirSync(jsModule)
modules.forEach(function (mod) {
if (path.extname(mod) === '.js' && path.basename(mod) !== 'index.js') {
plugins.push({
register: require(path.join(jsModule, mod))
})
}
})
// Single file was passed; register it
} else {
plugins.push({
register: require(jsModule + '.js')
})
}
})
cli.register(plugins, function (err) {
callback(err)
})
}
/**
* Initialize
*
* Registers commands and plugins
*/
cli.initialize = function (callback) {
cli.registerModules([
path.join(__dirname, 'lib', 'plugins', 'logger'),
path.join(__dirname, 'lib', 'plugins', 'env'),
path.join(__dirname, 'lib', 'plugins', 'fs'),
path.join(__dirname, 'lib', 'plugins', 'json'),
path.join(__dirname, 'lib', 'plugins', 'yaml'),
path.join(__dirname, 'lib', 'plugins', 'prompt'),
path.join(__dirname, 'lib', 'plugins', 'config'),
path.join(__dirname, 'lib', 'plugins', 'issuers'),
path.join(__dirname, 'lib', 'plugins', 'client'),
path.join(__dirname, 'lib', 'plugins', 'login'),
path.join(__dirname, 'lib', 'commands')
], callback)
}
/**
* Exports
*/
module.exports = cli