-
Notifications
You must be signed in to change notification settings - Fork 46
/
gulpfile.js
196 lines (175 loc) · 5.99 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
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
var del = require('del');
var gulp = require('gulp');
var typescript = require('gulp-typescript');
var watch = require('gulp-watch');
var exec = require('child_process').exec;
var karma = require('karma').Server;
var path = require('path');
var runSequence = require('run-sequence');
var through2 = require('through2');
var APP_NAME = 'ngRnSeed';
var PATHS = {
sources: {
src: 'src/**/*.ts',
test: 'test/**/*.ts',
assets: 'src/assets/**/*'
},
destination: 'dist',
karma: 'dist/karma',
app: 'dist/' + APP_NAME,
modules: [
'node_modules/@angular/**/*',
'node_modules/angular-react-native/**/*',
'node_modules/hammerjs/**/*',
'node_modules/reflect-metadata/**/*',
'node_modules/rxjs/**/*',
'node_modules/zone.js/**/*'
]
};
/**********************************************************************************/
/***************************** APPLICATION ***********************************/
/**********************************************************************************/
gulp.task('clean', function (done) {
del([PATHS.destination], done);
});
gulp.task('!create', ['clean'], function(done) {
executeInAppDir('react-native init ' + APP_NAME, done, true);
});
gulp.task('!postcreate', ['!create'], function() {
return gulp.src(PATHS.app + '/node_modules/react-native/Libraries/Renderer/ReactNativeStack*')
.pipe(transformReactNativeStack())
.pipe(gulp.dest(PATHS.app + '/node_modules/react-native/Libraries/Renderer/'));
});
gulp.task('init', ['!postcreate'], function() {
return gulp.src(PATHS.modules, { base: './node_modules/' })
.pipe(gulp.dest(PATHS.app + '/node_modules'));
});
gulp.task('!assets', function () {
return gulp.src(PATHS.sources.assets, {base: './src'}).pipe(gulp.dest(PATHS.app));
});
gulp.task('transpile', ['!assets'], function () {
return ts2js([PATHS.sources.src], PATHS.app);
});
gulp.task('!launch.android', ['transpile'], function(done) {
executeInAppDir('react-native run-android', done);
});
gulp.task('!launch.ios', ['transpile'], function(done) {
executeInAppDir('react-native run-ios', done);
});
gulp.task('watch', function(neverDone) {
watch([PATHS.sources.src], function() {
runSequence('transpile');
});
});
gulp.task('start.android', ['!launch.android', 'watch'], function (neverDone) {
});
gulp.task('start.ios', ['!launch.ios', 'watch'], function (neverDone) {
});
/**********************************************************************************/
/************************* UNIT TEST IN BROWSER *******************************/
/**********************************************************************************/
gulp.task('clean.test', function (done) {
del([PATHS.karma], done);
});
gulp.task('ts2system', ['clean.test'], function () {
return ts2js([PATHS.sources.src, PATHS.sources.test], PATHS.karma, true);
});
gulp.task('karma-launch', function() {
new karma({
configFile: path.join(__dirname, 'karma.conf.js')
}).start();
});
gulp.task('karma-run', function (done) {
runKarma('karma.conf.js', done);
});
gulp.task('test.browser', ['ts2system'], function (neverDone) {
runSequence(
'karma-launch',
function() {
watch([PATHS.sources.src, PATHS.sources.test], function() {
runSequence('ts2system', 'karma-run');
});
}
);
});
gulp.task('test.browser/ci', ['ts2system'], function(done) {
new karma({
configFile: path.join(__dirname, 'karma.conf.js'),
singleRun: true
}, done).start();
});
/**********************************************************************************/
/******************************* UTIL **************************************/
/**********************************************************************************/
function ts2js(path, dest, toSystem) {
var tsResult = gulp.src(path.concat(['typings/index.d.ts']), toSystem? {base: './'} : {})
.pipe(typescript({
noImplicitAny: true,
module: toSystem ? 'system' : 'commonjs',
target: 'ES5',
moduleResolution: 'node',
emitDecoratorMetadata: true,
experimentalDecorators: true
},
undefined,
customReporter()));
if (toSystem) {
return tsResult.js.pipe(replaceRequire()).pipe(gulp.dest(dest));
} else {
return tsResult.js.pipe(gulp.dest(dest));
}
}
function customReporter() {
return {
error: (error) => {
if (error.relativeFilename) {
console.error(error.message);
}
},
finish: typescript.reporter.defaultFinishHandler
};
}
function executeInAppDir(command, done, inParentFolder) {
var cmd = 'mkdir -p dist';
exec(cmd, function(e, stdout) {
var dir = './dist';
if (!inParentFolder) dir += '/' + APP_NAME;
exec(command, {cwd: dir, maxBuffer: 5000 * 1024}, function(e, stdout) {
if (e) console.log(e);
if (done) done();
}).stdout.on('data', function(data) {
console.log(data);
});
});
}
function runKarma(configFile, done) {
var cmd = process.platform === 'win32' ? 'node_modules\\.bin\\karma run ' :
'node node_modules/.bin/karma run ';
cmd += configFile;
exec(cmd, function(e, stdout) {
// ignore errors, we don't want to fail the build in the interactive (non-ci) mode
// karma server will print all test failures
done();
});
}
function replaceRequire() {
return through2.obj(function (file, encoding, done) {
var content = String(file.contents).replace(/require\(/g, 'global.require(');
file.contents = new Buffer(content);
this.push(file);
done();
});
}
function transformReactNativeStack() {
return through2.obj(function (file, encoding, done) {
var content = String(file.contents);
content = content.replace('module.exports = ReactNativeStackEntry;',
`ReactNativeStackEntry.ReactNativeEventEmitter = ReactNativeEventEmitter_1;
ReactNativeStackEntry.ReactNativeTagHandles = ReactNativeTagHandles_1;
ReactNativeStackEntry.ReactNativeAttributePayload = ReactNativeAttributePayload_1;
module.exports = ReactNativeStackEntry;`);
file.contents = new Buffer(content);
this.push(file);
done();
});
}