forked from mjmlio/mjml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
133 lines (116 loc) · 3.82 KB
/
gulpfile.babel.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
import { argv } from 'yargs'
import { cd, exec, rm } from 'shelljs'
import watch from 'gulp-watch'
import debug from 'gulp-debug'
import babel from 'gulp-babel'
import fs from 'fs'
import gulp from 'gulp'
import newer from 'gulp-newer'
import path from 'path'
import through from 'through2'
import lazypipe from 'lazypipe'
const PACKAGES_PATH = path.resolve(__dirname, './packages')
const DEV_FILES = `${PACKAGES_PATH}/*/src/**/*.js`
const packages = fs.readdirSync(PACKAGES_PATH).filter(file => {
return fs.statSync(path.resolve(PACKAGES_PATH, file)).isDirectory()
}).reduce((acc, file) => ({
...acc,
[file]: path.resolve(PACKAGES_PATH, file)
}), {})
let srcEx
let libFragment
if (path.win32 === path) {
srcEx = /(packages\\[^\\]+)\\src\\/
libFragment = '$1\\lib\\'
} else {
srcEx = /(packages\/[^\/]+)\/src\//
libFragment = '$1/lib/'
}
const processJS = lazypipe()
.pipe(() => through.obj((file, encoding, callback) => {
file.contents = new Buffer(String(file.contents).replace(/__MJML_VERSION__/g, require(path.resolve(PACKAGES_PATH, `${file.relative.split(path.sep)[0]}/package.json`)).version))
callback(null, file)
}))
.pipe(() => through.obj((file, encoding, callback) => {
file._path = file.path
file.path = file.path.replace(srcEx, libFragment)
callback(null, file)
}))
.pipe(() => newer(PACKAGES_PATH))
.pipe(babel)
.pipe(() => gulp.dest(PACKAGES_PATH))
gulp.task('watch', () => {
return watch(DEV_FILES)
.pipe(debug({title: 'updated:'}))
.pipe(processJS())
})
gulp.task('build', () => {
return gulp.src(DEV_FILES)
.pipe(processJS())
})
gulp.task('install', () => {
return Promise.all(
Object.keys(packages).map(packageName => new Promise(resolve => {
cd(packages[packageName])
exec('yarn')
resolve()
}))
)
})
gulp.task('test', () => {
return Promise.all(
Object.keys(packages).map(packageName => new Promise(resolve => {
cd(packages[packageName])
// test if there's a test directory
exec('mocha --compilers js:babel-register')
resolve()
}))
)
})
gulp.task('clean', () => {
// Remove package node_modules
return Promise.all(
Object.keys(packages).map(packageName => new Promise(resolve => {
rm('-rf', path.resolve(packages[packageName], 'node_modules'), path.resolve(packages[packageName], 'lib'), path.resolve(packages[packageName], 'dist'))
resolve()
}))
)
})
gulp.task('version', () => {
// Try to derive package name from directory where this was run from
const pwd = process.env.PWD
const pwdPackageName = Object.keys(packages).reduce((prev, name) => {
return packages[name] === pwd ? name : prev
}, undefined)
// Check params
const packageName = argv.pkg || argv.p || pwdPackageName
const version = argv.version || argv.v
if (!packageName || !version) {
throw new Error('Usage: gulp version -p <package> -v <version>')
}
// Bump the version
cd(packages[packageName])
const execResult = exec(`npm version ${version}`)
const bumpedVersion = execResult.toString().replace('\n', '').replace('v', '')
// Commit and tag
exec(`git add ${packages[packageName]}/package.json`)
const message = `${packageName}@${bumpedVersion}`
exec(`git commit -m "${message}"`)
const tagName = `${packageName}-v${bumpedVersion}`
exec(`git tag ${tagName}`)
})
gulp.task('publish', ['build'], () => {
// Try to derive package name from directory where this was run from
const pwd = process.env.PWD
const pwdPackageName = Object.keys(packages).reduce((prev, name) => {
return packages[name] === pwd ? name : prev
}, undefined)
// Check params
const packageName = argv.pkg || argv.p || pwdPackageName
if (!packageName) {
throw new Error('Usage: gulp publish -p <package> -t <tag>')
}
// Publish
cd(packages[packageName])
exec(`npm publish${argv.t ? ` --tag ${argv.t}` : ''}`)
})