-
Notifications
You must be signed in to change notification settings - Fork 392
/
gulpfile.js
184 lines (163 loc) · 5.59 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
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
184
/* eslint-disable import/no-commonjs, import/no-unassigned-import, max-nested-callbacks */
const fs = require('fs')
const path = require('path')
const {src, dest, watch, parallel, series} = require('gulp')
const del = require('del')
const changed = require('gulp-changed')
const ts = require('gulp-typescript')
const filter = require('gulp-filter')
const {flatten} = require('lodash')
const log = require('fancy-log')
const chalk = require('chalk')
const babel = require('gulp-babel')
const through = require('through2')
const {getPackagePaths} = require('./scripts/utils/getPackagePaths')
const {runSanityStart} = require('./scripts/utils/runSanityStart')
const SRC_DIR = 'src'
const DEST_DIR = 'lib'
// Regexes/names of packages that doesn't follow the src/lib convention
// or packages that does their own build (e.g. studios)
const IGNORED_PACKAGES = [
'packages/@sanity/generate-help-url',
'packages/@sanity/plugin-loader',
'packages/@sanity/eventsource',
'packages/@sanity/date-input',
'packages/@sanity/uuid',
'packages/eslint-config-sanity',
'packages/create-sanity',
'packages/storybook',
'packages/sanity',
/packages\/.*-studio/
]
const PACKAGE_PATHS = getPackagePaths().filter(pkgPath =>
IGNORED_PACKAGES.every(pattern =>
typeof pattern === 'string' ? pattern !== pkgPath : !pattern.test(pkgPath)
)
)
const withDisplayName = (name, fn) => {
fn.displayName = name
return fn
}
const TASK_INFO = {
babel: {title: 'Babel', color: chalk.yellowBright},
dts: {title: 'TypeScript (d.ts)', color: chalk.blueBright},
assets: {title: 'Assets (copy)', color: chalk.greenBright},
watch: {title: 'Watch', color: chalk.cyanBright},
_unknown: {title: 'Unknown', color: chalk.white}
}
const compileTaskName = (taskType, packagePath, extra = '') => {
const info = TASK_INFO[taskType] || TASK_INFO._unknown
return `${info.color(info.title)} → ${path.relative('packages', packagePath)}${
extra ? ` (${chalk.grey(extra)})` : ''
}`
}
function buildJavaScript(packageDir) {
return withDisplayName(compileTaskName('babel', packageDir), () =>
src(`${SRC_DIR}/**/*.{js,ts,tsx}`, {cwd: packageDir})
.pipe(
changed(DEST_DIR, {
cwd: packageDir,
transformPath: orgPath => orgPath.replace(/\.tsx?$/, '.js')
})
)
.pipe(babel())
.pipe(dest(DEST_DIR, {cwd: packageDir}))
)
}
function copyAssets(packageDir) {
return withDisplayName(compileTaskName('assets', packageDir), () =>
src(`${SRC_DIR}/**/*`, {cwd: packageDir})
.pipe(filter(['**/*.*', '!**/*.js', '!**/*.ts', '!**/*.tsx']))
.pipe(changed(DEST_DIR, {cwd: packageDir}))
.pipe(dest(DEST_DIR, {cwd: packageDir}))
)
}
function buildTypeScript(packageDir) {
const project = ts.createProject(path.join(packageDir, 'tsconfig.json'))
return withDisplayName(compileTaskName('dts', packageDir), () => {
const compilation = project.src().pipe(project())
return compilation.dts
.pipe(
changed(DEST_DIR, {
cwd: packageDir
})
)
.pipe(dest(project.options.outDir))
})
}
function buildPackage(packageDir) {
return parallel(buildJavaScript(packageDir), copyAssets(packageDir))
}
function watchPackage(name, packageDir, task) {
return withDisplayName(name, () => watch([`${SRC_DIR}/**/*`], {cwd: packageDir}, task))
}
const isTSProject = packageDir => {
const tsConfigPath = path.join(packageDir, 'tsconfig.json')
return fs.existsSync(tsConfigPath)
}
// Some TypeScript packages needs to be compiled first in order for their dependencies to compile
// successfully.
// Ideally this should have been taken care of by a dependency resolution, but for now the packages
// that needs to be built serially is listed below.
// Note: If you run into problems with packages that errors during TS compile due to issues with
// another package in this monorepo it might help adding it to this array
const BUILD_SERIALLY = [['packages/@sanity/mutator', 'packages/@sanity/base']]
const TS_PROJECTS = BUILD_SERIALLY.concat(
PACKAGE_PATHS.filter(isTSProject).filter(
packagePath => !BUILD_SERIALLY.some(entry => entry.some(pkgPath => pkgPath === packagePath))
)
)
const buildTS = parallel(
TS_PROJECTS.map(projectPath =>
Array.isArray(projectPath)
? series(projectPath.map(buildTypeScript))
: buildTypeScript(projectPath)
)
)
const watchTS = parallel(
flatten(TS_PROJECTS).map(packageDir =>
watchPackage(
compileTaskName('watch', packageDir, 'TS'),
packageDir,
buildTypeScript(packageDir)
)
)
)
const buildJSAndAssets = parallel(PACKAGE_PATHS.map(buildPackage))
const watchJSAndAssets = parallel(
PACKAGE_PATHS.map(packageDir =>
watchPackage(
compileTaskName('watch', packageDir, 'JS/Assets'),
packageDir,
buildPackage(packageDir)
)
)
)
function studioTask(name, port) {
return series(
buildJSAndAssets,
parallel(buildTS, watchJSAndAssets, watchTS, function runStudio() {
log(`Starting ${name}…`)
runSanityStart(path.join(__dirname, 'packages', name), port).pipe(
through((data, enc, cb) => {
log(data.toString())
cb()
})
)
})
)
}
;[
['test-studio', '3333'],
['movies-studio', '3334'],
['example-studio', '3335'],
['blog-studio', '3336'],
['ecommerce-studio', '3337'],
['clean-studio', '3338'],
['storybook', '9002']
].forEach(([name, port]) => {
exports[name] = studioTask(name, port)
})
exports.build = parallel(buildJSAndAssets, buildTS)
exports.watch = series(parallel(buildJSAndAssets, buildTS), parallel(watchJSAndAssets, watchTS))
exports.clean = () => del(PACKAGE_PATHS.map(pth => path.join(pth, DEST_DIR)))