Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open fewer files at once during migration. #2342

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}