Skip to content

Commit 1324d24

Browse files
committed
Refactor tasks into separate files
1 parent dfb27d7 commit 1324d24

29 files changed

+451
-3433
lines changed

gulpfile.js

Lines changed: 3 additions & 375 deletions
Original file line numberDiff line numberDiff line change
@@ -1,381 +1,9 @@
11
'use strict';
2-
/**
3-
* This file needs to be JavaScript and is read by gulp.
4-
*/
5-
// Global imports.
6-
const child_process = require('child_process');
7-
const fs = require('fs');
8-
const gulp = require('gulp');
92
const path = require('path');
10-
const resolveBin = require('resolve-bin');
113

12-
// Other imports.
13-
const inlineResources = require('./scripts/release/inline-resources');
14-
const karma = require('karma');
154

16-
// Gulp plugins.
17-
const gulpClean = require('gulp-clean');
18-
const gulpServer = require('gulp-server-livereload');
19-
const gulpMerge = require('merge2');
20-
const gulpRunSequence = require('run-sequence');
21-
const gulpSass = require('gulp-sass');
22-
const gulpSourcemaps = require('gulp-sourcemaps');
23-
const gulpTs = require('gulp-typescript');
24-
25-
26-
// Directories.
27-
const srcDir = path.join(__dirname, 'src');
28-
const componentsDir = path.join(srcDir, 'lib');
29-
const devAppDir = path.join(srcDir, 'demo-app');
30-
const e2eAppDir = path.join(srcDir, 'e2e-app');
31-
32-
const outDir = 'dist';
33-
const outLibDir = path.join(outDir, '@angular2-material');
34-
35-
36-
/**
37-
* Create a TS Build Task, based on the options.
38-
*/
39-
function createTsBuildTask(options) {
40-
const tsConfigDir = options.tsConfigPath;
41-
const tsConfigPath = path.join(tsConfigDir, 'tsconfig.json');
42-
const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8'));
43-
const dest = path.join(tsConfigDir, tsConfig.compilerOptions.outDir);
44-
45-
return function() {
46-
const tsProject = gulpTs.createProject(tsConfigPath, {
47-
typescript: require('typescript')
48-
});
49-
50-
let pipe = tsProject.src()
51-
.pipe(gulpSourcemaps.init())
52-
.pipe(gulpTs(tsProject));
53-
let dts = pipe.dts.pipe(gulp.dest(dest));
54-
55-
return gulpMerge([
56-
dts,
57-
pipe
58-
.pipe(gulpSourcemaps.write('.'))
59-
.pipe(gulp.dest(dest))
60-
]);
61-
};
62-
}
63-
64-
/**
65-
* Create a SASS Build Task.
66-
*/
67-
function createSassBuildTask(options) {
68-
const dest = options.dest;
69-
const glob = path.join(options.root, '**/*.scss');
70-
const sassOptions = {
71-
includePaths: options.includePaths
72-
};
73-
74-
return function() {
75-
return gulp.src(glob)
76-
.pipe(gulpSourcemaps.init())
77-
.pipe(gulpSass(sassOptions).on('error', gulpSass.logError))
78-
.pipe(gulpSourcemaps.write(dest))
79-
.pipe(gulp.dest(dest));
80-
};
81-
}
82-
83-
84-
/**
85-
* Create a Gulp task that executes a process.
86-
*/
87-
function createExecTask(packageName, executable, args) {
88-
if (!args) {
89-
args = executable;
90-
executable = undefined;
91-
}
92-
return function(done) {
93-
resolveBin(packageName, { executable: executable }, function (err, binPath) {
94-
if (err) {
95-
console.error(err);
96-
throw err;
97-
}
98-
99-
const spawn = child_process.spawn(binPath, args);
100-
101-
spawn.stdout.on('data', (data) => {
102-
process.stdout.write(data);
103-
});
104-
105-
spawn.stderr.on('data', (data) => {
106-
process.stderr.write(data);
107-
});
108-
109-
spawn.on('close', (code) => {
110-
if (code != 0) {
111-
throw new Error('Process failed with code ' + code);
112-
}
113-
done();
114-
});
115-
});
116-
}
117-
}
118-
119-
/***************************************************************************************************
120-
* Components Build Tasks.
121-
*/
122-
gulp.task(':build:components:ts', createTsBuildTask({ tsConfigPath: componentsDir }));
123-
gulp.task(':build:components:assets', function() {
124-
return gulp.src(path.join(componentsDir, '*/**/*.!(ts|spec.ts)'))
125-
.pipe(gulp.dest(outLibDir));
126-
});
127-
gulp.task(':build:components:scss', function() {
128-
const cssTask = createSassBuildTask({
129-
dest: outLibDir,
130-
root: componentsDir,
131-
includePaths: path.join(componentsDir, 'core/style')
132-
});
133-
// Also copy over the SCSS for the components.
134-
return gulpMerge([
135-
cssTask(),
136-
gulp.src(path.join(componentsDir, '**/*.scss'))
137-
.pipe(gulp.dest(outLibDir))
138-
]);
139-
});
140-
gulp.task('build:components', [
141-
':build:components:ts',
142-
':build:components:assets',
143-
':build:components:scss'
144-
], function() {
145-
inlineResources([outLibDir]);
5+
require('ts-node').register({
6+
project: path.join(__dirname, 'tools/gulp')
1467
});
147-
gulp.task(':build:components:ngc', ['build:components'], createExecTask(
148-
'@angular/compiler-cli', 'ngc', ['-p', path.relative(__dirname, componentsDir)]
149-
));
1508

151-
152-
/***************************************************************************************************
153-
* Vendor files Build Tasks.
154-
*/
155-
gulp.task(':build:vendor', function() {
156-
const npmVendorFiles = [
157-
'@angular', 'core-js/client', 'hammerjs', 'rxjs', 'systemjs/dist', 'zone.js/dist'
158-
];
159-
160-
return gulpMerge(
161-
npmVendorFiles.map(function(root) {
162-
const glob = path.join(root, '**/*.+(js|js.map)');
163-
return gulp.src(path.join('node_modules', glob))
164-
.pipe(gulp.dest(path.join('dist/vendor', root)));
165-
}));
166-
});
167-
168-
169-
/***************************************************************************************************
170-
* DevApp Build Tasks.
171-
*/
172-
gulp.task(':build:devapp:ts', [':build:components:ts'], createTsBuildTask({ tsConfigPath: devAppDir }));
173-
gulp.task(':build:devapp:scss', [':build:components:scss'], createSassBuildTask({
174-
dest: outDir,
175-
root: devAppDir,
176-
// Change this once we have a better strategy for releasing SCSS files.
177-
includePaths: [
178-
path.join(componentsDir, 'core/style'),
179-
componentsDir
180-
]
181-
}));
182-
gulp.task(':build:devapp:assets', function() {
183-
return gulp.src(path.join(devAppDir, '**/*'))
184-
.pipe(gulp.dest(outDir));
185-
});
186-
187-
gulp.task('build:devapp', [
188-
'build:components',
189-
':build:vendor',
190-
':build:devapp:ts',
191-
':build:devapp:scss',
192-
':build:devapp:assets'
193-
]);
194-
195-
/***************************************************************************************************
196-
* DevApp Build Tasks.
197-
*/
198-
gulp.task(':build:e2eapp:ts', [':build:components:ts'], createTsBuildTask({ tsConfigPath: e2eAppDir }));
199-
gulp.task(':build:e2eapp:scss', [':build:components:scss'], createSassBuildTask({
200-
dest: outDir,
201-
root: e2eAppDir,
202-
// Change this once we have a better strategy for releasing SCSS files.
203-
includePaths: [
204-
path.join(componentsDir, 'core/style'),
205-
componentsDir
206-
]
207-
}));
208-
gulp.task(':build:e2eapp:assets', function() {
209-
return gulp.src(path.join(e2eAppDir, '**/*'))
210-
.pipe(gulp.dest(outDir));
211-
});
212-
213-
gulp.task('build:e2eapp', [
214-
'build:components',
215-
':build:vendor',
216-
':build:e2eapp:ts',
217-
':build:e2eapp:scss',
218-
':build:e2eapp:assets'
219-
]);
220-
221-
222-
/***************************************************************************************************
223-
* Global tasks.
224-
*/
225-
gulp.task('default', ['help']);
226-
227-
gulp.task('help', function() {
228-
const tasks = Object.keys(gulp.tasks)
229-
.filter(x => !x.startsWith(':'))
230-
.filter(x => !x.startsWith('ci:'))
231-
.filter(x => x != 'default')
232-
.sort();
233-
234-
console.log(`\nHere's a list of supported tasks:\n `, tasks.join('\n '));
235-
console.log(`\nYou're probably looking for "test" or "serve:devapp".\n\n`);
236-
});
237-
238-
gulp.task('build', ['build:devapp']);
239-
240-
gulp.task('clean', function() {
241-
return gulp.src('dist', { read: false })
242-
.pipe(gulpClean());
243-
});
244-
gulp.task(':clean:spec', function() {
245-
return gulp.src('dist/**/*.spec.*', { read: false })
246-
.pipe(gulpClean());
247-
});
248-
gulp.task(':clean:assets', function() {
249-
return gulp.src('dist/**/*+(.html|.css)', { read: false })
250-
.pipe(gulpClean());
251-
});
252-
253-
gulp.task('lint', createExecTask('tslint', ['-c', 'tslint.json', 'src/**/*.ts']));
254-
gulp.task('stylelint', createExecTask(
255-
'stylelint', ['src/**/*.scss', '--config', 'stylelint-config.json', '--syntax', 'scss']
256-
));
257-
258-
/***************************************************************************************************
259-
* Watch Tasks.
260-
*/
261-
gulp.task(':watch:components', ['build:components'], function() {
262-
return gulp.watch(path.join(componentsDir, '**/*'), ['build:components']);
263-
});
264-
265-
266-
/***************************************************************************************************
267-
* Serve Tasks.
268-
*/
269-
gulp.task('serve:devapp', ['build:devapp'], function() {
270-
const stream = gulp.src('dist')
271-
.pipe(gulpServer({
272-
livereload: true,
273-
fallback: 'index.html',
274-
port: 4200
275-
}));
276-
277-
gulp.watch(path.join(componentsDir, '**/*.ts'), [':build:components:ts']);
278-
gulp.watch(path.join(componentsDir, '**/*.scss'), [':build:components:scss']);
279-
gulp.watch(path.join(componentsDir, '**/*.html'), [':build:components:assets']);
280-
gulp.watch(path.join(devAppDir, '**/*.ts'), [':build:devapp:ts']);
281-
gulp.watch(path.join(devAppDir, '**/*.scss'), [':build:devapp:scss']);
282-
gulp.watch(path.join(devAppDir, '**/*.html'), [':build:devapp:assets']);
283-
284-
return stream;
285-
});
286-
287-
288-
let stopE2eServer = null;
289-
gulp.task('serve:e2eapp', ['build:e2eapp'], function(done) {
290-
const stream = gulp.src('dist')
291-
.pipe(gulpServer({
292-
livereload: false,
293-
fallback: 'index.html',
294-
port: 4200
295-
}));
296-
297-
gulp.watch(path.join(componentsDir, '**/*.ts'), [':build:components:ts']);
298-
gulp.watch(path.join(componentsDir, '**/*.scss'), [':build:components:scss']);
299-
gulp.watch(path.join(componentsDir, '**/*.html'), [':build:components:assets']);
300-
gulp.watch(path.join(e2eAppDir, '**/*.ts'), [':build:e2eapp:ts']);
301-
gulp.watch(path.join(e2eAppDir, '**/*.scss'), [':build:e2eapp:scss']);
302-
gulp.watch(path.join(e2eAppDir, '**/*.html'), [':build:e2eapp:assets']);
303-
304-
stopE2eServer = function() {
305-
stream.emit('kill');
306-
};
307-
308-
return stream;
309-
});
310-
311-
gulp.task(':serve:e2eapp:stop', function() {
312-
if (stopE2eServer) {
313-
stopE2eServer();
314-
}
315-
});
316-
317-
318-
/***************************************************************************************************
319-
* Tests.
320-
*/
321-
gulp.task('test', [':build:vendor', 'build:components'], function(done) {
322-
new karma.Server({
323-
configFile: path.join(__dirname, 'test/karma.conf.js')
324-
}, done).start();
325-
});
326-
327-
gulp.task('test:single-run', [':build:vendor', 'build:components'], function(done) {
328-
new karma.Server({
329-
configFile: path.join(__dirname, 'test/karma.conf.js'),
330-
singleRun: true
331-
}, done).start();
332-
});
333-
334-
gulp.task(':test:protractor:setup', createExecTask('protractor', 'webdriver-manager', ['update']));
335-
336-
gulp.task(':test:protractor', createExecTask(
337-
'protractor', [path.join(__dirname, 'test/protractor.conf.js')]
338-
));
339-
340-
gulp.task(':e2e:done', function() {
341-
process.exit(0);
342-
});
343-
344-
gulp.task('e2e', function(done) {
345-
gulpRunSequence(
346-
':test:protractor:setup',
347-
'serve:e2eapp',
348-
':test:protractor',
349-
':serve:e2eapp:stop',
350-
':e2e:done',
351-
done
352-
);
353-
});
354-
355-
356-
/***************************************************************************************************
357-
* Release builds.
358-
*/
359-
gulp.task('build:release', function(done) {
360-
// Synchronously run those tasks.
361-
gulpRunSequence(
362-
'clean',
363-
':build:components:ngc',
364-
[':clean:spec', ':clean:assets'],
365-
done
366-
);
367-
});
368-
369-
370-
/***************************************************************************************************
371-
* Continuous Integration.
372-
*/
373-
gulp.task('ci:lint', ['lint', 'stylelint']);
374-
gulp.task('ci:test', ['test:single-run'], function() {
375-
process.exit(0);
376-
});
377-
gulp.task('ci:e2e', ['e2e']);
378-
gulp.task('ci:extract-metadata', [':build:components:ngc']);
379-
gulp.task('ci:forbidden-identifiers', function() {
380-
require('./scripts/ci/forbidden-identifiers.js');
381-
});
9+
require('./tools/gulp/gulpfile');

0 commit comments

Comments
 (0)