-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.js
183 lines (115 loc) · 5.06 KB
/
build.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
// we'll need filesystem functionality
var fs = require('fs');
// template directory path, for DRY sake
var dir_tpl = "_dev/";
// set up a list of the files we'll create.
var files = JSON.parse( fs.readFileSync( dir_tpl+"books.json", "utf8" ) );
var file_title = [];
files.forEach(function( file_info ){
file_title[file_info.filename] = file_info.title;
});
// open the book template files
var tpl_stylus = fs.readFileSync( dir_tpl+"book.styl", "utf8" );
var tpl_scss = fs.readFileSync( dir_tpl+"book.scss", "utf8" );
// open the index template files
var tpl_index = fs.readFileSync( dir_tpl+"index.html", "utf8" );
// arrays for each index file
var files_stylus = [],
files_scss = [],
files_less = [],
files_index = [];
// directory location
var dir = 'json/';
// loop through the directory
fs.readdir( dir, function( err, files ){
// throw an error if applicable
if ( err ) throw err;
var total = 0;
// loop through the files in the directory
files.forEach(function( file ){
//
var filename = file.replace( '.json', '' );
// open the file
var records = JSON.parse( fs.readFileSync( "json/"+file, "utf8" ) );
// some empty arrays for our colors
var records_csv = [];
var records_less = [];
var records_scss = [];
var records_html = [];
var records_index = [];
// start logging
console.log( "" );
console.log( "-------------------------------------------------------------------" );
console.log( " Book: " + file );
console.log( "-------------------------------------------------------------------" );
// variable name in Sass and Stylus
var var_name = filename+'-colors';
// loop through all the colors in this file.
records.forEach(function( color ){
// push another line of html
records_html.push( '<div class="swatch" style="background-color: '+color.hex+';" rel="'+color.label+'" data-hex="'+color.hex+'"><span><span>'+color.name+'</span>'+color.label+'</span></div>' );
// push another scss array value
records_scss.push( '("'+color.label+'" '+color.hex+')' );
// push another less array value
records_less.push( '@'+filename+'-'+color.label+': '+color.hex+';' );
// push another xml record
records_csv.push( '"'+color.name+'","'+color.label+'","'+color.hex+'"' );
});
// stylus files
files_less.push( "\n// " + file_title[filename] + '\n@import "book-'+filename+'.less";' );
// stylus files
files_scss.push( "\n// " + file_title[filename] + '\n@import "book-'+filename+'";' );
// stylus files
files_stylus.push( "\n// " + file_title[filename] + '\n@import "book-'+filename+'.styl"' );
// stylus files
files_index.push( '<option value="' + filename + '">' + file_title[filename] + ' <span>(' + records.length + ' colors)</span></option>' );
// dump a color count for each book
console.log( " > Colors in File: " + records_csv.length );
total = total + records_csv.length;
// write out the csv file
fs.writeFileSync( "csv/"+filename+".csv", records_csv.join( "\n" ) );
console.log( " > csv/"+filename+".csv created..." );
// write out the index.html file
fs.writeFileSync( 'book/'+filename+'.html', records_html.join("\n") )
console.log(' > book/'+filename+'.html created...');
// write out the Sass book file
fs.writeFileSync( 'scss/_book-'+filename+'.scss', tpl_scss
.replace( "{{colors}}", records_scss.join(", ") )
.replace( /\{\{var_name\}\}/g, var_name )
.replace( "{{fn_name}}", filename ),
console.log(' > scss/_book-' + filename + '.scss created...') );
// write out the Stylus book file
fs.writeFileSync( 'stylus/book-'+filename+'.styl', tpl_stylus
.replace( "{{colors}}", records_scss.join(" ") )
.replace( /\{\{var_name\}\}/g, var_name )
.replace( "{{fn_name}}", filename ) );
console.log(' > stylus/book-'+filename+'.styl created...');
// write out the LESS book file
fs.writeFileSync( 'less/book-'+filename+'.less', "// "+filename+"\n"+records_less.join("\n") );
console.log(' > less/book-'+filename+'.less created...');
});
// start outputting main file generation progress
console.log( "" );
console.log( "-------------------------------------------------------------------" );
console.log( " Main Files" );
console.log( "-------------------------------------------------------------------" );
// dump the total colors
console.log( " > Total Colors: " + total );
// write out the Sass book file
fs.writeFileSync( 'scss/_colornerd.scss', files_scss.join("\n") );
console.log(' > scss/_colornerd.scss created...');
// write out the Stylus book file
fs.writeFileSync( 'stylus/colornerd.styl', files_stylus.join("\n") );
console.log(' > stylus/colornerd.styl created...');
// write out the LESS book file
fs.writeFileSync( 'less/colornerd.less', files_less.join("\n") );
console.log(' > less/colornerd.less created...');
// write out the LESS book file
fs.writeFileSync( 'index.html', tpl_index
.replace( "{{colors}}", files_index.join("\n") )
.replace( "{{count}}", total )
.replace( "{{count_files}}", files.length )
);
console.log(' > index.html created...');
console.log( "" );
});