@mintlify/previewing 4.0.1665 → 4.0.1667
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/export-archive.test.d.ts +1 -0
- package/dist/__tests__/export-archive.test.js +53 -0
- package/dist/local-preview/export-archive.d.ts +9 -0
- package/dist/local-preview/export-archive.js +51 -0
- package/dist/local-preview/export.js +12 -5
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -7
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import AdmZip from 'adm-zip';
|
|
2
|
+
import fse from 'fs-extra';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { writeZipArchive } from '../local-preview/export-archive.js';
|
|
6
|
+
describe('writeZipArchive', () => {
|
|
7
|
+
let workDir;
|
|
8
|
+
let sourceDir;
|
|
9
|
+
let outputPath;
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
workDir = await fse.mkdtemp(path.join(os.tmpdir(), 'export-archive-test-'));
|
|
12
|
+
sourceDir = path.join(workDir, 'site');
|
|
13
|
+
outputPath = path.join(workDir, 'export.zip');
|
|
14
|
+
await fse.ensureDir(sourceDir);
|
|
15
|
+
});
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
await fse.remove(workDir);
|
|
18
|
+
});
|
|
19
|
+
it('archives nested files with forward-slash entry names', async () => {
|
|
20
|
+
await fse.outputFile(path.join(sourceDir, 'index.html'), '<h1>Home</h1>');
|
|
21
|
+
await fse.outputFile(path.join(sourceDir, 'ja', 'soc', 'page', 'index.html'), '<h1>ページ</h1>');
|
|
22
|
+
await fse.outputFile(path.join(sourceDir, '_next', 'static', 'app.js'), 'console.log(1);');
|
|
23
|
+
await writeZipArchive(sourceDir, outputPath);
|
|
24
|
+
const zip = new AdmZip(outputPath);
|
|
25
|
+
const names = zip
|
|
26
|
+
.getEntries()
|
|
27
|
+
.map((entry) => entry.entryName)
|
|
28
|
+
.sort();
|
|
29
|
+
expect(names).toEqual(['_next/static/app.js', 'index.html', 'ja/soc/page/index.html']);
|
|
30
|
+
expect(zip.readAsText('ja/soc/page/index.html')).toBe('<h1>ページ</h1>');
|
|
31
|
+
expect(await fse.pathExists(`${outputPath}.partial`)).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
it.skipIf(process.platform === 'win32')('preserves the executable bit', async () => {
|
|
34
|
+
const script = path.join(sourceDir, 'Start Docs.command');
|
|
35
|
+
await fse.outputFile(script, '#!/bin/sh\n');
|
|
36
|
+
await fse.chmod(script, 0o755);
|
|
37
|
+
await writeZipArchive(sourceDir, outputPath);
|
|
38
|
+
const [entry] = new AdmZip(outputPath).getEntries();
|
|
39
|
+
expect((entry.header.attr >>> 16) & 0o777).toBe(0o755);
|
|
40
|
+
});
|
|
41
|
+
it('creates missing parent directories for the output path', async () => {
|
|
42
|
+
await fse.outputFile(path.join(sourceDir, 'index.html'), '<h1>Home</h1>');
|
|
43
|
+
const nestedOutput = path.join(workDir, 'dist', 'nested', 'docs.zip');
|
|
44
|
+
await writeZipArchive(sourceDir, nestedOutput);
|
|
45
|
+
expect(new AdmZip(nestedOutput).readAsText('index.html')).toBe('<h1>Home</h1>');
|
|
46
|
+
});
|
|
47
|
+
it('leaves an existing archive untouched when archiving fails', async () => {
|
|
48
|
+
await fse.outputFile(outputPath, 'previous export');
|
|
49
|
+
await expect(writeZipArchive(path.join(workDir, 'missing'), outputPath)).rejects.toThrow();
|
|
50
|
+
expect(await fse.readFile(outputPath, 'utf8')).toBe('previous export');
|
|
51
|
+
expect(await fse.pathExists(`${outputPath}.partial`)).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streams every file under `sourceDir` into a zip at `outputPath`.
|
|
3
|
+
*
|
|
4
|
+
* The archive is written to disk as it is built, so its size isn't bounded by memory or by
|
|
5
|
+
* Node's 2 GiB Buffer limit, and yazl switches to ZIP64 once the archive needs it. Output goes
|
|
6
|
+
* to a `.partial` file that is renamed into place on success, so a failed run never leaves a
|
|
7
|
+
* truncated archive at `outputPath` or clobbers an existing one.
|
|
8
|
+
*/
|
|
9
|
+
export declare function writeZipArchive(sourceDir: string, outputPath: string): Promise<void>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import fse from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { pipeline } from 'stream/promises';
|
|
4
|
+
import yazl from 'yazl';
|
|
5
|
+
async function collectEntries(sourceDir) {
|
|
6
|
+
const entries = [];
|
|
7
|
+
async function walk(dir) {
|
|
8
|
+
const dirents = await fse.readdir(dir, { withFileTypes: true });
|
|
9
|
+
for (const dirent of dirents) {
|
|
10
|
+
const filePath = path.join(dir, dirent.name);
|
|
11
|
+
if (dirent.isDirectory()) {
|
|
12
|
+
await walk(filePath);
|
|
13
|
+
}
|
|
14
|
+
else if (dirent.isFile()) {
|
|
15
|
+
const { mode, mtime } = await fse.stat(filePath);
|
|
16
|
+
const entryName = path.relative(sourceDir, filePath).split(path.sep).join('/');
|
|
17
|
+
entries.push({ filePath, entryName, mode, mtime });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
await walk(sourceDir);
|
|
22
|
+
return entries.sort((a, b) => a.entryName.localeCompare(b.entryName));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Streams every file under `sourceDir` into a zip at `outputPath`.
|
|
26
|
+
*
|
|
27
|
+
* The archive is written to disk as it is built, so its size isn't bounded by memory or by
|
|
28
|
+
* Node's 2 GiB Buffer limit, and yazl switches to ZIP64 once the archive needs it. Output goes
|
|
29
|
+
* to a `.partial` file that is renamed into place on success, so a failed run never leaves a
|
|
30
|
+
* truncated archive at `outputPath` or clobbers an existing one.
|
|
31
|
+
*/
|
|
32
|
+
export async function writeZipArchive(sourceDir, outputPath) {
|
|
33
|
+
const entries = await collectEntries(sourceDir);
|
|
34
|
+
const partialPath = `${outputPath}.partial`;
|
|
35
|
+
await fse.ensureDir(path.dirname(outputPath));
|
|
36
|
+
const zip = new yazl.ZipFile();
|
|
37
|
+
const zipError = new Promise((_, reject) => zip.once('error', reject));
|
|
38
|
+
const written = pipeline(zip.outputStream, fse.createWriteStream(partialPath));
|
|
39
|
+
try {
|
|
40
|
+
for (const { filePath, entryName, mode, mtime } of entries) {
|
|
41
|
+
zip.addFile(filePath, entryName, { mode, mtime });
|
|
42
|
+
}
|
|
43
|
+
zip.end();
|
|
44
|
+
await Promise.race([written, zipError]);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
await fse.remove(partialPath);
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
await fse.move(partialPath, outputPath, { overwrite: true });
|
|
51
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { getAllPathsInDecoratedNav, getAllPathsInDocsNav } from '@mintlify/common';
|
|
3
3
|
import { prebuild } from '@mintlify/prebuild';
|
|
4
|
-
import AdmZip from 'adm-zip';
|
|
5
4
|
import fse, { pathExists } from 'fs-extra';
|
|
6
5
|
import os from 'os';
|
|
7
6
|
import path from 'path';
|
|
@@ -9,6 +8,7 @@ import { fileURLToPath } from 'url';
|
|
|
9
8
|
import { CMD_EXEC_PATH, CONTENT_PATH, VERSION_PATH, NEXT_PUBLIC_PATH, NEXT_PROPS_PATH, PREVIEW_PATH, configureSharedPreviewPath, } from '../constants.js';
|
|
10
9
|
import { addLog, clearLogs } from '../logging-state.js';
|
|
11
10
|
import { ErrorLog, SpinnerLog, SuccessLog, WarningLog } from '../logs.js';
|
|
11
|
+
import { writeZipArchive } from './export-archive.js';
|
|
12
12
|
import { localizeAssets } from './export-assets.js';
|
|
13
13
|
import { findBasePathConflict, normalizeExportBasePath } from './export-base-path.js';
|
|
14
14
|
import { generateExportLlmsTxt } from './export-llms.js';
|
|
@@ -126,6 +126,7 @@ async function generateExportArchive(routes, outputPath, groups, basePath, vite)
|
|
|
126
126
|
const { routes: preparedRoutes, rootRoute } = prepareRoutesForExport(routes, groups);
|
|
127
127
|
const routesToGenerate = withIndexRouteAliases(preparedRoutes);
|
|
128
128
|
let tmpDir;
|
|
129
|
+
let keepExportDir = false;
|
|
129
130
|
try {
|
|
130
131
|
clearLogs();
|
|
131
132
|
addLog(_jsx(SpinnerLog, { message: `generating ${routesToGenerate.length} pages...` }));
|
|
@@ -153,9 +154,15 @@ async function generateExportArchive(routes, outputPath, groups, basePath, vite)
|
|
|
153
154
|
});
|
|
154
155
|
const failed = [...clientAssetFailures, ...assetFailures];
|
|
155
156
|
addLog(_jsx(SpinnerLog, { message: "creating zip archive..." }));
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
157
|
+
try {
|
|
158
|
+
await writeZipArchive(exportDir, outputPath);
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
// Rendering a large site can take an hour, so keep the output rather than discard it.
|
|
162
|
+
keepExportDir = true;
|
|
163
|
+
const reason = err instanceof Error ? err.message : 'unknown error';
|
|
164
|
+
throw new Error(`failed to create zip archive: ${reason}. The rendered site was kept at ${exportDir}`);
|
|
165
|
+
}
|
|
159
166
|
clearLogs();
|
|
160
167
|
for (const url of failed) {
|
|
161
168
|
addLog(_jsx(WarningLog, { message: `could not bundle ${url}` }));
|
|
@@ -163,7 +170,7 @@ async function generateExportArchive(routes, outputPath, groups, basePath, vite)
|
|
|
163
170
|
addLog(_jsx(SuccessLog, { message: `exported to ${outputPath}` }));
|
|
164
171
|
}
|
|
165
172
|
finally {
|
|
166
|
-
if (tmpDir) {
|
|
173
|
+
if (tmpDir && !keepExportDir) {
|
|
167
174
|
await fse.remove(tmpDir);
|
|
168
175
|
}
|
|
169
176
|
await renderer.close();
|