This repository has been archived by the owner on May 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgulpfile.js
141 lines (124 loc) · 3.95 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
/**
* @license
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
'use strict';
const depcheck_lib = require('depcheck');
const fs = require('fs-extra');
const gulp = require('gulp');
const eslint_lib = require('gulp-eslint');
const mocha = require('gulp-mocha');
const newer = require('gulp-newer');
const shell = require('gulp-shell');
const sourcemaps = require('gulp-sourcemaps');
const tslint_lib = require('gulp-tslint');
const typescript = require('gulp-typescript');
const mergeStream = require('merge-stream');
const path = require('path');
const runSeq = require('run-sequence');
const typescript_lib = require('typescript');
const child_process = require('child_process');
function task(name, deps, impl) {
if (gulp.hasTask(name)) {
throw new Error(
`A task with the name ${JSON.stringify(name)} already exists!`);
}
gulp.task(name, deps, impl);
}
task('init');
task('depcheck', function () {
return new Promise((resolve, reject) => {
depcheck_lib(
__dirname,
{
ignoreDirs: [], ignoreMatches: ['@types/*']
},
resolve);
})
.then((result) => {
const invalidFiles = Object.keys(result.invalidFiles) || [];
const invalidJsFiles = invalidFiles.filter((f) => f.endsWith('.js'));
const unused = new Set(result.dependencies);
if (unused.size > 0) {
console.log('Unused dependencies:', unused);
throw new Error('Unused dependencies');
}
});
});
task('lint', ['tslint', 'depcheck']);
task('tslint', function () {
return gulp.src('src/**/*.ts')
.pipe(tslint_lib({
configuration: 'tslint.json',
formatter: 'verbose',
}))
.pipe(tslint_lib.report());
});
const tsProject =
typescript.createProject('tsconfig.json', { typescript: typescript_lib });
task('build', ['compile', 'json-schema']);
task('compile', function () {
const srcs =
gulp.src('src/**/*.ts'); //.pipe(newer({dest: 'lib', ext: '.js'}));
const tsResult =
srcs.pipe(sourcemaps.init())
.pipe(typescript(tsProject, [], typescript.reporter.fullReporter()));
// Use this once typescript-gulp supports `include` in tsconfig:
// const srcs = tsProject.src();
return mergeStream(
tsResult.js.pipe(sourcemaps.write('../lib')),
tsResult.dts)
.pipe(gulp.dest('lib'));
});
task('clean', () => {
for (const buildArtifact of ['lib']) {
fs.removeSync(path.join(__dirname, buildArtifact));
}
});
task('build-all', (done) => {
return runSeq('clean', 'init', 'lint', 'build', done);
});
task('test', ['build'], () => {
return gulp.src('test/**/*_test.js').pipe(mocha({
ui: 'tdd',
reporter: 'spec',
}));
});
task('json-schema', function () {
const inPath = 'src/analysis-format/analysis-format.ts';
const outPath = 'lib/analysis.schema.json';
const command = path.normalize(`node_modules/.bin/typescript-json-schema`);
const child = child_process.spawn(
command,
[`--required`, `--ignoreErrors`, `${inPath}`, `Analysis`],
{ shell: true, cwd: process.cwd(), hideWindows: true });
let buffer = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
buffer += chunk;
});
child.stderr.on('data', (chunk) => {
process.stderr.write(chunk);
});
const childFinished = new Promise((resolve, reject) => {
child.on('exit', (code) => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
return childFinished.then(() => {
fs.writeFileSync(outPath, buffer, 'utf8');
});
});