Skip to content

Commit 01f7604

Browse files
authored
Merge pull request #15 from ernilambar/refine-approach
Robust approach
2 parents 9af5cf1 + 4c03341 commit 01f7604

File tree

4 files changed

+154
-178
lines changed

4 files changed

+154
-178
lines changed

index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
import fs from 'fs-extra'
1212
import chalk from 'chalk'
13-
import { exec } from 'child_process'
14-
import { waterfall } from 'async'
13+
import { exec as execCb } from 'child_process'
14+
import { promisify } from 'util'
1515
import { resolveSettings } from './lib/config.js'
1616
import { createPluginSteps, createThemeSteps } from './lib/steps.js'
1717

18+
const exec = promisify(execCb)
19+
1820
const pkg = fs.readJsonSync('./package.json')
1921

2022
const awk = process.platform === 'win32' ? 'gawk' : 'awk'
@@ -33,15 +35,19 @@ const wpDeployer = async () => {
3335
process.exit()
3436
}
3537

36-
// Build steps and run waterfall (same step sequence and callback contract as before)
3738
const helpers = { exec, chalk, fs, awk, noRunIfEmpty }
3839
const steps = settings.repoType === 'plugin'
3940
? createPluginSteps(settings, helpers)
4041
: createThemeSteps(settings, helpers)
4142

42-
waterfall(steps, function (err, result) {
43-
if (err) console.error(chalk.red(err))
44-
})
43+
try {
44+
let s = settings
45+
for (const step of steps) {
46+
s = await step(s)
47+
}
48+
} catch (err) {
49+
console.error(chalk.red(err))
50+
}
4551
}
4652

4753
wpDeployer()

lib/steps.js

Lines changed: 112 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,141 @@
11
/**
2-
* Step factories for the deploy waterfall.
3-
* Each step is a function (settings, callback) used by async.waterfall.
2+
* Step factories for the deploy pipeline.
3+
* Each step is an async function (settings) => settings used in sequence.
44
* Config and helpers are passed in; this module does not read package.json.
55
*/
66

