diff --git a/.changeset/fix-presets-skip-framework-build.md b/.changeset/fix-presets-skip-framework-build.md new file mode 100644 index 00000000..5d5960f8 --- /dev/null +++ b/.changeset/fix-presets-skip-framework-build.md @@ -0,0 +1,5 @@ +--- +'@aziontech/presets': patch +--- + +Fix the `vue`, `react`, `preact`, `qwik`, `vitepress`, `vuepress` and `angular` presets leaving the storage folder empty (or with the build nested in a subfolder, for `angular`) when building with `--skip-framework-build`. The prebuild now copies the output of the previous framework build (`outDir` from the Vite config, `dist`, `build` for CRA, or the VitePress/VuePress `dist`) into `.edge/assets` instead of only creating the folder. The `angular` preset moves a plain `ng build` output (`dist/` or `dist//browser`) to the `dist` root, like the regular build does. The `vuepress` preset no longer deletes its previous build output in this mode. diff --git a/packages/presets/src/presets/__helpers__/tempProject.ts b/packages/presets/src/presets/__helpers__/tempProject.ts new file mode 100644 index 00000000..40db2ddb --- /dev/null +++ b/packages/presets/src/presets/__helpers__/tempProject.ts @@ -0,0 +1,39 @@ +import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'fs'; +import { tmpdir } from 'os'; +import { dirname, join } from 'path'; + +/** + * Creates a temporary project directory and makes it the current working directory, + * so presets that resolve paths relative to `process.cwd()` run against it. + */ +export function createTempProject() { + const originalCwd = process.cwd(); + const root = mkdtempSync(join(tmpdir(), 'preset-prebuild-')); + process.chdir(root); + + return { + root, + /** Writes files (path -> content) relative to the project root, creating folders as needed. */ + write(files: Record) { + for (const [file, content] of Object.entries(files)) { + const target = join(root, file); + mkdirSync(dirname(target), { recursive: true }); + writeFileSync(target, content); + } + }, + /** Lists, recursively and sorted, the files inside a folder relative to the project root. */ + list(dir: string): string[] { + const walk = (current: string): string[] => + readdirSync(join(root, current), { withFileTypes: true }).flatMap((entry) => + entry.isDirectory() ? walk(join(current, entry.name)) : [join(current, entry.name)], + ); + return walk(dir) + .map((file) => file.slice(dir.length + 1)) + .sort(); + }, + cleanup() { + process.chdir(originalCwd); + rmSync(root, { recursive: true, force: true }); + }, + }; +} diff --git a/packages/presets/src/presets/angular/prebuild.test.ts b/packages/presets/src/presets/angular/prebuild.test.ts new file mode 100644 index 00000000..70ecdde0 --- /dev/null +++ b/packages/presets/src/presets/angular/prebuild.test.ts @@ -0,0 +1,52 @@ +import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; +import type { BuildConfiguration, BuildContext } from '@aziontech/config'; +import { createTempProject } from '../__helpers__/tempProject'; +import prebuild from './prebuild'; + +const skipCtx = { skipFrameworkBuild: true } as BuildContext; +const buildConfig = {} as BuildConfiguration; + +describe('angular prebuild with skipFrameworkBuild', () => { + let project: ReturnType; + + beforeEach(() => { + project = createTempProject(); + }); + + afterEach(() => { + project.cleanup(); + }); + + it('keeps a build that is already at the dist root', async () => { + project.write({ 'dist/index.html': '', 'dist/main.js': 'console.log(1)' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('dist')).toEqual(['index.html', 'main.js']); + }); + + it('moves dist//browser to the dist root (application builder)', async () => { + project.write({ + 'dist/my-app/browser/index.html': '', + 'dist/my-app/browser/chunk.js': 'console.log(1)', + }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('dist')).toEqual(['chunk.js', 'index.html']); + }); + + it('moves dist/ to the dist root (browser builder)', async () => { + project.write({ 'dist/my-app/index.html': '', 'dist/my-app/main.js': 'console.log(1)' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('dist')).toEqual(['index.html', 'main.js']); + }); + + it('creates an empty dist when there is no previous build', async () => { + await prebuild(buildConfig, skipCtx); + + expect(project.list('dist')).toEqual([]); + }); +}); diff --git a/packages/presets/src/presets/angular/prebuild.ts b/packages/presets/src/presets/angular/prebuild.ts index 6b2584b7..0b825c0c 100644 --- a/packages/presets/src/presets/angular/prebuild.ts +++ b/packages/presets/src/presets/angular/prebuild.ts @@ -19,15 +19,67 @@ async function moveBrowserFiles(browserFolder: string, storagePath: string): Pro } } +/** + * Find where a previous `ng build` left its output inside the dist folder. + * A plain `ng build` writes to `dist/` (browser builder) or `dist//browser` + * (application builder), while the storage dir expects the files directly in `dist`. + */ +async function findPreviousBuildOutput(outputDir: string): Promise { + const root = path.join(process.cwd(), outputDir); + const projects = await fs.readdir(root, { withFileTypes: true }); + + for (const project of projects.filter((entry) => entry.isDirectory())) { + for (const candidate of [path.join(root, project.name, 'browser'), path.join(root, project.name)]) { + try { + await fs.access(path.join(candidate, 'index.html')); + return candidate; + } catch { + // index.html not found here, try the next candidate + } + } + } + + return null; +} + +/** + * Make sure a previous `ng build` output is at the storage root (dist), like the regular build flow does. + */ +async function prepareSkippedBuild(outputDir: string): Promise { + const root = path.join(process.cwd(), outputDir); + await fs.mkdir(root, { recursive: true }); + + // Already at the storage root (e.g. built with --output-path=./dist) + try { + await fs.access(path.join(root, 'index.html')); + return; + } catch { + // not at the root, look for it inside dist/ + } + + const source = await findPreviousBuildOutput(outputDir); + if (!source) { + return; + } + + await moveBrowserFiles(source, root); + + // remove the project folder left behind when it only contained the browser output + const projectFolder = path.dirname(source); + if (projectFolder !== root) { + await fs.rmdir(projectFolder).catch(() => undefined); + } +} + /** * Runs custom prebuild actions for Angular */ async function prebuild(_: BuildConfiguration, ctx: BuildContext): Promise { try { const outputDir = './dist'; - // if skipFrameworkBuild is true, we need to create the dist folder + // If skipFrameworkBuild is true, reuse the output of a previous framework build if (ctx.skipFrameworkBuild) { - await fs.mkdir(outputDir, { recursive: true }); + await prepareSkippedBuild(outputDir); return; } const packageManager: PackageManagerType = await getPackageManager(); diff --git a/packages/presets/src/presets/preact/prebuild.test.ts b/packages/presets/src/presets/preact/prebuild.test.ts new file mode 100644 index 00000000..36fe196b --- /dev/null +++ b/packages/presets/src/presets/preact/prebuild.test.ts @@ -0,0 +1,58 @@ +import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; +import type { BuildConfiguration, BuildContext } from '@aziontech/config'; +import { createTempProject } from '../__helpers__/tempProject'; +import prebuild from './prebuild'; + +const skipCtx = { skipFrameworkBuild: true } as BuildContext; +const buildConfig = {} as BuildConfiguration; + +describe('preact prebuild with skipFrameworkBuild', () => { + let project: ReturnType; + + beforeEach(() => { + project = createTempProject(); + }); + + afterEach(() => { + project.cleanup(); + }); + + it('copies the dist folder to .edge/assets', async () => { + project.write({ + 'vite.config.ts': `import { defineConfig } from 'vite';\nexport default defineConfig({ plugins: [] });\n`, + 'dist/index.html': '', + }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('copies the outDir declared in vite.config', async () => { + project.write({ + 'vite.config.ts': `import { defineConfig } from 'vite';\nexport default defineConfig({\n build: { outDir: 'out' },\n});\n`, + 'out/index.html': '', + }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('keeps the content already in .edge/assets when the outDir is gone', async () => { + project.write({ + 'vite.config.ts': `import { defineConfig } from 'vite';\nexport default defineConfig({ plugins: [] });\n`, + '.edge/assets/index.html': '', + }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('creates an empty .edge/assets when there is no previous build', async () => { + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual([]); + }); +}); diff --git a/packages/presets/src/presets/preact/prebuild.ts b/packages/presets/src/presets/preact/prebuild.ts index 0df9e0e1..8e3b079b 100644 --- a/packages/presets/src/presets/preact/prebuild.ts +++ b/packages/presets/src/presets/preact/prebuild.ts @@ -25,6 +25,18 @@ async function viteConfigExists() { return results.includes(true); } +/** + * Check if a path exists + */ +async function pathExists(path: string): Promise { + try { + await lstat(path); + return true; + } catch { + return false; + } +} + /** * Read vite.config build output * @returns {object} The parsed configuration object or null if the file doesn't exist. @@ -64,9 +76,17 @@ async function prebuild(_: BuildConfiguration, ctx: BuildContext) { const packageManager = await getPackageManager(); const npmArgsForward = packageManager === 'npm' ? '--' : ''; - // if skipFrameworkBuild is true, we need to create the dist folder + // If skipFrameworkBuild is true, reuse the output of a previous framework build. + // If the outDir is gone (a regular build removes it after copying), keep what is already in the storage dir. if (ctx.skipFrameworkBuild) { await mkdir(edgeStorageDir, { recursive: true }); + if (await viteConfigExists()) { + const config = await readViteBuildOutput(); + const outDir = config?.build?.outDir || defaultViteOutDir; + if (await pathExists(outDir)) { + copyDirectory(outDir, edgeStorageDir); + } + } return; } diff --git a/packages/presets/src/presets/qwik/prebuild.test.ts b/packages/presets/src/presets/qwik/prebuild.test.ts new file mode 100644 index 00000000..64fe799a --- /dev/null +++ b/packages/presets/src/presets/qwik/prebuild.test.ts @@ -0,0 +1,33 @@ +import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; +import type { BuildConfiguration, BuildContext } from '@aziontech/config'; +import { createTempProject } from '../__helpers__/tempProject'; +import prebuild from './prebuild'; + +const skipCtx = { skipFrameworkBuild: true } as BuildContext; +const buildConfig = {} as BuildConfiguration; + +describe('qwik prebuild with skipFrameworkBuild', () => { + let project: ReturnType; + + beforeEach(() => { + project = createTempProject(); + }); + + afterEach(() => { + project.cleanup(); + }); + + it('copies the dist folder to .edge/assets', async () => { + project.write({ 'dist/index.html': '', 'dist/build/q-abc.js': 'console.log(1)' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['build/q-abc.js', 'index.html']); + }); + + it('creates an empty .edge/assets when there is no previous build', async () => { + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual([]); + }); +}); diff --git a/packages/presets/src/presets/qwik/prebuild.ts b/packages/presets/src/presets/qwik/prebuild.ts index 28832c94..3efbd02d 100644 --- a/packages/presets/src/presets/qwik/prebuild.ts +++ b/packages/presets/src/presets/qwik/prebuild.ts @@ -1,31 +1,54 @@ import { BuildConfiguration, BuildContext } from '@aziontech/config'; import { copyDirectory, exec, getPackageManager } from '@aziontech/utils/node'; -import { mkdir, readFile } from 'fs/promises'; +import { lstat, mkdir, readFile } from 'fs/promises'; /** - * Runs custom prebuild actions for Qwik + * Check if a path exists */ -async function prebuild(_: BuildConfiguration, ctx: BuildContext): Promise { - const packageManager = await getPackageManager(); - const newOutDir = '.edge/assets'; - let outDir = 'dist'; - - // if skipFrameworkBuild is true, we need to create the dist folder - if (ctx.skipFrameworkBuild) { - await mkdir(newOutDir, { recursive: true }); - return; +async function pathExists(path: string): Promise { + try { + await lstat(path); + return true; + } catch { + return false; } +} - // Check if an output path is specified in config file +/** + * Read the outDir from the static adapter config, if specified + */ +async function readOutDir(defaultOutDir: string): Promise { try { const configFileContent = await readFile('./adapters/static/vite.config.ts', 'utf-8'); const attributeMatch = Array.from(configFileContent.matchAll(/outDir:(.*)\n/g), (match) => match)[0]; if (attributeMatch) { - outDir = attributeMatch[1].trim(); + return attributeMatch[1].trim(); } } catch { // Config file doesn't exist or doesn't have outDir specified } + return defaultOutDir; +} + +/** + * Runs custom prebuild actions for Qwik + */ +async function prebuild(_: BuildConfiguration, ctx: BuildContext): Promise { + const packageManager = await getPackageManager(); + const newOutDir = '.edge/assets'; + const defaultOutDir = 'dist'; + + // If skipFrameworkBuild is true, reuse the output of a previous framework build + if (ctx.skipFrameworkBuild) { + await mkdir(newOutDir, { recursive: true }); + const outDir = await readOutDir(defaultOutDir); + if (await pathExists(outDir)) { + copyDirectory(outDir, newOutDir); + } + return; + } + + const outDir = await readOutDir(defaultOutDir); await exec(`${packageManager} run build`, { scope: 'Qwik', diff --git a/packages/presets/src/presets/react/prebuild.test.ts b/packages/presets/src/presets/react/prebuild.test.ts new file mode 100644 index 00000000..3575d6d8 --- /dev/null +++ b/packages/presets/src/presets/react/prebuild.test.ts @@ -0,0 +1,55 @@ +import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; +import type { BuildConfiguration, BuildContext } from '@aziontech/config'; +import { createTempProject } from '../__helpers__/tempProject'; +import prebuild from './prebuild'; + +const skipCtx = { skipFrameworkBuild: true } as BuildContext; +const buildConfig = {} as BuildConfiguration; + +describe('react prebuild with skipFrameworkBuild', () => { + let project: ReturnType; + + beforeEach(() => { + project = createTempProject(); + }); + + afterEach(() => { + project.cleanup(); + }); + + it('copies the dist folder of a Vite project to .edge/assets', async () => { + project.write({ + 'vite.config.ts': `import { defineConfig } from 'vite';\nexport default defineConfig({ plugins: [] });\n`, + 'dist/index.html': '', + }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('copies the outDir declared in vite.config', async () => { + project.write({ + 'vite.config.ts': `import { defineConfig } from 'vite';\nexport default defineConfig({\n build: { outDir: 'out' },\n});\n`, + 'out/index.html': '', + }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('copies the build folder of a CRA project to .edge/assets', async () => { + project.write({ 'build/index.html': '', 'build/static/js/main.js': 'console.log(1)' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html', 'static/js/main.js']); + }); + + it('creates an empty .edge/assets when there is no previous build', async () => { + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual([]); + }); +}); diff --git a/packages/presets/src/presets/react/prebuild.ts b/packages/presets/src/presets/react/prebuild.ts index 76f4664d..35db5b87 100644 --- a/packages/presets/src/presets/react/prebuild.ts +++ b/packages/presets/src/presets/react/prebuild.ts @@ -21,6 +21,18 @@ async function viteConfigExists(): Promise { return results.includes(true); } +/** + * Check if a path exists + */ +async function pathExists(path: string): Promise { + try { + await lstat(path); + return true; + } catch { + return false; + } +} + /** * Read vite.config build output */ @@ -52,16 +64,27 @@ async function readViteBuildOutput() { * Runs custom prebuild actions for React */ async function prebuild(_: BuildConfiguration, ctx: BuildContext): Promise { - // if skipFrameworkBuild is true, we need to create the dist folder + const defaultViteOutDir = 'dist'; + const defaultCraOutDir = 'build'; + const destPath = '.edge/assets'; + + // If skipFrameworkBuild is true, reuse the output of a previous framework build. + // Vite outputs to `outDir` (default `dist`) and CRA to `build` when run without BUILD_PATH. if (ctx.skipFrameworkBuild) { - await mkdir('.edge/assets', { recursive: true }); + await mkdir(destPath, { recursive: true }); + let outDir = defaultCraOutDir; + if (await viteConfigExists()) { + const config = await readViteBuildOutput(); + outDir = config?.build?.outDir || defaultViteOutDir; + } + if (await pathExists(outDir)) { + copyDirectory(outDir, destPath); + } return; } const packageManager = await getPackageManager(); const npmArgsForward = packageManager === 'npm' ? '--' : ''; - const defaultViteOutDir = 'dist'; - const destPath = '.edge/assets'; const isViteProject = await viteConfigExists(); diff --git a/packages/presets/src/presets/vitepress/prebuild.test.ts b/packages/presets/src/presets/vitepress/prebuild.test.ts new file mode 100644 index 00000000..a5a2f4c8 --- /dev/null +++ b/packages/presets/src/presets/vitepress/prebuild.test.ts @@ -0,0 +1,41 @@ +import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; +import type { BuildConfiguration, BuildContext } from '@aziontech/config'; +import { createTempProject } from '../__helpers__/tempProject'; +import prebuild from './prebuild'; + +const skipCtx = { skipFrameworkBuild: true } as BuildContext; +const buildConfig = {} as BuildConfiguration; + +describe('vitepress prebuild with skipFrameworkBuild', () => { + let project: ReturnType; + + beforeEach(() => { + project = createTempProject(); + }); + + afterEach(() => { + project.cleanup(); + }); + + it('copies the root .vitepress/dist folder to .edge/assets', async () => { + project.write({ '.vitepress/dist/index.html': '', '.vitepress/dist/assets/app.js': 'console.log(1)' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['assets/app.js', 'index.html']); + }); + + it('copies docs/.vitepress/dist when the project uses a docs folder', async () => { + project.write({ 'docs/.vitepress/dist/index.html': '' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('creates an empty .edge/assets when there is no previous build', async () => { + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual([]); + }); +}); diff --git a/packages/presets/src/presets/vitepress/prebuild.ts b/packages/presets/src/presets/vitepress/prebuild.ts index fefcfd01..1b263f64 100644 --- a/packages/presets/src/presets/vitepress/prebuild.ts +++ b/packages/presets/src/presets/vitepress/prebuild.ts @@ -14,6 +14,18 @@ async function docsFolderExists(): Promise { } } +/** + * Check if a path exists + */ +async function pathExists(path: string): Promise { + try { + await lstat(path); + return true; + } catch { + return false; + } +} + /** * Runs custom prebuild actions for VitePress */ @@ -25,9 +37,12 @@ async function prebuild(_: BuildConfiguration, ctx: BuildContext): Promise // however the users also might use the root folder const outDir = (await docsFolderExists()) ? 'docs/.vitepress/dist' : '.vitepress/dist'; - // If skipFrameworkBuild is true, we need to create the dist folder + // If skipFrameworkBuild is true, reuse the output of a previous framework build if (ctx.skipFrameworkBuild) { await mkdir(newOutDir, { recursive: true }); + if (await pathExists(outDir)) { + copyDirectory(outDir, newOutDir); + } return; } diff --git a/packages/presets/src/presets/vue/prebuild.test.ts b/packages/presets/src/presets/vue/prebuild.test.ts new file mode 100644 index 00000000..364b0b87 --- /dev/null +++ b/packages/presets/src/presets/vue/prebuild.test.ts @@ -0,0 +1,44 @@ +import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; +import type { BuildConfiguration, BuildContext } from '@aziontech/config'; +import { createTempProject } from '../__helpers__/tempProject'; +import prebuild from './prebuild'; + +const skipCtx = { skipFrameworkBuild: true } as BuildContext; +const buildConfig = {} as BuildConfiguration; + +describe('vue prebuild with skipFrameworkBuild', () => { + let project: ReturnType; + + beforeEach(() => { + project = createTempProject(); + }); + + afterEach(() => { + project.cleanup(); + }); + + it('copies the existing dist folder to .edge/assets', async () => { + project.write({ 'dist/index.html': '', 'dist/assets/app.js': 'console.log(1)' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['assets/app.js', 'index.html']); + }); + + it('copies the outDir declared in vite.config', async () => { + project.write({ + 'vite.config.ts': `import { defineConfig } from 'vite';\nexport default defineConfig({\n build: { outDir: 'out' },\n});\n`, + 'out/index.html': '', + }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('creates an empty .edge/assets when there is no previous build', async () => { + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual([]); + }); +}); diff --git a/packages/presets/src/presets/vue/prebuild.ts b/packages/presets/src/presets/vue/prebuild.ts index d5e1e3c9..989ff016 100644 --- a/packages/presets/src/presets/vue/prebuild.ts +++ b/packages/presets/src/presets/vue/prebuild.ts @@ -21,6 +21,18 @@ async function viteConfigExists(): Promise { return results.includes(true); } +/** + * Check if a path exists + */ +async function pathExists(path: string): Promise { + try { + await lstat(path); + return true; + } catch { + return false; + } +} + /** * Read vite.config build output */ @@ -57,9 +69,16 @@ async function prebuild(_: BuildConfiguration, ctx: BuildContext): Promise const defaultViteOutDir = 'dist'; const destPath = '.edge/assets'; - // If skipFrameworkBuild is true, we need to create the dist folder + // If skipFrameworkBuild is true, reuse the output of a previous framework build + // (vite outDir, or the default `dist` for Vue CLI) and copy it to destPath if (ctx.skipFrameworkBuild) { + const config = (await viteConfigExists()) ? await readViteBuildOutput() : null; + const outDir = config?.build?.outDir || defaultViteOutDir; + await mkdir(destPath, { recursive: true }); + if (await pathExists(outDir)) { + copyDirectory(outDir, destPath); + } return; } diff --git a/packages/presets/src/presets/vuepress/prebuild.test.ts b/packages/presets/src/presets/vuepress/prebuild.test.ts new file mode 100644 index 00000000..9befa06f --- /dev/null +++ b/packages/presets/src/presets/vuepress/prebuild.test.ts @@ -0,0 +1,49 @@ +import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; +import type { BuildConfiguration, BuildContext } from '@aziontech/config'; +import { createTempProject } from '../__helpers__/tempProject'; +import prebuild from './prebuild'; + +const skipCtx = { skipFrameworkBuild: true } as BuildContext; +const buildConfig = {} as BuildConfiguration; + +describe('vuepress prebuild with skipFrameworkBuild', () => { + let project: ReturnType; + + beforeEach(() => { + project = createTempProject(); + }); + + afterEach(() => { + project.cleanup(); + }); + + it('copies the root .vuepress/dist folder to .edge/assets', async () => { + project.write({ '.vuepress/dist/index.html': '', '.vuepress/dist/assets/app.js': 'console.log(1)' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['assets/app.js', 'index.html']); + }); + + it('copies docs/.vuepress/dist when the project uses a docs folder', async () => { + project.write({ 'docs/.vuepress/dist/index.html': '' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual(['index.html']); + }); + + it('creates an empty .edge/assets when there is no previous build', async () => { + await prebuild(buildConfig, skipCtx); + + expect(project.list('.edge/assets')).toEqual([]); + }); + + it('does not delete the previous build output', async () => { + project.write({ '.vuepress/dist/index.html': '' }); + + await prebuild(buildConfig, skipCtx); + + expect(project.list('.vuepress/dist')).toEqual(['index.html']); + }); +}); diff --git a/packages/presets/src/presets/vuepress/prebuild.ts b/packages/presets/src/presets/vuepress/prebuild.ts index 770cd7ac..8092ca8f 100644 --- a/packages/presets/src/presets/vuepress/prebuild.ts +++ b/packages/presets/src/presets/vuepress/prebuild.ts @@ -1,6 +1,6 @@ import type { BuildConfiguration, BuildContext } from '@aziontech/config'; import { copyDirectory, exec, getPackageManager } from '@aziontech/utils/node'; -import { lstat, rm } from 'fs/promises'; +import { lstat, mkdir } from 'fs/promises'; /** * Check if the project uses the "/docs" @@ -14,6 +14,18 @@ async function docsFolderExists(): Promise { } } +/** + * Check if a path exists + */ +async function pathExists(path: string): Promise { + try { + await lstat(path); + return true; + } catch { + return false; + } +} + /** * Runs custom prebuild actions for VitePress */ @@ -25,9 +37,12 @@ async function prebuild(_: BuildConfiguration, ctx: BuildContext): Promise // however the users also might use the root folder const outDir = (await docsFolderExists()) ? 'docs/.vuepress/dist' : '.vuepress/dist'; - // If skipFrameworkBuild is true, we need to remove the dist folder + // If skipFrameworkBuild is true, reuse the output of a previous framework build if (ctx.skipFrameworkBuild) { - await rm(outDir, { recursive: true, force: true }); + await mkdir(newOutDir, { recursive: true }); + if (await pathExists(outDir)) { + copyDirectory(outDir, newOutDir); + } return; }