Skip to content

Commit

Permalink
fix(sample): bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Sep 19, 2024
1 parent d4624a3 commit 92e7846
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
18 changes: 13 additions & 5 deletions packages/sample/lib/lib/pdf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export async function pdf(data, config) {
const browser = await instance.launch();
const page = await browser.newPage();

const mock = JSON.stringify({ ...config, data });
const raw = { ...config, data };

await page.setContent(
template.replace('/*inject*/', `window.mock = ${mock}`),
);
await page.exposeFunction('mock', () => raw, raw);

await page.setContent(template);

const options = {
headerTemplate:
Expand All @@ -50,7 +50,15 @@ export async function pdf(data, config) {
footerTemplate: ' ',
};

await page.waitForFunction(() => globalThis.document.title, 50 * 1000);
await page.waitForFunction(
() => {
return globalThis.document.title;
},
null,
{
timeout: 3 * 60 * 1000,
},
);

await page.pdf({
path: join(
Expand Down
4 changes: 3 additions & 1 deletion packages/sample/lib/lib/sample.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export async function action() {
const files = await scan(config);
const code = await picker(files, config);

await pdf(code, config);
const { title, version } = config;

await pdf(code, { title, version });
logger.task('Generated');
}
}
20 changes: 11 additions & 9 deletions packages/sample/lib/lib/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@
</header>
<pre><code></code></pre>
</body>
<script>
/*inject*/

<script defer>
const { body } = document;
const { mock } = globalThis;
window.mock().then((result) => {

const header = body.querySelector('header');

const header = body.querySelector('header');
header.querySelector('h1').textContent = result.title;

header.querySelector('h1').textContent = mock.title;
header.querySelector('h2').textContent = result.version;

header.querySelector('h2').textContent = mock.version;
document.title = [result.title, result.version].join(' ');

document.title = [mock.title, mock.version].join(' ');
body.querySelector('pre code').textContent = result.data;

body.querySelector('pre code').textContent = mock.data;
}).catch(error => {
console.error(error);
});
</script>
</html>
2 changes: 1 addition & 1 deletion packages/sample/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/sample",
"version": "0.4.10",
"version": "0.4.12",
"description": "Generate code sample files",
"license": "MIT",
"author": {
Expand Down

0 comments on commit 92e7846

Please sign in to comment.