7-
function copyDirectory (fs, srcDir, destDir, callback) {
7+
function copyDirectory (fs, srcDir, destDir) {
88
if (srcDir.slice(-1) !== '/') {
99
srcDir = `${srcDir}/`
1010
}
1111
fs.copySync(srcDir, destDir)
12-
callback()
1312
}
1413

1514
export function createPluginSteps (settings, { exec, chalk, fs, awk, noRunIfEmpty }) {
1615
const clearTrunk = (s) => {
17-
return function (s, callback) {
16+
return async function (s) {
1817
console.log('Clearing trunk.')
19-
exec(`rm -fr ${s.svnPath}/trunk/*`, function (error, stdout, stderr) {
20-
if (error) console.error(error)
21-
callback(null, s)
22-
})
18+
try {
19+
await exec(`rm -fr ${s.svnPath}/trunk/*`)
20+
} catch (error) {
21+
console.error(error)
22+
}
23+
return s
2324
}
2425
}
2526

2627
const clearAssets = (s) => {
27-
return function (s, callback) {
28+
return async function (s) {
2829
console.log('Clearing assets.')
29-
exec(`rm -fr ${s.svnPath}/assets/*`, function (error, stdout, stderr) {
30-
if (error) console.error(error)
31-
callback(null, s)
32-
})
30+
try {
31+
await exec(`rm -fr ${s.svnPath}/assets/*`)
32+
} catch (error) {
33+
console.error(error)
34+
}
35+
return s
3336
}
3437
}
3538

3639
const checkoutDir = (dir, s) => {
37-
return function (s, callback) {
40+
return async function (s) {
3841
console.log(`Checking out ${s.url}${dir}/...`)
3942
const checkoutUrl = `${s.url}${dir}/`
4043
const targetPath = `${s.svnPath}/${dir}`
41-
exec(`svn co --force-interactive --username="${s.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: s.maxBuffer }, function (error, stdout, stderr) {
42-
if (error !== null) {
43-
console.error(`Checkout of ${s.url}${dir}/ unsuccessful: ${error}`)
44-
} else {
45-
console.log('Check out complete.')
46-
}
47-
callback(null, s)
48-
})
44+
try {
45+
await exec(`svn co --force-interactive --username="${s.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: s.maxBuffer })
46+
console.log('Check out complete.')
47+
} catch (error) {
48+
console.error(`Checkout of ${s.url}${dir}/ unsuccessful: ${error}`)
49+
}
50+
return s
4951
}
5052
}
5153

5254
const copyBuild = (s) => {
53-
return function (s, callback) {
55+
return async function (s) {
5456
console.log(`Copying build directory: ${s.buildDir}`)
55-
copyDirectory(fs, s.buildDir, `${s.svnPath}/trunk/`, function () {
56-
callback(null, s)
57-
})
57+
copyDirectory(fs, s.buildDir, `${s.svnPath}/trunk/`)
58+
return s
5859
}
5960
}
6061

6162
const addFiles = (s) => {
62-
return function (s, callback) {
63+
return async function (s) {
6364
console.log('Adding files in trunk')
6465
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
6566
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
66-
exec(cmd, { cwd: `${s.svnPath}/trunk` }, function (error, stdout, stderr) {
67-
if (error) console.error(error)
68-
callback(null, s)
69-
})
67+
try {
68+
await exec(cmd, { cwd: `${s.svnPath}/trunk` })
69+
} catch (error) {
70+
console.error(error)
71+
}
72+
return s
7073
}
7174
}
7275

7376
const commitToTrunk = (s) => {
74-
return function (s, callback) {
77+
return async function (s) {
7578
const commitMsg = `Committing ${s.newVersion} to trunk`
7679
const cmd = `svn commit --force-interactive --username="${s.username}" -m "${commitMsg}"`
77-
exec(cmd, { cwd: `${s.svnPath}/trunk` }, function (error, stdout, stderr) {
78-
if (error !== null) {
79-
console.error(chalk.red(`Failed to commit to trunk: ${error}`))
80-
}
81-
callback(null, s)
82-
})
80+
try {
81+
await exec(cmd, { cwd: `${s.svnPath}/trunk` })
82+
} catch (error) {
83+
console.error(chalk.red(`Failed to commit to trunk: ${error}`))
84+
}
85+
return s
8386
}
8487
}
8588

8689
const commitTag = (s) => {
87-
return function (s, callback) {
90+
return async function (s) {
8891
const tagCommitMsg = `Tagging ${s.newVersion}`
8992
console.log(tagCommitMsg)
9093
const cmd = 'svn copy ' + s.url + 'trunk/ ' + s.url + 'tags/' + s.newVersion + '/ ' + ' ' + ' --force-interactive --username="' + s.username + '" -m "' + tagCommitMsg + '"'
91-
exec(cmd, { cwd: s.svnPath }, function (error, stdout, stderr) {
92-
if (error !== null) {
93-
console.error(`Failed to commit tag: ${error}`)
94-
}
95-
callback(null, s)
96-
})
94+
try {
95+
await exec(cmd, { cwd: s.svnPath })
96+
} catch (error) {
97+
console.error(`Failed to commit tag: ${error}`)
98+
}
99+
return s
97100
}
98101
}
99102

100103
const copyAssets = (s) => {
101-
return function (s, callback) {
104+
return async function (s) {
102105
console.log('Copying assets')
103-
copyDirectory(fs, s.assetsDir, `${s.svnPath}/assets/`, function () {
104-
callback(null, s)
105-
})
106+
copyDirectory(fs, s.assetsDir, `${s.svnPath}/assets/`)
107+
return s
106108
}
107109
}
108110

109111
const addAssets = (s) => {
110-
return function (s, callback) {
112+
return async function (s) {
111113
console.log('Adding assets')
112114
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
113115
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
114-
exec(cmd, { cwd: `${s.svnPath}/assets` }, function (error, stdout, stderr) {
115-
if (error) console.error(error)
116-
callback(null, s)
117-
})
116+
try {
117+
await exec(cmd, { cwd: `${s.svnPath}/assets` })
118+
} catch (error) {
119+
console.error(error)
120+
}
121+
return s
118122
}
119123
}
120124

121125
const commitToAssets = (s) => {
122-
return function (s, callback) {
126+
return async function (s) {
123127
const commitMsg = 'Committing assets'
124128
const cmd = `svn commit --force-interactive --username="${s.username}" -m "${commitMsg}"`
125-
exec(cmd, { cwd: `${s.svnPath}/assets` }, function (error, stdout, stderr) {
126-
if (error !== null) {
127-
console.error(chalk.red(`Failed to commit to assets: ${error}`))
128-
}
129-
callback(null, s)
130-
})
129+
try {
130+
await exec(cmd, { cwd: `${s.svnPath}/assets` })
131+
} catch (error) {
132+
console.error(chalk.red(`Failed to commit to assets: ${error}`))
133+
}
134+
return s
131135
}
132136
}
133137

134138
const steps = [
135-
function (callback) {
136-
callback(null, settings)
137-
},
138139
settings.deployTrunk ? checkoutDir('trunk', settings) : null,
139140
settings.deployTrunk ? clearTrunk(settings) : null,
140141
settings.deployTrunk ? copyBuild(settings) : null,
@@ -153,93 +154,94 @@ export function createPluginSteps (settings, { exec, chalk, fs, awk, noRunIfEmpt
153154

154155
export function createThemeSteps (settings, { exec, chalk, fs, awk, noRunIfEmpty }) {
155156
const prepareThemeTmp = (s) => {
156-
return function (s, callback) {
157+
return async function (s) {
157158
console.log('Preparing temporary folder.')
158-
exec(`rm -fr ${s.svnPath}/`, function (error, stdout, stderr) {
159-
if (error) console.error(error)
160-
callback(null, s)
161-
})
159+
try {
160+
await exec(`rm -fr ${s.svnPath}/`)
161+
} catch (error) {
162+
console.error(error)
163+
}
164+
return s
162165
}
163166
}
164167

165168
const checkoutTheme = (s) => {
166-
return function (s, callback) {
169+
return async function (s) {
167170
console.log(`Checking out ${s.url}...`)
168171
const checkoutUrl = `${s.url}/`
169172
const targetPath = `${s.svnPath}/`
170-
exec(`svn co --force-interactive --username="${s.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: s.maxBuffer }, function (error, stdout, stderr) {
171-
if (error !== null) {
172-
console.error(`Checkout of ${s.url}/ unsuccessful: ${error}`)
173-
} else {
174-
console.log('Check out complete.')
175-
}
176-
callback(null, s)
177-
})
173+
try {
174+
await exec(`svn co --force-interactive --username="${s.username}" ${checkoutUrl} ${targetPath}`, { maxBuffer: s.maxBuffer })
175+
console.log('Check out complete.')
176+
} catch (error) {
177+
console.error(`Checkout of ${s.url}/ unsuccessful: ${error}`)
178+
}
179+
return s
178180
}
179181
}
180182

181183
const createThemeTag = (s) => {
182-
return function (s, callback) {
184+
return async function (s) {
183185
console.log(`Creating tag ${s.newVersion}`)
184186
const cmd = `svn copy ${s.earlierVersion} ${s.newVersion}`
185-
exec(cmd, { cwd: s.svnPath }, function (error, stdout, stderr) {
186-
if (error !== null) {
187-
console.error(`Failed to create tag: ${error}`)
188-
}
189-
callback(null, s)
190-
})
187+
try {
188+
await exec(cmd, { cwd: s.svnPath })
189+
} catch (error) {
190+
console.error(`Failed to create tag: ${error}`)
191+
}
192+
return s
191193
}
192194
}
193195

194196
const clearTheme = (s) => {
195-
return function (s, callback) {
197+
return async function (s) {
196198
console.log('Clearing theme.')
197-
exec(`rm -fr ${s.svnPath}/${s.newVersion}/*`, function (error, stdout, stderr) {
198-
if (error) console.error(error)
199-
callback(null, s)
200-
})
199+
try {
200+
await exec(`rm -fr ${s.svnPath}/${s.newVersion}/*`)
201+
} catch (error) {
202+
console.error(error)
203+
}
204+
return s
201205
}
202206
}
203207

204208
const copyTheme = (s) => {
205-
return function (s, callback) {
209+
return async function (s) {
206210
console.log(`Copying build directory: ${s.buildDir}`)
207-
copyDirectory(fs, s.buildDir, `${s.svnPath}/${s.newVersion}/`, function () {
208-
callback(null, s)
209-
})
211+
copyDirectory(fs, s.buildDir, `${s.svnPath}/${s.newVersion}/`)
212+
return s
210213
}
211214
}
212215

213216
const addThemeFiles = (s) => {
214-
return function (s, callback) {
217+
return async function (s) {
215218
console.log('Adding theme files')
216219
let cmd = 'svn resolve --accept working -R . && svn status |' + awk + " '/^[?]/{print $2}' | xargs " + noRunIfEmpty + 'svn add;'
217220
cmd += 'svn status | ' + awk + " '/^[!]/{print $2}' | xargs " + noRunIfEmpty + 'svn delete;'
218-
exec(cmd, { cwd: `${s.svnPath}/${s.newVersion}` }, function (error, stdout, stderr) {
219-
if (error) console.error(error)
220-
callback(null, s)
221-
})
221+
try {
222+
await exec(cmd, { cwd: `${s.svnPath}/${s.newVersion}` })
223+
} catch (error) {
224+
console.error(error)
225+
}
226+
return s
222227
}
223228
}
224229

225230
const commitTheme = (s) => {
226-
return function (s, callback) {
231+
return async function (s) {
227232
const commitMsg = `Committing theme ${s.newVersion}`
228233
console.log(commitMsg)
229234
const cmd = `svn commit --force-interactive --username="${s.username}" -m "${commitMsg}"`
230-
exec(cmd, { cwd: `${s.svnPath}/${s.newVersion}` }, function (error, stdout, stderr) {
231-
if (error !== null) {
232-
console.error(chalk.red(`Failed to commit theme: ${error}`))
233-
}
234-
callback(null, s)
235-
})
235+
try {
236+
await exec(cmd, { cwd: `${s.svnPath}/${s.newVersion}` })
237+
} catch (error) {
238+
console.error(chalk.red(`Failed to commit theme: ${error}`))
239+
}
240+
return s
236241
}
237242
}
238243

239244
const steps = [
240-
function (callback) {
241-
callback(null, settings)
242-
},
243245
prepareThemeTmp(settings),
244246
checkoutTheme(settings),
245247
createThemeTag(settings),

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
},
3131
"homepage": "https://github.com/ernilambar/wp-deployer#readme",
3232
"dependencies": {
33-
"async": "^3.2.6",
3433
"chalk": "^5.6.2",
3534
"fs-extra": "^11.3.4",
3635
"just-merge": "^3.2.0"

0 commit comments

Comments
 (0)