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

fix: make plugin-check automatically copy current bundle to baselines #257

Merged
merged 2 commits into from
Sep 30, 2022
Merged
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
21 changes: 21 additions & 0 deletions packages/plugin-check/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2022 Climate Interactive / New Venture Fund

import { existsSync } from 'fs'
import { copyFile, mkdir } from 'fs/promises'
import { dirname, join as joinPath, relative } from 'path'
import { fileURLToPath } from 'url'

Expand Down Expand Up @@ -90,6 +92,11 @@ class CheckPlugin implements Plugin {
// which currently isn't made available to the `watch` function
if (this.options?.current === undefined) {
// Path to current bundle was not provided, so generate a default bundle
if (context.config.mode === 'development') {
// Copy the previous bundle to the `baselines` directory so that
// we automatically have it available as a baseline for comparison
await this.copyPreviousBundle(context.config)
}
context.log('info', 'Generating model check bundle...')
await this.genCurrentBundle(context.config, modelSpec)
}
Expand All @@ -116,6 +123,20 @@ class CheckPlugin implements Plugin {
}
}

private async copyPreviousBundle(config: ResolvedConfig): Promise<void> {
// Only copy if the current bundle exists
const currentBundleFile = joinPath(config.prepDir, 'check-bundle.js')
if (existsSync(currentBundleFile)) {
// TODO: Use the baselines directory from the config (not yet available)
const baselinesDir = joinPath(config.rootDir, 'baselines')
if (!existsSync(baselinesDir)) {
await mkdir(baselinesDir, { recursive: true })
}
const previousBundleFile = joinPath(baselinesDir, 'previous.js')
await copyFile(currentBundleFile, previousBundleFile)
}
}

private async genCurrentBundle(config: ResolvedConfig, modelSpec: ModelSpec): Promise<void> {
const prepDir = config.prepDir
const viteConfig = await createViteConfigForBundle(prepDir, modelSpec)
Expand Down