-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.ts
183 lines (157 loc) · 4.25 KB
/
gulpfile.ts
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
/**
* rocket-nuimo gulp.js task configuration
*
* Tasks:
* - clean
* - lint
* - build
* - preparePublish
* - publish
*/
// tslint:disable:completed-docs
import * as del from 'del'
import * as typescript from 'typescript'
import * as merge from 'merge2'
import * as jsonEditor from 'gulp-json-editor'
import gulpTsLint from 'gulp-tslint'
import { Linter } from 'tslint'
import { Project } from 'gulp-typescript'
import { createProject as createTsProject } from 'gulp-typescript'
import { dest, parallel, series, src } from 'gulp'
// Build destination paths
const ARTIFACTS_DEST = 'publish' // Build artificats
const COVERAGE_DEST = 'coverage' // Coverage reports
const DIST_DEST = 'dist' // Javascript sources
const TYPEDEF_DEST = 'dts' // Defintion files
// Package projects
const PROJECT_TSCONFIG = './tsconfig.json'
const EXAMPLES_TSCONFIG = './examples/tsconfig.json'
// Package files
const PACKAGE_FILES_GLOB = [
'LICENSE',
'package.json',
'README.md',
]
/**
* Purges all generated sources and artifacts
*/
export async function clean() {
return del([
ARTIFACTS_DEST,
COVERAGE_DEST,
DIST_DEST,
TYPEDEF_DEST,
])
}
/**
* Performs a lint on package & example sources
*/
export async function lint(done: (error?: any) => void) {
return parallel(
lintSources.bind(undefined, tsProject()),
lintSources.bind(undefined, tsExamplesProject()),
)(done)
}
/**
* Transpiles package sources
*/
export async function build() {
return transpileSources(tsProject(), './')
}
/**
* Packages up the package and assembles an set of artifacts for publishing
*/
export async function preparePublish(done: (error?: any) => void) {
const project = tsProject()
const artifactsPath = ARTIFACTS_DEST
const cleanPublish = () => del(ARTIFACTS_DEST)
return series(
cleanPublish,
lintSources.bind(undefined, project),
transpileSources.bind(undefined, project, artifactsPath),
parallel(
copyPackageFiles.bind(undefined, artifactsPath),
copyTypeScriptProjectSources.bind(undefined, project, `${artifactsPath}/src`),
),
updatePackageJson.bind(undefined, artifactsPath),
)(done)
}
/**
*
*/
export function publish(done: (error?: any) => void) {
}
//
// Private functions
//
/**
* Package default project
*/
function tsProject(): Project {
return createTsProject(PROJECT_TSCONFIG, {
typescript,
declaration: true,
})
}
/**
* Package examples project
*/
function tsExamplesProject(): Project {
return createTsProject(EXAMPLES_TSCONFIG, {
typescript,
})
}
/**
* Lints a given project
*/
function lintSources(project: Project) {
const program = Linter.createProgram(project.configFileName)
program.getSourceFiles()
return project.src()
.pipe(gulpTsLint({
fix: false,
formatter: 'codeFrame',
program,
}))
.pipe(gulpTsLint.report({
summarizeFailureOutput: true,
allowWarnings: true,
}));
}
/**
* Transpiles TypeScript sources
*/
function transpileSources(project: Project, artifactsPath: string) {
const compilationResult = project.src().pipe(project())
return merge([
compilationResult.js.pipe(dest(`${artifactsPath}/${DIST_DEST}`)),
compilationResult.dts.pipe(dest(`${artifactsPath}/${TYPEDEF_DEST}`)),
])
}
/**
* Copies package TypeScript sources
*/
function copyTypeScriptProjectSources(project: Project, destPath: string) {
return project.src().pipe(dest(destPath))
}
/**
* Copies all other package files based on PACKAGE_FILES_GLOB
*/
function copyPackageFiles(artifactsPath: string) {
return src(PACKAGE_FILES_GLOB).pipe(dest(artifactsPath))
}
/**
* Updates the package.json configuration
*/
function updatePackageJson(artifactsPath: string) {
return src(`${artifactsPath}/package.json`)
.pipe(jsonEditor((json: Record<string, any>) => {
json.main = `${DIST_DEST}/index.js`
json.types = `${TYPEDEF_DEST}/index.d.ts`
json.devDependencies = undefined
json.private = undefined
json.scripts = undefined
return json
}))
.pipe(dest(artifactsPath))
}