Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

data-directory.js 2.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. const assert = require('assert')
  2. const fs = require('fs')
  3. const path = require('path')
  4. const walk = require('walk-sync')
  5. const yaml = require('js-yaml')
  6. const { isRegExp, set } = require('lodash')
  7. const filenameToKey = require('./filename-to-key')
  8. module.exports = function dataDirectory (dir, opts = {}) {
  9. const defaultOpts = {
  10. preprocess: (content) => { return content },
  11. ignorePatterns: [/README\.md$/i],
  12. extensions: [
  13. '.json',
  14. '.md',
  15. '.markdown',
  16. '.yaml',
  17. '.yml'
  18. ]
  19. }
  20. opts = Object.assign({}, defaultOpts, opts)
  21. // validate input
  22. assert(Array.isArray(opts.ignorePatterns))
  23. assert(opts.ignorePatterns.every(isRegExp))
  24. assert(Array.isArray(opts.extensions))
  25. assert(opts.extensions.length)
  26. // start with an empty data object
  27. const data = {}
  28. // find YAML and Markdown files in the given directory, recursively
  29. const filenames = walk(dir, { includeBasePath: true })
  30. .filter(filename => {
  31. // ignore files that match any of ignorePatterns regexes
  32. if (opts.ignorePatterns.some(pattern => pattern.test(filename))) return false
  33. // ignore files that don't have a whitelisted file extension
  34. return opts.extensions.includes(path.extname(filename).toLowerCase())
  35. })
  36. const files = filenames.map(
  37. filename => [filename, fs.readFileSync(filename, 'utf8')]
  38. )
  39. files.forEach(([filename, fileContent]) => {
  40. // derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename
  41. const key = filenameToKey(path.relative(dir, filename))
  42. const extension = path.extname(filename).toLowerCase()
  43. if (opts.preprocess) fileContent = opts.preprocess(fileContent)
  44. // add this file's data to the global data object
  45. switch (extension) {
  46. case '.json':
  47. set(data, key, JSON.parse(fileContent))
  48. break
  49. case '.yaml':
  50. case '.yml':
  51. set(data, key, yaml.safeLoad(fileContent, { filename }))
  52. break
  53. case '.md':
  54. case '.markdown':
  55. set(data, key, fileContent)
  56. break
  57. }
  58. }
  59. )
  60. return data
  61. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...