.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / browserslist / node.js
1 var path = require('path')
2 var fs = require('fs')
3
4 var BrowserslistError = require('./error')
5
6 var IS_SECTION = /^\s*\[(.+)\]\s*$/
7 var CONFIG_PATTERN = /^browserslist-config-/
8 var SCOPED_CONFIG__PATTERN = /@[^./]+\/browserslist-config(-|$)/
9
10 var filenessCache = { }
11 var configCache = { }
12
13 function checkExtend (name) {
14   var use = ' Use `dangerousExtend` option to disable.'
15   if (!CONFIG_PATTERN.test(name) && !SCOPED_CONFIG__PATTERN.test(name)) {
16     throw new BrowserslistError(
17       'Browserslist config needs `browserslist-config-` prefix. ' + use)
18   }
19   if (name.indexOf('.') !== -1) {
20     throw new BrowserslistError(
21       '`.` not allowed in Browserslist config name. ' + use)
22   }
23   if (name.indexOf('node_modules') !== -1) {
24     throw new BrowserslistError(
25       '`node_modules` not allowed in Browserslist config.' + use)
26   }
27 }
28
29 function isFile (file) {
30   if (file in filenessCache) {
31     return filenessCache[file]
32   }
33   var result = fs.existsSync(file) && fs.statSync(file).isFile()
34   if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
35     filenessCache[file] = result
36   }
37   return result
38 }
39
40 function eachParent (file, callback) {
41   var loc = path.resolve(file)
42   do {
43     var result = callback(loc)
44     if (typeof result !== 'undefined') return result
45   } while (loc !== (loc = path.dirname(loc)))
46   return undefined
47 }
48
49 function pickEnv (config, opts) {
50   if (typeof config !== 'object') return config
51
52   var name
53   if (typeof opts.env === 'string') {
54     name = opts.env
55   } else if (process.env.BROWSERSLIST_ENV) {
56     name = process.env.BROWSERSLIST_ENV
57   } else if (process.env.NODE_ENV) {
58     name = process.env.NODE_ENV
59   } else {
60     name = 'development'
61   }
62
63   return config[name] || config.defaults
64 }
65
66 function parsePackage (file) {
67   var config = JSON.parse(fs.readFileSync(file))
68   if (config.browserlist && !config.browserslist) {
69     throw new BrowserslistError(
70       '`browserlist` key instead of `browserslist` in ' + file)
71   }
72   var list = config.browserslist
73   if (typeof list === 'object' && list.length) {
74     list = { defaults: list }
75   }
76   return list
77 }
78
79 module.exports = {
80   loadQueries: function loadQueries (context, name) {
81     if (!context.dangerousExtend) checkExtend(name)
82     // eslint-disable-next-line security/detect-non-literal-require
83     var queries = require(name)
84     if (!Array.isArray(queries)) {
85       throw new BrowserslistError(
86         '`' + name + '` config exports not an array of queries')
87     }
88     return queries
89   },
90
91   getStat: function getStat (opts) {
92     var stats
93     if (opts.stats) {
94       stats = opts.stats
95     } else if (process.env.BROWSERSLIST_STATS) {
96       stats = process.env.BROWSERSLIST_STATS
97     } else if (opts.path && path.resolve && fs.existsSync) {
98       stats = eachParent(opts.path, function (dir) {
99         var file = path.join(dir, 'browserslist-stats.json')
100         return isFile(file) ? file : undefined
101       })
102     }
103
104     if (typeof stats === 'string') {
105       try {
106         stats = JSON.parse(fs.readFileSync(stats))
107       } catch (e) {
108         throw new BrowserslistError('Can\'t read ' + stats)
109       }
110     }
111
112     return stats
113   },
114
115   loadConfig: function loadConfig (opts) {
116     if (process.env.BROWSERSLIST) {
117       return process.env.BROWSERSLIST
118     } else if (opts.config || process.env.BROWSERSLIST_CONFIG) {
119       var file = opts.config || process.env.BROWSERSLIST_CONFIG
120       if (path.basename(file) === 'package.json') {
121         return pickEnv(parsePackage(file), opts)
122       } else {
123         return pickEnv(module.exports.readConfig(file), opts)
124       }
125     } else if (opts.path) {
126       return pickEnv(module.exports.findConfig(opts.path), opts)
127     } else {
128       return undefined
129     }
130   },
131
132   parseConfig: function parseConfig (string) {
133     var result = { defaults: [] }
134     var section = 'defaults'
135
136     string.toString()
137       .replace(/#[^\n]*/g, '')
138       .split(/\n/)
139       .map(function (line) {
140         return line.trim()
141       })
142       .filter(function (line) {
143         return line !== ''
144       })
145       .forEach(function (line) {
146         if (IS_SECTION.test(line)) {
147           section = line.match(IS_SECTION)[1].trim()
148           result[section] = result[section] || []
149         } else {
150           result[section].push(line)
151         }
152       })
153
154     return result
155   },
156
157   readConfig: function readConfig (file) {
158     if (!isFile(file)) {
159       throw new BrowserslistError('Can\'t read ' + file + ' config')
160     }
161     return module.exports.parseConfig(fs.readFileSync(file))
162   },
163
164   findConfig: function findConfig (from) {
165     from = path.resolve(from)
166
167     var cacheKey = isFile(from) ? path.dirname(from) : from
168     if (cacheKey in configCache) {
169       return configCache[cacheKey]
170     }
171
172     var resolved = eachParent(from, function (dir) {
173       var config = path.join(dir, 'browserslist')
174       var pkg = path.join(dir, 'package.json')
175       var rc = path.join(dir, '.browserslistrc')
176
177       var pkgBrowserslist
178       if (isFile(pkg)) {
179         try {
180           pkgBrowserslist = parsePackage(pkg)
181         } catch (e) {
182           if (e.name === 'BrowserslistError') throw e
183           console.warn(
184             '[Browserslist] Could not parse ' + pkg + '. Ignoring it.')
185         }
186       }
187
188       if (isFile(config) && pkgBrowserslist) {
189         throw new BrowserslistError(
190           dir + ' contains both browserslist and package.json with browsers')
191       } else if (isFile(rc) && pkgBrowserslist) {
192         throw new BrowserslistError(
193           dir + ' contains both .browserslistrc and package.json with browsers')
194       } else if (isFile(config) && isFile(rc)) {
195         throw new BrowserslistError(
196           dir + ' contains both .browserslistrc and browserslist')
197       } else if (isFile(config)) {
198         return module.exports.readConfig(config)
199       } else if (isFile(rc)) {
200         return module.exports.readConfig(rc)
201       } else {
202         return pkgBrowserslist
203       }
204     })
205     if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
206       configCache[cacheKey] = resolved
207     }
208     return resolved
209   },
210
211   clearCaches: function clearCaches () {
212     filenessCache = { }
213     configCache = { }
214   }
215 }