-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
191 lines (166 loc) · 4.96 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
185
186
187
188
189
190
191
'use strict'
const path = require('path')
const fs = require('fs')
const gulp = require('gulp')
const stripper = require('gulp-strip-comments')
// const concat = require('gulp-concat')
const header = require('gulp-header')
const del = require('del')
const cp = require('child_process')
const pkg = require('./package.json')
let headerComment = '/**\n * v' + pkg.version + ' generated on: ' +
(new Date()) + '\n * Copyright (c) 2014-' + (new Date()).getFullYear() +
', Ecor Ventures LLC. All Rights Reserved. See LICENSE (BSD3).\n */\n'
const DIR = {
source: path.resolve('./'),
shared: path.resolve('./shared/data'),
dist: path.resolve('./dist')
}
const walk = function (dir) {
let files = []
fs.readdirSync(dir).forEach(function (filepath) {
filepath = path.join(dir, filepath)
const stat = fs.statSync(filepath)
if (stat.isDirectory()) {
files = files.concat(walk(filepath))
} else {
files.push(filepath)
}
})
return files
}
const clean = function () {
try {
fs.accessSync(DIR.dist, fs.F_OK)
del.sync(DIR.dist)
} catch (e) {}
}
gulp.task('clear', function () {
clean()
})
// Create a clean build
gulp.task('clean', function (next) {
console.log('Cleaning distribution.')
clean()
fs.mkdirSync(DIR.dist)
next()
})
gulp.task('generate', function () {
// Shared codebase
const primaryshared = walk(DIR.shared)
let primarysharedjs = []
let primarysharedother = []
primaryshared.forEach(function (file) {
if (file.indexOf('.git') < 0) {
if (/[^.*\.js]$/gi.test(file)) { // eslint-disable-line
primarysharedother.push(file)
} else {
primarysharedjs.push(file)
}
}
})
console.log('Stripping comments from', primarysharedjs)
primarysharedjs.forEach(function (file) {
let localpath = path.dirname(file.replace(DIR.source + path.sep, ''))
gulp.src(file)
.pipe(stripper())
.pipe(header(headerComment))
.pipe(gulp.dest(path.join(DIR.dist, localpath)))
})
console.log('Copying', primarysharedother)
primarysharedother.forEach(function (file) {
let localpath = path.dirname(file.replace(DIR.source + path.sep, ''))
gulp.src(file)
.pipe(header(headerComment))
.pipe(gulp.dest(path.join(DIR.dist, localpath)))
})
// Primary Files
const files = ['data.js'].map(function (file) {
return path.join(DIR.source, file)
}).filter(function (file) {
try {
fs.accessSync(file, fs.F_OK)
return true
} catch (e) {
return false
}
})
let filesjs = []
let filesother = []
files.forEach(function (file) {
if (file.indexOf('.git') < 0) {
if (/[^.*\.js]$/gi.test(file)) { // eslint-disable-line
filesother.push(file)
} else {
filesjs.push(file)
}
}
})
console.log('Stripping comments from', filesjs)
filesjs.forEach(function (file) {
let localpath = path.dirname(file.replace(DIR.source + path.sep, ''))
gulp.src(file)
.pipe(stripper())
.pipe(header(headerComment))
.pipe(gulp.dest(path.join(DIR.dist, localpath)))
})
console.log('Copying', filesother)
filesother.forEach(function (file) {
let localpath = path.dirname(file.replace(DIR.source + path.sep, ''))
gulp.src(file)
.pipe(header(headerComment))
.pipe(gulp.dest(path.join(DIR.dist, localpath)))
})
// Copy other files
const assets = ['LICENSE', '.npmignore'].map(function (file) {
return path.join(DIR.source, file)
})
console.log('Copying other assets', assets)
gulp.src(assets)
.pipe(gulp.dest(DIR.dist))
// Update package.json for production release
let newpkg = {}
const pkgitems = [
'name',
'version',
'description',
'main',
'repository',
'keywords',
'preferGlobal',
'engines',
'author',
'contributors',
'homepage',
'license',
'dependencies'
]
Object.keys(pkg).forEach(function (attr) {
if (pkgitems.indexOf(attr) >= 0) {
newpkg[attr] = pkg[attr]
}
})
console.log('Generating package.json...')
fs.writeFileSync(path.join(DIR.dist, 'package.json'), JSON.stringify(newpkg, null, 2))
console.log('DONE!')
})
gulp.task('prereleasecheck', function (next) {
console.log('Checking if package already exists.')
const child = cp.spawn('npm', ['info', pkg.name])
let data = ''
child.stdout.on('data', function (chunk) {
data += chunk.toString()
})
child.on('close', function () {
const re = new RegExp('latest: \'' + pkg.version + '\'')
if (re.exec(data) === null) {
fs.writeFileSync(path.join('./', 'postprocess.sh'), 'npm publish')
next()
} else {
fs.writeFileSync(path.join('./', 'postprocess.sh'), 'echo "The version has not changed (' + pkg.version + '). A new release is unnecessary. Aborting deployment with success code."')
console.log('The version has not changed (' + pkg.version + '). A new release is unnecessary. Aborting deployment with success code.')
process.exit(0)
}
})
})
gulp.task('build', ['clean', 'generate'])