Skip to content

Commit

Permalink
WIP cache pnpm-lock.yaml between e2e next poject installs
Browse files Browse the repository at this point in the history
  • Loading branch information
jankaifer committed Dec 16, 2022
1 parent 8cf54a9 commit 4ef969b
Showing 1 changed file with 72 additions and 10 deletions.
82 changes: 72 additions & 10 deletions test/lib/create-next-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ const { randomBytes } = require('crypto')
const { linkPackages } =
require('../../.github/actions/next-stats-action/src/prepare/repo-setup')()

/**
* These are simple dependencies provided by default. We want to optimize this special case.
*/
const areGenericDependencies = (dependencies) =>
Object.keys(dependencies).length === 6 &&
Object.entries(dependencies).every(([dep, version]) => {
if (dep === 'next') return true
return (
[
'react',
'react-dom',
'typescript',
'@types/react',
'@types/node',
].includes(dep) && version === 'latest'
)
})

async function createNextInstall({
parentSpan,
dependencies,
Expand All @@ -29,7 +47,7 @@ async function createNextInstall({
`next-repo-${randomBytes(32).toString('hex')}${dirSuffix}`
)

require('console').log('Creting temporary test directories:')
require('console').log('Using following temporary directories:')
require('console').log(installDir)
require('console').log(tmpRepoDir)

Expand Down Expand Up @@ -141,17 +159,61 @@ async function createNextInstall({
await rootSpan
.traceChild('run generic install command')
.traceAsyncFn(async () => {
await execa(
'pnpm',
['install', '--strict-peer-dependencies=false'],
{
cwd: installDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: process.env,
const runInstall = async () =>
await execa(
'pnpm',
['install', '--strict-peer-dependencies=false'],
{
cwd: installDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: process.env,
}
)

if (!areGenericDependencies(combinedDependencies)) {
await runInstall()
} else {
const cacheDir = path.join(
origRepoDir,
'node_modules',
'.cache',
'tests',
'genericInstall'
)

const cachedFiles = [
// We can't cache node-modules because .pnpm store must be on the same mount - we can't move it between mountpoints
// 'node_modules',
'pnpm-lock.yaml',
]

if (
await fs
.access(cacheDir)
.then(() => true)
.catch(() => false)
) {
require('console').log(
'We are able to prepopulate pnpm install from cache'
)
// There is cache available, let's populate our install dir from that cache
cachedFiles.forEach((file) => {
fs.copy(
path.join(cacheDir, file),
path.join(installDir, file)
)
})
}
)

await runInstall()

// Save current node_modules and pnpm-lock.yaml to a cache
await fs.ensureDir(cacheDir)
cachedFiles.forEach((file) => {
fs.copy(path.join(installDir, file), path.join(cacheDir, file))
})
}
})
await new Promise(() => {})
}

await fs.remove(tmpRepoDir)
Expand Down

0 comments on commit 4ef969b

Please sign in to comment.