superconf 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js DELETED
@@ -1,216 +0,0 @@
1
- 'use strict'
2
-
3
- const CoffeeScript = require('coffeescript').compile
4
- const fs = require('fs')
5
- const path = require('path')
6
- const yaml = require('js-yaml')
7
-
8
- class Superconf {
9
- constructor (opts) {
10
- opts = opts || {}
11
-
12
- this.files = opts.files || [
13
- '%s.json',
14
- '%s.cson',
15
- '%s.yaml',
16
- '%s.yml',
17
- '.%src',
18
- '.%src.json',
19
- '.%src.cson',
20
- '.%src.yaml',
21
- '.%src.yml',
22
- 'package.json'
23
- ]
24
-
25
- if (opts.defaultConf) {
26
- this.files.push(opts.defaultConf)
27
- }
28
-
29
- this.cwd = opts.cwd || process.cwd()
30
- }
31
-
32
- getFirstExisting (name) {
33
- for (let file of this.files) {
34
- let filepath = path.resolve(this.cwd, file.replace('%s', name))
35
- try {
36
- fs.accessSync(filepath)
37
- if (file === 'package.json') {
38
- const json = require(filepath)
39
- if (json[name]) return filepath
40
- } else {
41
- return filepath
42
- }
43
- } catch (err) {
44
-
45
- }
46
- }
47
-
48
- return null
49
- }
50
-
51
- tryFiles (name) {
52
- if (!name) {
53
- throw new Error('Name arg must be set!')
54
- }
55
-
56
- let confFile = this.getFirstExisting(name)
57
-
58
- if (!confFile) {
59
- return null
60
- }
61
-
62
- let ext = path.extname(confFile)
63
- let json
64
-
65
- try {
66
- if (ext === '.json') {
67
- json = require(confFile)
68
- if (path.basename(confFile) === 'package.json') {
69
- json = json[name]
70
- }
71
- } else {
72
- let source = fs.readFileSync(confFile, { encoding: 'utf8' })
73
-
74
- if (ext === '.cson') {
75
- let js = CoffeeScript('module.exports =\n' + source)
76
- let module = {}
77
- eval(js) // eslint-disable-line no-eval
78
- json = module.exports
79
- } else if (ext === '.yaml' || ext === '.yml') {
80
- json = yaml.safeLoad(source)
81
- } else {
82
- let source = fs.readFileSync(confFile, { encoding: 'utf8' })
83
- eval('json = ' + source) // eslint-disable-line no-eval
84
- }
85
- }
86
- } catch (err) {
87
- throw new SyntaxError('Could not parse config file: ' + confFile + '\n\n' + err)
88
- }
89
-
90
- if (json) {
91
- return json
92
- }
93
- }
94
-
95
- config (conf) {
96
- this.mergeConf = {
97
- dept: conf.dept || 0
98
- }
99
-
100
- if (conf.cwd) {
101
- this.cwd = conf.cwd
102
- }
103
-
104
- return this
105
- }
106
-
107
- copy (obj) {
108
- if (typeof obj !== 'object' || obj === null) {
109
- return obj
110
- }
111
-
112
- if (Array.isArray(obj)) {
113
- return obj.map((item) => this.copy(item))
114
- }
115
-
116
- const copied = {}
117
- for (const key in obj) {
118
- if (obj.hasOwnProperty(key)) {
119
- if (obj[key] === null) {
120
- copied[key] = null
121
- continue
122
- }
123
-
124
- if (Array.isArray(obj[key])) {
125
- copied[key] = obj[key].map((item) => this.copy(item))
126
- continue
127
- }
128
-
129
- if (typeof obj[key] === 'object') {
130
- copied[key] = this.copy(obj[key])
131
- continue
132
- }
133
-
134
- copied[key] = obj[key]
135
- }
136
- }
137
-
138
- return copied
139
- }
140
-
141
- merge () {
142
- let conf = {}
143
- let args = Array.prototype.slice.call(arguments)
144
-
145
- let dept = this.mergeConf ? this.mergeConf.dept : 0
146
-
147
- let merge = function (left, right, curdept) {
148
- if (left === undefined || left === null) {
149
- left = {}
150
- }
151
-
152
- if (typeof right !== 'object') {
153
- return left
154
- }
155
-
156
- for (let key in right) {
157
- if (right.hasOwnProperty(key)) {
158
- if (right[key] === undefined) {
159
- continue
160
- }
161
-
162
- if (right[key] === null) {
163
- left[key] = null
164
- continue
165
- }
166
-
167
- if (Array.isArray(right[key])) {
168
- left[key] = right[key].map(item => {
169
- if (typeof item === 'object') {
170
- return JSON.parse(JSON.stringify())
171
- }
172
-
173
- return item
174
- })
175
- continue
176
- }
177
-
178
- if (typeof right[key] === 'object') {
179
- if (curdept) {
180
- left[key] = merge(left[key], right[key], left[key]--)
181
- continue
182
- }
183
-
184
- left[key] = Object.assign({}, right[key])
185
- continue
186
- }
187
-
188
- left[key] = right[key]
189
- }
190
- }
191
-
192
- return left
193
- }
194
-
195
- for (let i = 0; i < args.length; i++) {
196
- conf = merge(conf, args[i], dept)
197
- }
198
-
199
- return conf
200
- }
201
- }
202
-
203
- module.exports = function (name, opts) {
204
- let sc = new Superconf(opts)
205
- return sc.tryFiles(name)
206
- }
207
-
208
- module.exports.config = function (conf) {
209
- let sc = new Superconf()
210
- return sc.config(conf)
211
- }
212
-
213
- module.exports.Superconf = Superconf
214
- module.exports.merge = Superconf.prototype.merge
215
- module.exports.copy = Superconf.prototype.copy.bind(Superconf.prototype)
216
- module.exports.Superconf = Superconf