-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-release.js
108 lines (99 loc) · 2.7 KB
/
build-release.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
import createFileTransformerSync from './utility/createFileTransformerSync'
import createJsonTransformer from './utility/createJsonTransformer'
import guardedSpawnSync from '../scripts/utility/guardedSpawnSync'
import { join } from 'path'
import { relative } from 'path'
import { resolve } from 'path'
const workingDirectoryPath = join(__dirname, '..')
const inputDirectoryPath = workingDirectoryPath
const outputDirectoryPath = join(workingDirectoryPath, 'release')
const copyFileSync = createFileTransformerSync(input => input)
build({
inputDirectoryPath,
outputDirectoryPath,
preset: 'es2015-node4',
engines: {
node: '>=4.0.0'
}
})
function build({
inputDirectoryPath,
outputDirectoryPath,
preset,
engines
}) {
// Build source files.
guardedSpawnSync('babel', [
'*.js',
'--out-dir', relative(workingDirectoryPath, resolve(workingDirectoryPath, outputDirectoryPath)),
'--source-maps',
'--presets', preset,
'--ignore', 'node_modules/',
'--ignore', 'examples/',
'--ignore', 'release/',
'--ignore', 'hooks/',
'--ignore', 'scripts/'
], {
cwd: workingDirectoryPath,
stdio: 'inherit'
})
// Build package.json.
createFileTransformerSync(
createJsonTransformer(
createPackageTransformer({ engines }),
{ sort: true }
)
)({
workingDirectoryPath,
inputFilePath: join(inputDirectoryPath, 'package.json'),
outputFilePath: join(outputDirectoryPath, 'package.json')
})
// Build mocha.opts
createFileTransformerSync(
input => input.toString().replace('--compilers js:babel-register', '')
)({
workingDirectoryPath,
inputFilePath: join(inputDirectoryPath, 'test/mocha.opts'),
outputFilePath: join(outputDirectoryPath, 'test/mocha.opts')
})
// Build readme.md and .npmignore.
;[ 'readme.md', '.npmignore' ].forEach(relativeFilePath =>
copyFileSync({
workingDirectoryPath,
inputFilePath: join(inputDirectoryPath, relativeFilePath),
outputFilePath: join(outputDirectoryPath, relativeFilePath)
})
)
}
function createPackageTransformer({ engines }) {
return function transformPackage(pkg) {
return Object.keys(pkg)
.filter(key => !/^(private)$/.test(key))
.reduce((map, key) => {
map[key] = key === 'devDependencies'
? transformDevDependencies(pkg[key])
: key === 'scripts'
? transformScripts(pkg[key])
: pkg[key]
return map
}, {
engines
})
}
}
function transformDevDependencies(devDependencies) {
return Object.keys(devDependencies)
.filter(key => /^(mocha|must)$/.test(key))
.reduce((map, key) => {
map[key] = devDependencies[key]
return map
}, {})
}
function transformScripts(scripts) {
return Object.keys(scripts)
.filter(key => /^(test|prepublish)$/.test(key))
.reduce((map, key) => {
map[key] = scripts[key]
return map
}, {})
}