Skip to content

Commit

Permalink
Open fewer files at once during migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
nataliecarey committed Sep 19, 2023
1 parent 40af747 commit 78c92d8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- [#2342: Open fewer files at once during migration](https://github.com/alphagov/govuk-prototype-kit/pull/2342)

## 13.13.2

### Fixes
Expand Down
64 changes: 64 additions & 0 deletions lib/utils/asyncSerialMap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { asyncSeriesMap } = require('./asyncSeriesMap')
describe('asyncSerialMap', () => {
const nextTick = () => new Promise((resolve) => {
process.nextTick(() => resolve())
})

it('should work through an array and return the results', async () => {
const arr = ['abc', 'def', 'ghi']
const handler = async (item) => `"${item}"`

const result = await asyncSeriesMap(arr, handler)

expect(result).toEqual([
'"abc"',
'"def"',
'"ghi"'
])
})

it('should work through an array and return the results', async () => {
const arr = ['abc', 'def', 'ghi']
const handler = async (item, index, array) => `"${item}|${index}|${JSON.stringify(array)}"`

const result = await asyncSeriesMap(arr, handler)

expect(result).toEqual([
'"abc|0|["abc","def","ghi"]"',
'"def|1|["abc","def","ghi"]"',
'"ghi|2|["abc","def","ghi"]"'
])
})

it('should run these in series', async () => {
let latestResolve
let callCount = 0
const handler = (item) => new Promise((resolve, reject) => {
callCount++
latestResolve = resolve
})

const resultPromise = asyncSeriesMap(['abc', 'def', 'ghi'], handler)

await nextTick()

expect(callCount).toBe(1)
latestResolve('this is the first')

await nextTick()

expect(callCount).toBe(2)
latestResolve('this is the second')

await nextTick()

expect(callCount).toBe(3)
latestResolve('this is the third')

expect(await resultPromise).toEqual([
'this is the first',
'this is the second',
'this is the third'
])
})
})
12 changes: 12 additions & 0 deletions lib/utils/asyncSeriesMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
async function asyncSeriesMap (arr, handler) {
const results = []
let index = -1
while (arr.length > ++index) {
results.push(await handler(arr[index], index, arr))
}
return results
}

module.exports = {
asyncSeriesMap
}
5 changes: 3 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const plugins = require('../plugins/plugins')
const routes = require('../routes/api')
const { appDir, projectDir, packageDir } = require('./paths')
const fse = require('fs-extra')
const { asyncSeriesMap } = require('./asyncSeriesMap')

// Tweak the Markdown renderer
const defaultMarkedRenderer = marked.defaults.renderer || new marked.Renderer()
Expand Down Expand Up @@ -234,7 +235,7 @@ function recursiveDirectoryContentsSync (baseDir) {

async function searchAndReplaceFiles (dir, searchText, replaceText, extensions) {
const files = await fsp.readdir(dir)
const modifiedFiles = await Promise.all(files.map(async file => {
const modifiedFiles = await asyncSeriesMap(files, async file => {
const filePath = path.join(dir, file)
const fileStat = await fsp.stat(filePath)

Expand All @@ -248,7 +249,7 @@ async function searchAndReplaceFiles (dir, searchText, replaceText, extensions)
return filePath
}
}
}))
})

return modifiedFiles.flat().filter(Boolean)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@
"/node_modules/",
"/tmp/"
],
"testTimeout": 30000
"testTimeout": 5000
}
}

0 comments on commit 78c92d8

Please sign in to comment.