Skip to content

Commit 300f7ab

Browse files
authored
Modify gulpfile to allow for autogenerated types per-service (#967)
* Add auto-generate mode for Gulpfile
1 parent 02f82df commit 300f7ab

File tree

3 files changed

+127
-40
lines changed

3 files changed

+127
-40
lines changed

gulpfile.js

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var ts = require('gulp-typescript');
3030
var del = require('del');
3131
var header = require('gulp-header');
3232
var replace = require('gulp-replace');
33+
var filter = require('gulp-filter');
3334

3435

3536
/****************/
@@ -50,12 +51,35 @@ var paths = {
5051
],
5152

5253
build: 'lib/',
54+
55+
curatedTypings: ['src/*.d.ts'],
5356
};
5457

55-
// Create a separate project for buildProject that overrides the rootDir
58+
const TEMPORARY_TYPING_EXCLUDES = [
59+
'!lib/default-namespace.d.ts',
60+
'!lib/firebase-namespace.d.ts',
61+
'!lib/firebase-app.d.ts',
62+
'!lib/firebase-service.d.ts',
63+
'!lib/auth/*.d.ts',
64+
'!lib/database/*.d.ts',
65+
'!lib/firestore/*.d.ts',
66+
'!lib/instance-id/*.d.ts',
67+
'!lib/machine-learning/*.d.ts',
68+
'!lib/messaging/*.d.ts',
69+
'!lib/project-management/*.d.ts',
70+
'!lib/remote-config/*.d.ts',
71+
'!lib/security-rules/*.d.ts',
72+
'!lib/storage/*.d.ts',
73+
'!lib/utils/*.d.ts'
74+
];
75+
76+
// Create a separate project for buildProject that overrides the rootDir.
5677
// This ensures that the generated production files are in their own root
57-
// rather than including both src and test in the lib dir.
58-
var buildProject = ts.createProject('tsconfig.json', {rootDir: 'src'});
78+
// rather than including both src and test in the lib dir. Declaration
79+
// is used by TypeScript to determine if auto-generated typings should be
80+
// emitted.
81+
const declaration = process.env.TYPE_GENERATION_MODE === 'auto';
82+
var buildProject = ts.createProject('tsconfig.json', { rootDir: 'src', declaration });
5983

6084
var buildTest = ts.createProject('tsconfig.json');
6185

@@ -71,16 +95,36 @@ gulp.task('cleanup', function() {
7195
]);
7296
});
7397

98+
// Task used to compile the TypeScript project. If automatic typings
99+
// are set to be generated (determined by TYPE_GENERATION_MODE), declarations
100+
// for files terminating in -internal.d.ts are removed because we do not
101+
// want to expose internally used types to developers. As auto-generated
102+
// typings are a work-in-progress, we remove the *.d.ts files for modules
103+
// which we do not intend to auto-generate typings for yet.
74104
gulp.task('compile', function() {
75-
return gulp.src(paths.src)
105+
let workflow = gulp.src(paths.src)
76106
// Compile Typescript into .js and .d.ts files
77107
.pipe(buildProject())
78108

79109
// Add header
80-
.pipe(header(banner))
81-
82-
// Write to build directory
83-
.pipe(gulp.dest(paths.build))
110+
.pipe(header(banner));
111+
112+
// Exclude typings that are unintended (currently excludes all auto-generated
113+
// typings, but as services are refactored to auto-generate typings this will
114+
// change). Moreover, all *-internal.d.ts typings should not be exposed to
115+
// developers as it denotes internally used types.
116+
if (declaration) {
117+
const configuration = [
118+
'lib/**/*.js',
119+
'lib/**/*.d.ts',
120+
'!lib/**/*-internal.d.ts',
121+
].concat(TEMPORARY_TYPING_EXCLUDES);
122+
123+
workflow = workflow.pipe(filter(configuration));
124+
}
125+
126+
// Write to build directory
127+
return workflow.pipe(gulp.dest(paths.build))
84128
});
85129

86130
/**
@@ -104,19 +148,23 @@ gulp.task('copyDatabase', function() {
104148
});
105149

106150
gulp.task('copyTypings', function() {
107-
return gulp.src('src/*.d.ts')
151+
let workflow = gulp.src('src/*.d.ts')
108152
// Add header
109-
.pipe(header(banner))
110-
111-
// Write to build directory
112-
.pipe(gulp.dest(paths.build))
153+
.pipe(header(banner));
154+
155+
if (declaration) {
156+
workflow = workflow.pipe(filter(paths.curatedTypings));
157+
}
158+
159+
// Write to build directory
160+
return workflow.pipe(gulp.dest(paths.build))
113161
});
114162

115163
gulp.task('compile_all', gulp.series('compile', 'copyDatabase', 'copyTypings', 'compile_test'));
116164

117165
// Regenerates js every time a source file changes
118166
gulp.task('watch', function() {
119-
gulp.watch(paths.src.concat(paths.test), {ignoreInitial: false}, gulp.series('compile_all'));
167+
gulp.watch(paths.src.concat(paths.test), { ignoreInitial: false }, gulp.series('compile_all'));
120168
});
121169

122170
// Build task

package-lock.json

Lines changed: 64 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@firebase/database": "^0.6.0",
5858
"@types/node": "^10.10.0",
5959
"dicer": "^0.3.0",
60+
"gulp-filter": "^6.0.0",
6061
"jsonwebtoken": "^8.5.1",
6162
"node-forge": "^0.9.1"
6263
},

0 commit comments

Comments
 (0)