forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
160 lines (128 loc) · 4.46 KB
/
gulpfile.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
/*
gulpfile.js - gulp script for building Faker package for browser / stand-alone package
run this file using the gulp command
If this is your first time trying to build faker, you will need to install gulp:
cd faker.js/
[sudo] npm install
[sudo] npm install gulp -g
cd build/
gulp
*/
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var mustache = require('gulp-mustache');
var browserify = require('browserify');
var transform = require('vinyl-transform');
var path = require('path');
var fs = require('fs');
var through = require('through2')
gulp.task('browser-package', function() {
/* task for building browser bundles using browserify
this task will generate the following files:
./build/faker.js
./build/faker.min.js
../examples/browser/js/faker.js
../examples/browser/js/faker.min.js
*/
var browserified = transform(function(filename) {
// use browserify to create UMD stand-alone browser package
var b = browserify(filename, {
standalone: 'faker'
});
return b.bundle();
});
return gulp.src('../index.js')
.pipe(browserified)
.pipe(rename('faker.js'))
.pipe(gulp.dest('build/'))
.pipe(gulp.dest('../examples/browser/js'))
.pipe(rename({ extname: ".min.js" }))
.pipe(uglify())
.pipe(gulp.dest('build/'))
.pipe(gulp.dest('../examples/browser/js'))
.pipe(rename('../examples/browser/js/faker.min.js'));
});
// builds Readme.md file from docs.md and exported faker methods
gulp.task('documentation', function(cb) {
/* task for generating documentation
this task will generate the following file:
../Readme.md
*/
var API = '', LOCALES = '';
var faker = require('../index');
// generate locale list
for (var locale in faker.locales) {
LOCALES += ' * ' + locale + '\n';
}
var keys = Object.keys(faker);
keys = keys.sort();
// generate nice tree of api for docs
keys.forEach(function(module){
// ignore certain properties
var ignore = ['locale', 'localeFallback', 'definitions', 'locales'];
if (ignore.indexOf(module) !== -1) {
return;
}
API += '* ' + module + '\n';
for (var method in faker[module]) {
API += ' * ' + method + '\n';
}
});
return gulp.src('./src/docs.md')
.pipe(mustache({
'API': API,
'LOCALES': LOCALES,
'startYear': 2010,
'currentYear': new Date().getFullYear()
}))
.pipe(rename("./Readme.md"))
.pipe(gulp.dest('../'))
});
var tasks = ['nodeLocalRequires', 'browser-package', 'documentation'];
// var tasks = [];
var locales = require('../lib/locales');
var localTasks = Object.keys(locales);
/* task for generating unique browser builds for every locale */
Object.keys(locales).forEach(function(locale, i) {
if (i > 0) {
// return;
}
tasks.push(locale + 'Task');
gulp.task(locale + 'Task', function() {
var browserified = transform(function(filename) {
// use browserify to create UMD stand-alone browser package
var b = browserify(filename, {
standalone: 'faker'
});
return b.bundle();
});
process.chdir('../locale/');
return gulp.src('./' + locale + '.js')
.pipe(browserified)
.pipe(rename('faker.' + locale + '.js'))
.pipe(gulp.dest('../build/build/locales/' + locale))
.pipe(gulp.dest('../examples/browser/locales/' + locale + "/"))
.pipe(rename({ extname: ".min.js" }))
.pipe(uglify())
.pipe(gulp.dest('../build/build/locales/' + locale))
.pipe(gulp.dest('../examples/browser/locales/' + locale + '/'))
.pipe(rename('../examples/browser/locales/' + locale + '/' + 'faker.' + locale + 'min.js'));
});
});
gulp.task('nodeLocalRequires', function (cb){
var locales = require('../lib/locales');
for (var locale in locales) {
var localeFile = path.normalize(__dirname + "/../locale/" + locale + ".js");
var localeRequire = '';
localeRequire += "var Faker = require('../lib');\n";
localeRequire += "var faker = new Faker({ locale: '" + locale + "', localeFallback: 'en' });\n";
// TODO: better fallback support
localeRequire += "faker.locales['" + locale + "'] = require('../lib/locales/" + locale + "');\n";
localeRequire += "faker.locales['" + 'en' + "'] = require('../lib/locales/" + 'en' + "');\n";
localeRequire += "module['exports'] = faker;\n";
fs.writeFileSync(localeFile, localeRequire);
}
cb();
});
gulp.task('default', tasks);