-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·332 lines (268 loc) · 10.3 KB
/
index.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env node
import fs from 'fs-extra'
import chalk from 'chalk'
import merge from 'just-merge'
import { exec } from 'child_process'
import { waterfall } from 'async'
const pkg = fs.readJsonSync('./package.json')
const awk = process.platform === 'win32' ? 'gawk' : 'awk'
const noRunIfEmpty = process.platform !== 'darwin' ? '--no-run-if-empty ' : ''
const wpDeployer = async () => {
console.log(chalk.cyan('Processing...'))
const clearTrunk = (settings) => {
return function (settings, callback) {
console.log('Clearing trunk.')
exec(`rm -fr ${settings.svnPath}/trunk/*`, function (error, stdout, stderr) {
callback(null, settings)
})
}
}
const clearAssets = (settings) => {
return function (settings, callback) {
console.log('Clearing assets.')
exec(`rm -fr ${settings.svnPath}/assets/*`, function (error, stdout, stderr) {
callback(null, settings)
})
}
}
const prepareThemeTmp = (settings) => {
return function (settings, callback) {
console.log('Preparing temporary folder.')
exec(`rm -fr ${settings.svnPath}/`, function (error, stdout, stderr) {
callback(null, settings)
})
}
}
const clearTheme = (settings) => {
return function (settings, callback) {
console.log('Clearing theme.')
exec(`rm -fr ${settings.svnPath}/${settings.newVersion}/*`, function (error, stdout, stderr) {
callback(null, settings)
})
}
}
const checkoutDir = (dir, settings) => {
return function (settings, callback) {
console.log(`Checking out ${settings.url}${dir}/...`)
const checkoutUrl = `${settings.url}${dir}/`
const targetPath = `${settings.svnPath}/${dir}`
exec(`svn co --force-interactive --username="${settings.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: settings.maxBuffer }, function (error, stdout, stderr) {
if (error !== null) {
console.error(`Checkout of ${settings.url}${dir}/ unsuccessful: ${error}`)
} else {
console.log('Check out complete.')
}
callback(null, settings)
})
}
}
const checkoutTheme = (settings) => {
return function (settings, callback) {
console.log(`Checking out ${settings.url}...`)
const checkoutUrl = `${settings.url}/`
const targetPath = `${settings.svnPath}/`
exec(`svn co --force-interactive --username="${settings.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: settings.maxBuffer }, function (error, stdout, stderr) {
if (error !== null) {
console.error(`Checkout of ${settings.url}/ unsuccessful: ${error}`)
} else {
console.log('Check out complete.')
}
callback(null, settings)
})
}
}
const copyDirectory = (srcDir, destDir, callback) => {
if (srcDir.substr(-1) !== '/') {
srcDir = `${srcDir}/`
}
fs.copySync(srcDir, destDir)
callback()
}
const copyBuild = (settings) => {
return function (settings, callback) {
console.log(`Copying build directory: ${settings.buildDir}`)
copyDirectory(settings.buildDir, `${settings.svnPath}/trunk/`, function () {
callback(null, settings)
})
}
}
const copyTheme = (settings) => {
return function (settings, callback) {
console.log(`Copying build directory: ${settings.buildDir}`)
copyDirectory(settings.buildDir, `${settings.svnPath}/${settings.newVersion}/`, function () {
callback(null, settings)
})
}
}
const copyAssets = (settings) => {
return function (settings, callback) {
console.log('Copying assets')
copyDirectory(settings.assetsDir, `${settings.svnPath}/assets/`, function () {
callback(null, settings)
})
}
}
const addFiles = (settings, callback) => {
return function (settings, callback) {
console.log('Adding files in trunk')
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
exec(cmd, { cwd: `${settings.svnPath}/trunk` }, function (error, stdout, stderr) {
callback(null, settings)
})
}
}
const addThemeFiles = (settings, callback) => {
return function (settings, callback) {
console.log('Adding theme files')
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
exec(cmd, { cwd: `${settings.svnPath}/${settings.newVersion}` }, function (error, stdout, stderr) {
callback(null, settings)
})
}
}
const addAssets = (settings, callback) => {
return function (settings, callback) {
console.log('Adding assets')
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
exec(cmd, { cwd: `${settings.svnPath}/assets` }, function (error, stdout, stderr) {
callback(null, settings)
})
}
}
const commitToTrunk = (settings, callback) => {
return function (settings, callback) {
const commitMsg = `Committing ${settings.newVersion} to trunk`
const cmd = `svn commit --force-interactive --username="${settings.username}" -m "${commitMsg}"`
exec(cmd, { cwd: `${settings.svnPath}/trunk` }, function (error, stdout, stderr) {
if (error !== null) {
console.error(chalk.red(`Failed to commit to trunk: ${error}`))
}
callback(null, settings)
})
}
}
const commitToAssets = (settings, callback) => {
return function (settings, callback) {
const commitMsg = 'Committing assets'
const cmd = `svn commit --force-interactive --username="${settings.username}" -m "${commitMsg}"`
exec(cmd, { cwd: `${settings.svnPath}/assets` }, function (error, stdout, stderr) {
if (error !== null) {
console.error(chalk.red(`Failed to commit to assets: ${error}`))
}
callback(null, settings)
})
}
}
const commitTheme = (settings, callback) => {
return function (settings, callback) {
const commitMsg = `Committing theme ${settings.newVersion}`
console.log(commitMsg)
const cmd = `svn commit --force-interactive --username="${settings.username}" -m "${commitMsg}"`
exec(cmd, { cwd: `${settings.svnPath}/${settings.newVersion}` }, function (error, stdout, stderr) {
if (error !== null) {
console.error(chalk.red(`Failed to commit theme: ${error}`))
}
callback(null, settings)
})
}
}
const commitTag = (settings, callback) => {
return function (settings, callback) {
const tagCommitMsg = `Tagging ${settings.newVersion}`
console.log(tagCommitMsg)
const cmd = 'svn copy ' + settings.url + 'trunk/ ' + settings.url + 'tags/' + settings.newVersion + '/ ' + ' ' + ' --force-interactive --username="' + settings.username + '" -m "' + tagCommitMsg + '"'
exec(cmd, { cwd: settings.svnPath }, function (error, stdout, stderr) {
if (error !== null) {
console.error(`Failed to commit tag: ${error}`)
}
callback(null, settings)
})
}
}
const createThemeTag = (settings, callback) => {
return function (settings, callback) {
console.log(`Creating tag ${settings.newVersion}`)
const cmd = `svn copy ${settings.earlierVersion} ${settings.newVersion}`
exec(cmd, { cwd: settings.svnPath }, function (error, stdout, stderr) {
if (error !== null) {
console.error(`Failed to create tag: ${error}`)
}
callback(null, settings)
})
}
}
const defaults = {
url: '',
slug: `${pkg.name}`,
mainFile: `${pkg.name}.php`,
username: '',
repoType: 'plugin',
buildDir: 'dist',
maxBuffer: 200 * 1024,
deployTrunk: true,
deployTag: true,
deployAssets: false,
assetsDir: '.wordpress-org',
tmpDir: '/tmp/',
earlierVersion: '',
newVersion: pkg.version
}
let settings = merge(defaults, Object.prototype.hasOwnProperty.call(pkg, 'wpDeployer') ? pkg.wpDeployer : {})
settings = merge(settings, {
svnPath: settings.tmpDir.replace(/\/$|$/, '/') + settings.slug
})
settings.buildDir = settings.buildDir.replace(/\/$|$/, '/')
if (!settings.url) {
if (settings.repoType === 'plugin') {
settings.url = `https://plugins.svn.wordpress.org/${pkg.name}/`
} else if (settings.repoType === 'theme') {
settings.url = `https://themes.svn.wordpress.org/${pkg.name}/`
}
}
if (!settings.username) {
console.error(chalk.red('Username is required.'))
process.exit()
}
if (!settings.earlierVersion && settings.repoType === 'theme') {
console.error(chalk.red('For repoType theme, earlierVersion is required.'))
process.exit()
}
let steps = []
if (settings.repoType === 'plugin') {
steps = [
function (callback) {
callback(null, settings)
},
settings.deployTrunk ? checkoutDir('trunk', settings) : null,
settings.deployTrunk ? clearTrunk(settings) : null,
settings.deployTrunk ? copyBuild(settings) : null,
settings.deployTrunk ? addFiles(settings) : null,
settings.deployTrunk ? commitToTrunk(settings) : null,
settings.deployTag ? commitTag(settings) : null,
settings.deployAssets ? checkoutDir('assets', settings) : null,
settings.deployAssets ? clearAssets(settings) : null,
settings.deployAssets ? copyAssets(settings) : null,
settings.deployAssets ? addAssets(settings) : null,
settings.deployAssets ? commitToAssets(settings) : null
].filter(function (val) { return val !== null })
} else if (settings.repoType === 'theme') {
steps = [
function (callback) {
callback(null, settings)
},
prepareThemeTmp(settings),
checkoutTheme(settings),
createThemeTag(settings),
clearTheme(settings),
copyTheme(settings),
addThemeFiles(settings),
commitTheme(settings)
].filter(function (val) { return val !== null })
}
waterfall(steps, function (err, result) {
})
}
wpDeployer()