This repository has been archived by the owner on Apr 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate-json.js
241 lines (190 loc) · 8.93 KB
/
generate-json.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***************************************************************************************************************************************************************
*
* Generate a json file from the UI-Kit repository and move into _data folder for Jekyll to ingest
*
**************************************************************************************************************************************************************/
'use strict';
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Dependencies
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const Fs = require(`fs`);
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const GEN = (() => { //constructor factory
return {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Settings
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
UIKIT: '_assets/vendor/dto-ui-kit',
UIKIT_SCSS: 'assets/sass',
UIKIT_templates: 'assets/sass/components/templates',
KIT: '_data/uikit.json',
TEMPLATES: '_includes/templates',
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Log to console.log
//
// @method success Log success info
// @param [text] {string} The sting you want to log
// @return [ansi] output
//
// @method error Log error info
// @param [text] {string} The sting you want to log
// @return [ansi] output
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
log: {
success: ( text ) => {
CFonts.say(`✅ ${text}/`, {
font: 'console',
space: false,
colors: ['green'],
});
},
error: ( text ) => {
CFonts.say(`❎ ${text}`, {
font: 'console',
space: false,
colors: ['red'],
});
},
},
}
})();
/***************************************************************************************************************************************************************
*
* MODULE
*
* Generate the json file
*
**************************************************************************************************************************************************************/
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Dependencies
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const KSS = require(`kss`);
GEN.json = (() => {
return {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Module init method
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
init: () => {
const uikitPath = `${GEN.UIKIT}/${GEN.UIKIT_SCSS}`;
//http://kss-node.github.io/kss-node/api/master/module-kss.html
KSS //use KSS to parse the scss files
.traverse( uikitPath ) //we use the root path
.then( kssData => { //promise return
const data = JSON.parse( JSON.stringify( kssData.data.sections ) ); //parsing the data into an object
let ID = ''; //we need to find the base ID
let templates = []; //and populate the template array
let kit = {}; //this is the object we will be building
//reading this package.json for it's version
const pkg = JSON.parse( Fs.readFileSync(`${GEN.UIKIT}/package.json`, `utf8`) );
kit.version = pkg.version;
data.map( section => { //iterate over the data
//this is a top level entry
if( section.depth === 1 ) {
// Adding complete block to kit.json
if( ID !== '' ) {
kit[ ID ] = {
templates: templates,
};
}
ID = section.referenceURI; //setting the ID for the next section
templates = []; //resetting the templates array
}
//this is a child entry
else {
if( section.markup !== '' ) { //we're only interested in the markup file paths
templates.push( section.markup ); //adding it into our templates array
}
}
});
//adding last iteration to object
kit[ ID ] = {
templates: templates,
};
//writing the object to file
Fs.writeFile( GEN.KIT, JSON.stringify( kit ), ( error ) => {
if( error ) {
throw error;
}
GEN.log.success(`successfully generated ${GEN.KIT} file`);
console.log(`\n\n`);
});
}
);
},
}
})();
/***************************************************************************************************************************************************************
*
* MODULE
*
* Copy template files into jekyll _includes folder
*
**************************************************************************************************************************************************************/
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Dependencies
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const Exec = require('child_process').exec;
GEN.copy = (() => {
return {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Module init method
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
init: () => {
//remove all html files in the folder so we don't produce garbage files
let script = `rm ${GEN.TEMPLATES}/*.html`;
Exec(script, ( error ) => {
if( error ) {
GEN.log.error(`exec error: ${error}`);
return;
}
GEN.log.success(`successfully removed all files from ${GEN.TEMPLATES}/`);
});
//create folder just in case this is the first time we run this
script = `mkdir ${GEN.TEMPLATES}`;
Exec(script, ( error ) => {
if( error ) {
GEN.log.error(`Folder already created. All good...`);
return;
}
GEN.log.success(`successfully created the folder: ${GEN.TEMPLATES}/`);
});
//copy files from submodule to includes folder of jekyll
script = `cp ${GEN.UIKIT}/${GEN.UIKIT_templates}/* ${GEN.TEMPLATES}`;
Exec(script, ( error ) => {
if( error ) {
GEN.log.error(`exec error: ${error}`);
return;
}
GEN.log.success(`successfully copied all files from ${GEN.UIKIT}/${GEN.UIKIT_templates}/ to ${GEN.TEMPLATES}`);
});
},
}
})();
/***************************************************************************************************************************************************************
*
* MODULE
*
* Initiate application
*
**************************************************************************************************************************************************************/
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Dependencies
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const CFonts = require(`cfonts`);
GEN.init = () => {
CFonts.say(`Doing work`, {
font: 'chrome',
space: false,
colors: ['red', 'magenta', 'blue'],
});
CFonts.say(`... so you don't have to`, {
font: 'console',
space: false,
});
console.log(`\n`);
GEN.json.init();
GEN.copy.init();
};
GEN.init();