Skip to content

Commit 4ba2502

Browse files
committed
More cleanup
1 parent f0b3825 commit 4ba2502

File tree

9 files changed

+129
-141
lines changed

9 files changed

+129
-141
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ before_script:
4848
- mkdir -p $LOGS_DIR
4949

5050
script:
51-
- ./scripts/ci/build-and-test.sh
51+
- ./scripts/ci/build-and-test.sh
5252

5353
cache:
5454
directories:

tools/gulp/task_helpers.ts

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import * as gulp from 'gulp';
44
import * as gulpTs from 'gulp-typescript';
55
import * as path from 'path';
66

7+
import {NPM_VENDOR_FILES, PROJECT_ROOT, DIST_ROOT} from './constants';
8+
79

810
/** Those imports lack typings. */
11+
const gulpClean = require('gulp-clean');
912
const gulpMerge = require('merge2');
1013
const gulpRunSequence = require('run-sequence');
1114
const gulpSass = require('gulp-sass');
15+
const gulpServer = require('gulp-server-livereload');
1216
const gulpSourcemaps = require('gulp-sourcemaps');
1317
const resolveBin = require('resolve-bin');
1418

@@ -104,11 +108,11 @@ export function execNodeTask(packageName: string, executable: string[] | string,
104108
executable = undefined;
105109
}
106110

