Skip to content

Commit 66eab35

Browse files
authored
Merge pull request brave#3085 from brave/async-update-patches
update_patches command becomes async
2 parents 537a288 + c254926 commit 66eab35

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

lib/updatePatches.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('fs-extra')
33
const config = require('../lib/config')
44
const util = require('../lib/util')
55

6-
const updatePatches = (options) => {
6+
const updatePatches = async (options) => {
77
config.update(options)
88

99
const patchDir = path.join(config.projects['brave-core'].dir, 'patches')
@@ -18,8 +18,7 @@ const updatePatches = (options) => {
1818

1919
// grab Modified (and later Deleted) files but not Created (since we copy those)
2020
const modifiedDiffArgs = ['diff', '--diff-filter=M', '--name-only', '--ignore-space-at-eol']
21-
const modifiedDiff = util.run('git', modifiedDiffArgs, runOptionsChrome)
22-
const modifiedFileList = modifiedDiff.stdout.toString()
21+
const modifiedFileList = (await util.runAsync('git', modifiedDiffArgs, runOptionsChrome))
2322
.split('\n')
2423
.filter(s => s.length > 0 &&
2524
!s.startsWith('chrome/app/theme/default') &&
@@ -38,8 +37,8 @@ const updatePatches = (options) => {
3837

3938
// grab every existing patch file in the dir (at this point, patchfiles for now-unmodified files live on)
4039
const existingFileArgs = ['ls-files', '--exclude-standard']
41-
let existingFileOutput = util.run('git', existingFileArgs, runOptionsPatch)
42-
let existingFileList = existingFileOutput.stdout.toString().split('\n').filter(s => s.length > 0)
40+
let existingFileList = (await util.runAsync('git', existingFileArgs, runOptionsPatch))
41+
.split('\n').filter(s => s.length > 0)
4342

4443
// Add files here we specifically want to keep around regardless
4544
const exclusionList = []
@@ -61,22 +60,20 @@ const updatePatches = (options) => {
6160
// appear, you can quickly patch this by changing the separator, even
6261
// to something longer
6362

64-
let n = modifiedFileList.length
65-
66-
for (let i = 0; i < n; i++) {
67-
const old = modifiedFileList[i]
68-
const revised = substitutedFileList[i]
69-
63+
let writeOpsDoneCount = 0
64+
let writePatchOps = modifiedFileList.map(async (old, i) => {
7065
const singleDiffArgs = ['diff', '--src-prefix=a/', '--dst-prefix=b/', '--full-index', old]
71-
let singleDiff = util.run('git', singleDiffArgs, runOptionsChrome)
66+
const patchContents = await util.runAsync('git', singleDiffArgs, runOptionsChrome)
67+
const patchFilename = substitutedFileList[i]
68+
await fs.writeFile(path.join(patchDir, patchFilename), patchContents)
7269

73-
const contents = singleDiff.stdout.toString()
74-
const filename = revised
70+
writeOpsDoneCount++
71+
console.log(
72+
`updatePatches wrote ${writeOpsDoneCount} / ${modifiedFileList.length}: ${patchFilename}`
73+
)
74+
})
7575

76-
fs.writeFileSync(path.join(patchDir, filename), contents)
77-
78-
console.log('updatePatches wrote ' + (1 + i) + '/' + n + ': ' + filename)
79-
}
76+
await Promise.all(writePatchOps)
8077

8178
// regular rm patchfiles whose target is no longer modified
8279
let m = cruftList.length

lib/util.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require('path')
2-
const spawnSync = require('child_process').spawnSync
2+
const { spawn, spawnSync } = require('child_process')
33
const config = require('./config')
44
const fs = require('fs-extra')
55
const crypto = require('crypto')
@@ -34,6 +34,32 @@ const util = {
3434
return prog
3535
},
3636

37+
runAsync: (cmd, args = [], options = {}) => {
38+
console.log(cmd, args.join(' '))
39+
const continueOnFail = options.continueOnFail
40+
delete options.continueOnFail
41+
return new Promise((resolve, reject) => {
42+
const prog = spawn(cmd, args, options)
43+
let stderr = ''
44+
let stdout = ''
45+
prog.stderr.on('data', data => {
46+
stderr += data
47+
})
48+
prog.stdout.on('data', data => {
49+
stdout += data
50+
})
51+
prog.on('close', statusCode => {
52+
const hasFailed = statusCode !== 0
53+
if (hasFailed && !continueOnFail) {
54+
console.log(stdout)
55+
console.error(stderr)
56+
process.exit(1)
57+
}
58+
resolve(stdout)
59+
})
60+
})
61+
},
62+
3763
buildGClientConfig: () => {
3864
function replacer(key, value) {
3965
return value;

0 commit comments

Comments
 (0)