This repository was archived by the owner on Aug 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (97 loc) · 3.73 KB
/
index.js
File metadata and controls
117 lines (97 loc) · 3.73 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict'
const path = require('path');
const fs = require('fs');
const utils = require('loader-utils');
const yaml = require('js-yaml');
const matter = require('gray-matter');
module.exports = function(source){
this.cacheable && this.cacheable();
// lets load the shared locals object
let loader = utils.getOptions(this);
let locals = loader.locals;
const file_to_locals = (ref, dir) => {
// read the data directory
if (dir) {
ref.forEach((file) => {
// make sure its a yaml file
if( file.includes('yaml') || file.includes('yml') ){
let file_name = file.split(".")[0]
if (file_name){
let data = path.resolve(`${dir}/${file}`)
this.addDependency( data )
let temp = yaml.safeLoad( fs.readFileSync(data, 'utf8') );
if (temp) {
locals[file_name] = temp
}
}
}
})
}
}
const pages_to_locals = (ref, dir) => {
// read the pages directory
if (dir) {
locals.pages = Object.create({})
ref.forEach((file) => {
// make sure its a yaml file
if( file.includes('md') || file.includes('markdown') ){
let file_name = file.split(".")[0]
if (file_name){
let data = path.resolve(`${dir}/${file}`)
this.addDependency( data )
locals.pages[file_name] = matter(fs.readFileSync(data, 'utf8')).data;
}
}
})
}
}
const file_to_json = (ref, dir) => {
let output = loader.import.output
if (output.dir) {
// create json folder if it doesn't exist
if (!fs.existsSync(output.dir)) {
fs.mkdirSync (output.dir)
}
ref.forEach((file) => {
if( file.includes('yaml') || file.includes('yml') ){
let data = path.resolve(`${dir}/${file}`)
this.addDependency( data )
let file_name = file.split(".")[0]
let file_name_path = `${output.dir}/${file_name}.json`
if (!output.files || output.files.includes (file_name) ) {
fs.writeFileSync(
file_name_path,
JSON.stringify (locals[file_name])
)
}
}
})
}
}
if (!locals.load) {
// get which config options to load
// get the data property of that load configuration
if (loader.import.data) {
let data_dir = loader.import.data
let data_ref = fs.readdirSync (data_dir)
file_to_locals (data_ref, data_dir)
}
// get styles
if (loader.import.styles) {
let styles_dir = loader.import.styles
let styles_ref = fs.readdirSync (styles_dir)
file_to_locals (styles_ref, styles_dir)
file_to_json (styles_ref, styles_dir)
}
// // get PAGES loader info, bypass if not there
if (loader.import.pages) {
let pages_dir = loader.import.pages
let pages_ref = fs.readdirSync (pages_dir)
pages_to_locals (pages_ref, pages_dir)
}
// trip load boolean so we don't run the loader for each source file,
// but only on initial compilation load
locals.load = true;
}
return source;
}