107-
return (done: () => void) => {
108-
resolveBin(packageName, { executable: executable }, function (err: any, binPath: string) {
111+
return (done: (err: any) => void) => {
112+
resolveBin(packageName, { executable: executable }, (err: any, binPath: string) => {
109113
if (err) {
110114
console.error(err);
111-
throw err;
115+
return done(err);
112116
}
113117

114118
// Forward to execTask.
@@ -121,11 +125,17 @@ export function execNodeTask(packageName: string, executable: string[] | string,
121125
/** Copy files from a glob to a destination. */
122126
export function copyTask(srcGlobOrDir: string, outRoot: string) {
123127
return () => {
124-
return gulp.src(path.join(_globify(srcGlobOrDir))).pipe(gulp.dest(outRoot));
128+
return gulp.src(_globify(srcGlobOrDir)).pipe(gulp.dest(outRoot));
125129
}
126130
}
127131

128132

133+
/** Delete files. */
134+
export function cleanTask(glob: string) {
135+
return () => gulp.src(glob, { read: false }).pipe(gulpClean(null));
136+
}
137+
138+
129139
/** Build an task that depends on all application build tasks. */
130140
export function buildAppTask(appName: string) {
131141
const buildTasks = ['vendor', 'ts', 'scss', 'assets']
@@ -139,3 +149,42 @@ export function buildAppTask(appName: string) {
139149
);
140150
};
141151
}
152+
153+
154+
/** Create a task that copies vendor files in the proper destination. */
155+
export function vendorTask() {
156+
return () => gulpMerge(
157+
NPM_VENDOR_FILES.map(root => {
158+
const glob = path.join(PROJECT_ROOT, 'node_modules', root, '**/*.+(js|js.map)');
159+
return gulp.src(glob).pipe(gulp.dest(path.join(DIST_ROOT, 'vendor', root)));
160+
}));
161+
}
162+
163+
164+
/** Create a task that serves the dist folder. */
165+
export function serverTask(liveReload: boolean = true,
166+
streamCallback: (stream: NodeJS.ReadWriteStream) => void = null) {
167+
return () => {
168+
const stream = gulp.src('dist').pipe(gulpServer({
169+
livereload: liveReload,
170+
fallback: 'index.html',
171+
port: 4200
172+
}));
173+
174+
if (streamCallback) {
175+
streamCallback(stream);
176+
}
177+
return stream;
178+
}
179+
}
180+
181+
182+
/** Create a task that's a sequence of other tasks. */
183+
export function sequenceTask(...args: any[]) {
184+
return (done: any) => {
185+
gulpRunSequence(
186+
...args,
187+
done
188+
);
189+
}
190+
}

tools/gulp/tasks/clean.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
import {src, task} from 'gulp';
2-
const gulpClean = require('gulp-clean');
1+
import {task} from 'gulp';
2+
import {cleanTask} from '../task_helpers';
33

44

5-
task('clean', function() {
6-
return src('dist', { read: false })
7-
.pipe(gulpClean());
8-
});
9-
task(':clean:spec', function() {
10-
return src('dist/**/*.spec.*', { read: false })
11-
.pipe(gulpClean());
12-
});
13-
task(':clean:assets', function() {
14-
return src('dist/**/*+(.html|.css)', { read: false })
15-
.pipe(gulpClean());
16-
});
5+
task('clean', cleanTask('dist'));
6+
task(':clean:spec', cleanTask('dist/**/*.spec.*'));
7+
task(':clean:assets', cleanTask('dist/**/*+(.html|.css)'));

tools/gulp/tasks/components.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as gulp from 'gulp';
1+
import {task, watch} from 'gulp';
22
import * as path from 'path';
3-
import gulpMerge = require('merge2');
43

54
import {SOURCE_ROOT, DIST_COMPONENTS_ROOT, PROJECT_ROOT} from '../constants';
65
import {sassBuildTask, tsBuildTask, execNodeTask, copyTask} from '../task_helpers';
@@ -11,30 +10,26 @@ const inlineResources = require('../../../scripts/release/inline-resources');
1110
const componentsDir = path.join(SOURCE_ROOT, 'lib');
1211

1312

14-
export function watchComponents() {
15-
gulp.watch(path.join(componentsDir, '**/*.ts'), [':build:components:ts']);
16-
gulp.watch(path.join(componentsDir, '**/*.scss'), [':build:components:scss']);
17-
gulp.watch(path.join(componentsDir, '**/*.html'), [':build:components:assets']);
18-
}
19-
20-
21-
gulp.task(':build:components:ts', tsBuildTask(componentsDir));
13+
task(':watch:components', () => {
14+
watch(path.join(componentsDir, '**/*.ts'), [':build:components:ts']);
15+
watch(path.join(componentsDir, '**/*.scss'), [':build:components:scss']);
16+
watch(path.join(componentsDir, '**/*.html'), [':build:components:assets']);
17+
});
2218

23-
gulp.task(':build:components:assets',
24-
copyTask(path.join(componentsDir, '*/**/*.!(ts|spec.ts)'), DIST_COMPONENTS_ROOT));
2519

26-
gulp.task(':build:components:scss', sassBuildTask(
20+
task(':build:components:ts', tsBuildTask(componentsDir));
21+
task(':build:components:assets',
22+
copyTask(path.join(componentsDir, '*/**/*.!(ts|spec.ts)'), DIST_COMPONENTS_ROOT));
23+
task(':build:components:scss', sassBuildTask(
2724
DIST_COMPONENTS_ROOT, componentsDir, [path.join(componentsDir, 'core/style')]
2825
));
2926

30-
gulp.task('build:components', [
27+
task('build:components', [
3128
':build:components:ts',
3229
':build:components:assets',
3330
':build:components:scss'
34-
], function() {
35-
inlineResources([DIST_COMPONENTS_ROOT]);
36-
});
31+
], () => inlineResources([DIST_COMPONENTS_ROOT]));
3732

38-
gulp.task(':build:components:ngc', ['build:components'], execNodeTask(
33+
task(':build:components:ngc', ['build:components'], execNodeTask(
3934
'@angular/compiler-cli', 'ngc', ['-p', path.relative(PROJECT_ROOT, componentsDir)]
4035
));

tools/gulp/tasks/development.ts

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,31 @@
1-
import * as gulp from 'gulp';
1+
import {task, watch} from 'gulp';
22
import * as path from 'path';
33

4-
import gulpMerge = require('merge2');
5-
const gulpServer = require('gulp-server-livereload');
6-
7-
import {DIST_ROOT, NPM_VENDOR_FILES, SOURCE_ROOT} from '../constants';
8-
import {sassBuildTask, tsBuildTask, copyTask, buildAppTask} from '../task_helpers';
9-
import {watchComponents} from './components';
4+
import {DIST_ROOT, SOURCE_ROOT} from '../constants';
5+
import {
6+
sassBuildTask, tsBuildTask, copyTask, buildAppTask, vendorTask,
7+
serverTask, sequenceTask
8+
} from '../task_helpers';
109

1110

1211
const appDir = path.join(SOURCE_ROOT, 'demo-app');
1312
const outDir = DIST_ROOT;
1413

1514

16-
export function watchDevelopmentApp() {
17-
gulp.watch(path.join(appDir, '**/*.ts'), [':build:devapp:ts']);
18-
gulp.watch(path.join(appDir, '**/*.scss'), [':build:devapp:scss']);
19-
gulp.watch(path.join(appDir, '**/*.html'), [':build:devapp:assets']);
20-
}
21-
22-
23-
gulp.task(':build:devapp:vendor', function() {
24-
return gulpMerge(
25-
NPM_VENDOR_FILES.map(function(root) {
26-
const glob = path.join(root, '**/*.+(js|js.map)');
27-
return gulp.src(path.join('node_modules', glob))
28-
.pipe(gulp.dest(path.join('dist/vendor', root)));
29-
}));
15+
task(':watch:devapp', () => {
16+
watch(path.join(appDir, '**/*.ts'), [':build:devapp:ts']);
17+
watch(path.join(appDir, '**/*.scss'), [':build:devapp:scss']);
18+
watch(path.join(appDir, '**/*.html'), [':build:devapp:assets']);
3019
});
3120

3221

33-
gulp.task(':build:devapp:ts', [':build:components:ts'], tsBuildTask(appDir));
34-
gulp.task(':build:devapp:scss', [':build:components:scss'], sassBuildTask(outDir, appDir, []));
35-
gulp.task(':build:devapp:assets', copyTask(appDir, outDir));
36-
37-
gulp.task('build:devapp', buildAppTask('devapp'));
38-
39-
gulp.task('serve:devapp', ['build:devapp'], function() {
40-
const stream = gulp.src('dist')
41-
.pipe(gulpServer({
42-
livereload: true,
43-
fallback: 'index.html',
44-
port: 4200
45-
}));
46-
47-
watchComponents();
48-
watchDevelopmentApp();
49-
return stream;
50-
});
22+
task(':build:devapp:vendor', vendorTask());
23+
task(':build:devapp:ts', [':build:components:ts'], tsBuildTask(appDir));
24+
task(':build:devapp:scss', [':build:components:scss'], sassBuildTask(outDir, appDir, []));
25+
task(':build:devapp:assets', copyTask(appDir, outDir));
26+
task('build:devapp', buildAppTask('devapp'));
5127

28+
task(':serve:devapp', serverTask());
29+
task('serve:devapp', ['build:devapp'], sequenceTask(
30+
[':serve:devapp', ':watch:components', ':watch:devapp']
31+
));

tools/gulp/tasks/e2e.ts

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,50 @@
1-
import * as gulp from 'gulp';
1+
import {task, watch} from 'gulp';
22
import * as path from 'path';
33
import gulpMerge = require('merge2');
44
import gulpRunSequence = require('run-sequence');
55

66
import {SOURCE_ROOT, DIST_ROOT, PROJECT_ROOT, NPM_VENDOR_FILES} from '../constants';
7-
import {tsBuildTask, sassBuildTask, copyTask, buildAppTask, execNodeTask} from '../task_helpers';
8-
import {watchComponents} from './components';
9-
10-
const gulpServer = require('gulp-server-livereload');
7+
import {
8+
tsBuildTask, sassBuildTask, copyTask, buildAppTask, execNodeTask,
9+
vendorTask, sequenceTask, serverTask
10+
} from '../task_helpers';
1111

1212

1313
const appDir = path.join(SOURCE_ROOT, 'e2e-app');
1414
const outDir = DIST_ROOT;
15+
const PROTRACTOR_CONFIG_PATH = path.join(PROJECT_ROOT, 'test/protractor.conf.js');
1516

1617

17-
export function watchE2eApp() {
18-
gulp.watch(path.join(appDir, '**/*.ts'), [':build:e2eapp:ts']);
19-
gulp.watch(path.join(appDir, '**/*.scss'), [':build:e2eapp:scss']);
20-
gulp.watch(path.join(appDir, '**/*.html'), [':build:e2eapp:assets']);
21-
}
22-
23-
gulp.task(':build:e2eapp:vendor', function() {
24-
return gulpMerge(
25-
NPM_VENDOR_FILES.map(function(root) {
26-
const glob = path.join(root, '**/*.+(js|js.map)');
27-
return gulp.src(path.join('node_modules', glob))
28-
.pipe(gulp.dest(path.join('dist/vendor', root)));
29-
}));
18+
task(':watch:e2eapp', () => {
19+
watch(path.join(appDir, '**/*.ts'), [':build:e2eapp:ts']);
20+
watch(path.join(appDir, '**/*.scss'), [':build:e2eapp:scss']);
21+
watch(path.join(appDir, '**/*.html'), [':build:e2eapp:assets']);
3022
});
3123

32-
gulp.task(':build:e2eapp:ts', [':build:components:ts'], tsBuildTask(appDir));
33-
gulp.task(':build:e2eapp:scss', [':build:components:scss'], sassBuildTask(outDir, appDir, []));
34-
gulp.task(':build:e2eapp:assets', copyTask(appDir, outDir));
3524

36-
gulp.task('build:e2eapp', buildAppTask('e2eapp'));
25+
task(':build:e2eapp:vendor', vendorTask());
26+
task(':build:e2eapp:ts', [':build:components:ts'], tsBuildTask(appDir));
27+
task(':build:e2eapp:scss', [':build:components:scss'], sassBuildTask(outDir, appDir, []));
28+
task(':build:e2eapp:assets', copyTask(appDir, outDir));
3729

30+
task('build:e2eapp', buildAppTask('e2eapp'));
3831

39-
let stopE2eServer: () => void = null;
40-
gulp.task('serve:e2eapp', ['build:e2eapp'], function() {
41-
const stream = gulp.src('dist')
42-
.pipe(gulpServer({
43-
livereload: false,
44-
fallback: 'index.html',
45-
port: 4200
46-
}));
47-
48-
watchComponents();
49-
watchE2eApp();
5032

51-
stopE2eServer = () => { stream.emit('kill'); };
52-
return stream;
53-
});
54-
55-
gulp.task(':serve:e2eapp:stop', function() {
56-
if (stopE2eServer) {
57-
stopE2eServer();
58-
}
59-
});
33+
task(':test:protractor:setup', execNodeTask('protractor', 'webdriver-manager', ['update']));
34+
task(':test:protractor', execNodeTask('protractor', [PROTRACTOR_CONFIG_PATH]));
35+
task(':e2e:done', () => process.exit(0));
6036

61-
gulp.task(':test:protractor:setup', execNodeTask('protractor', 'webdriver-manager', ['update']));
62-
63-
gulp.task(':test:protractor', execNodeTask(
64-
'protractor', [path.join(PROJECT_ROOT, 'test/protractor.conf.js')]
37+
let stopE2eServer: () => void = null;
38+
task(':serve:e2eapp', serverTask(false, (stream) => { stopE2eServer = () => stream.emit('kill') }));
39+
task(':serve:e2eapp:stop', () => stopE2eServer());
40+
task('serve:e2eapp', ['build:e2eapp'], sequenceTask([
41+
':serve:e2eapp',
42+
':watch:components',
43+
':watch:e2eapp'
44+
]));
45+
46+
47+
task('e2e', sequenceTask(
48+
':test:protractor:setup', 'serve:e2eapp', ':test:protractor', ':serve:e2eapp:stop',
49+
':e2e:done'
6550
));
66-
67-
gulp.task(':e2e:done', function() {
68-
process.exit(0);
69-
});
70-
71-
gulp.task('e2e', function(done: () => void) {
72-
gulpRunSequence(
73-
':test:protractor:setup',
74-
'serve:e2eapp',
75-
':test:protractor',
76-
':serve:e2eapp:stop',
77-
':e2e:done',
78-
done
79-
);
80-
});

tools/gulp/tasks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import './development';
66
import './e2e';
77
import './lint';
88
import './release';
9+
import './serve';
910
import './spec';

tools/gulp/tasks/serve.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {task} from 'gulp';
2+
import {serverTask} from '../task_helpers';
3+
4+
5+
task('serve', serverTask());

tools/gulp/tsconfig.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,5 @@
2121
"types": [
2222
"node"
2323
]
24-
},
25-
"files": [
26-
"gulpfile.ts"
27-
]
24+
}
2825
}

0 commit comments

Comments
 (0)