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

create-spectacle CLI: Fix generated spectacle version, add custom types for MD build. #1313

Merged
merged 4 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/perfect-pumpkins-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-spectacle': patch
---

Fix generated Spectacle version in CLI, add custom types to MD output.
1 change: 1 addition & 0 deletions packages/create-spectacle/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('create-spectacle', () => {
'.babelrc',
'.gitignore',
'README.md',
'custom.d.ts',
'index.html',
'index.tsx',
'package.json',
Expand Down
5 changes: 2 additions & 3 deletions packages/create-spectacle/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import {
writeOnePageHTMLFile,
writeViteProjectFiles
} from './templates/file-writers';
// @ts-ignore
import { devDependencies } from '../package.json';

const spectaclePackage = require(`${__dirname}/../spectacle-package.json`);
const argv = yargs(hideBin(process.argv)).argv;
const cwd = process.cwd();

Expand Down Expand Up @@ -176,7 +175,7 @@ const main = async () => {
lang,
port,
isVite: /vite$/.test(type),
spectacleVersion: devDependencies.spectacle
spectacleVersion: spectaclePackage.version
};

switch (type) {
Expand Down
7 changes: 5 additions & 2 deletions packages/create-spectacle/src/templates/file-writers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { gitignoreTemplate } from './gitignore';
import { readmeTemplate } from './readme';
import { viteConfigTemplate } from './viteConfig';
import { createOnePage } from '../generators/one-page';
import { markdownTemplate } from './markdown';
import { markdownCustomTypesTemplate, markdownTemplate } from './markdown';

export type FileOptions = {
snakeCaseName: string;
Expand Down Expand Up @@ -52,7 +52,10 @@ const prepForProjectWrite = async (fileOptions: FileOptions) => {
await writeFile(pathFor('README.md'), readmeTemplate({ name, isVite }));
await writeFile(pathFor('tsconfig.json'), tsconfigTemplate());
fileOptions.useMarkdownSlides &&
(await writeFile(pathFor('slides.md'), markdownTemplate({ name })));
(await Promise.all([
writeFile(pathFor('custom.d.ts'), markdownCustomTypesTemplate()),
writeFile(pathFor('slides.md'), markdownTemplate({ name }))
]));

return { outPath, pathFor };
};
Expand Down
66 changes: 48 additions & 18 deletions packages/create-spectacle/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ type IndexTemplateOptions = {
usesMarkdown: boolean;
};

const tsxImports = `
import { Deck, DefaultTemplate, Slide, FlexBox, Heading, SpectacleLogo } from 'spectacle';
`;
const content = {
reactImports() {
return `
import React from 'react';
import { createRoot } from 'react-dom/client';
import { Deck, DefaultTemplate, Slide, FlexBox, Heading, SpectacleLogo } from 'spectacle'
`;
},

const mdImports = `
mdImports() {
return `
import React from 'react';
import { createRoot } from 'react-dom/client';
import { Deck, DefaultTemplate, MarkdownSlideSet } from 'spectacle';
import mdContent from './slides.md';
`;

export const indexTemplate = (options: IndexTemplateOptions) =>
`import React from 'react';
import { createRoot } from 'react-dom/client';
${(options.usesMarkdown ? mdImports : tsxImports).trim()}
`;
},

const Presentation = () => (
<Deck template={() => <DefaultTemplate />}>
${(options.usesMarkdown
? `<MarkdownSlideSet>{mdContent}</MarkdownSlideSet>`
: `
reactBody(name: string) {
return `
<Slide>
<FlexBox height="100%">
<Heading>${options.name}</Heading>
<Heading>${name}</Heading>
</FlexBox>
</Slide>
<Slide>
Expand All @@ -34,9 +35,38 @@ const Presentation = () => (
</FlexBox>
</Slide>
`
).trim()}
.substring(1)
.trim();
},

mdBody() {
return `
<MarkdownSlideSet>{mdContent}</MarkdownSlideSet>
`
.substring(1)
.trim();
}
};

export const indexTemplate = (options: IndexTemplateOptions) =>
`
${(() => {
if (options.usesMarkdown) {
return content.mdImports();
}
return content.reactImports();
})().trim()}

const Presentation = () => (
<Deck template={() => <DefaultTemplate />}>
${(() => {
if (options.usesMarkdown) {
return content.mdBody();
}
return content.reactBody(options.name);
})()}
</Deck>
);

createRoot(document.getElementById('app')!).render(<Presentation />);
`;
`.trim();
8 changes: 8 additions & 0 deletions packages/create-spectacle/src/templates/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ export const markdownTemplate = (options: TemplateOptions) =>
---
- Made with Spectacle
`.trim();

export const markdownCustomTypesTemplate = () =>
`
declare module '*.md' {
const content: string;
export default content;
}
`.trim();
Loading