Skip to content

Commit

Permalink
create-spectacle CLI: Fix generated spectacle version, add custom t…
Browse files Browse the repository at this point in the history
…ypes for MD build. (#1313)

* Fix generated Spectacle version in CLI, add custom types to MD output.

* Changeset

* Use require() for JSON import

* Update cli.test.ts
  • Loading branch information
carloskelly13 authored Dec 29, 2023
1 parent 2e95eca commit d2621a7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
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();

1 comment on commit d2621a7

@vercel
Copy link

@vercel vercel bot commented on d2621a7 Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.