Skip to content

Commit fe35a5d

Browse files
committed
fix: use glob to move files
1 parent ec6bc21 commit fe35a5d

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/helpers/files.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
11
// @ts-check
2+
const { cpus } = require('os')
3+
24
const { existsSync, readJson, move, cpSync, copy, writeJson } = require('fs-extra')
35
const pLimit = require('p-limit')
46
const { join } = require('pathe')
7+
const glob = require('tiny-glob')
58

6-
const TEST_ROUTE = /\/\[[^/]+?](?=\/|$)/
9+
const TEST_ROUTE = /(|\/)\[[^/]+?](\/|\.html|$)/
710

811
const isDynamicRoute = (route) => TEST_ROUTE.test(route)
912

1013
exports.moveStaticPages = async ({ netlifyConfig, target, i18n, failBuild }) => {
11-
const root = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless')
12-
const pagesManifestPath = join(root, 'pages-manifest.json')
13-
if (!existsSync(pagesManifestPath)) {
14-
failBuild(`Could not find pages manifest at ${pagesManifestPath}`)
15-
}
14+
console.log('Moving static page files to serve from CDN...')
15+
const root = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless', 'pages')
16+
1617
const files = []
1718

1819
const moveFile = async (file) => {
1920
const source = join(root, file)
20-
// Trim the initial "pages"
21-
const filePath = file.slice(6)
22-
files.push(filePath)
23-
const dest = join(netlifyConfig.build.publish, filePath)
21+
files.push(file)
22+
const dest = join(netlifyConfig.build.publish, source)
2423
await move(source, dest)
2524
}
25+
// Move all static files, except error documents and nft manifests
26+
const pages = await glob('**/!(500|404|*.nft).{html,json}', {
27+
cwd: root,
28+
dot: true,
29+
})
2630

27-
const pagesManifest = await readJson(pagesManifestPath)
28-
// Arbitrary limit of 10 concurrent file moves
29-
const limit = pLimit(10)
30-
const promises = Object.entries(pagesManifest).map(async ([route, filePath]) => {
31-
if (
32-
isDynamicRoute(route) ||
33-
!(filePath.endsWith('.html') || filePath.endsWith('.json')) ||
34-
filePath.endsWith('/404.html') ||
35-
filePath.endsWith('/500.html')
36-
) {
31+
// Limit concurrent file moves to number of cpus or 2 if there is only 1
32+
const limit = pLimit(Math.max(2, cpus().length))
33+
const promises = pages.map(async (filePath) => {
34+
if (isDynamicRoute(filePath)) {
3735
return
3836
}
3937
return limit(moveFile, filePath)
4038
})
4139
await Promise.all(promises)
42-
console.log(`Moved ${files.length} page files`)
40+
console.log(`Moved ${files.length} files`)
4341

4442
// Write the manifest for use in the serverless functions
4543
await writeJson(join(netlifyConfig.build.publish, 'static-manifest.json'), files)

0 commit comments

Comments
 (0)