-
Notifications
You must be signed in to change notification settings - Fork 864
feat(react-email): installation of UI without npm install
#2573
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
Open
gabrielmfern
wants to merge
25
commits into
canary
Choose a base branch
from
feat/new-preview-server-strategy
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
cbf4f3b
feat(demo): use Tailwind v4 on all templates (#2487)
bukinoshita 59c58eb
fix: lockfile
gabrielmfern 4197fff
move preview-server to react-email/ui
gabrielmfern 8cf8dac
Revert "move preview-server to react-email/ui"
gabrielmfern 519c525
add code to download the preview server from npm, and install next to it
gabrielmfern 001a9f9
add spinners to preview server installation
gabrielmfern fbef658
add directory location to UI installation success message
gabrielmfern e73e883
add error message when emails directory doesn't exist in build
gabrielmfern 31cafff
simplify
gabrielmfern 7fdd5b5
update `email build` to work with new strategy
gabrielmfern 68af13c
remove dependency on nypm
gabrielmfern 221ea23
lint
gabrielmfern b191c86
update snaps, and make test more reliable
gabrielmfern 3cb975b
make tree run in a sub directory where no other tests can cause race …
gabrielmfern 0e7ae4c
feat(demo): use Tailwind v4 on all templates (#2487)
bukinoshita 0a7d210
fix: lockfile
gabrielmfern ddd37a4
feat(tailwind): update to tailwind v4 (#2425)
gabrielmfern 19de23f
chore(demo): use local tailwind version
gabrielmfern 9360e39
chore: remove preview version and add changeset
gabrielmfern 26ee920
chore: update lockfile
gabrielmfern 3c8938f
chore(root): version packages (canary) (#2578)
github-actions[bot] 3e5ea15
fix success message always showing
gabrielmfern b134d7d
Merge branch 'canary' into feat/new-preview-server-strategy
gabrielmfern 5871d86
fix lock
gabrielmfern 678be46
Merge branch 'canary' into feat/new-preview-server-strategy
gabrielmfern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .test |
36 changes: 14 additions & 22 deletions
36
packages/react-email/src/utils/__snapshots__/tree.spec.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,19 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`tree(__dirname, 2) 1`] = ` | ||
| "utils | ||
| ├── __snapshots__ | ||
| │ └── tree.spec.ts.snap | ||
| ├── esbuild | ||
| │ ├── escape-string-for-regex.ts | ||
| │ └── renderring-utilities-exporter.ts | ||
| ├── preview | ||
| │ ├── hot-reloading | ||
| │ ├── get-env-variables-for-preview-app.ts | ||
| │ ├── index.ts | ||
| │ ├── serve-static-file.ts | ||
| │ └── start-dev-server.ts | ||
| ├── types | ||
| │ ├── hot-reload-change.ts | ||
| │ └── hot-reload-event.ts | ||
| ├── get-emails-directory-metadata.spec.ts | ||
| ├── get-emails-directory-metadata.ts | ||
| ├── get-preview-server-location.ts | ||
| "preview | ||
| ├── hot-reloading | ||
| │ ├── __snapshots__ | ||
| │ ├── test | ||
| │ ├── create-dependency-graph.spec.ts | ||
| │ ├── create-dependency-graph.ts | ||
| │ ├── get-imported-modules.spec.ts | ||
| │ ├── get-imported-modules.ts | ||
| │ ├── resolve-path-aliases.spec.ts | ||
| │ ├── resolve-path-aliases.ts | ||
| │ └── setup-hot-reloading.ts | ||
| ├── get-env-variables-for-preview-app.ts | ||
| ├── index.ts | ||
| ├── packageJson.ts | ||
| ├── register-spinner-autostopping.ts | ||
| ├── tree.spec.ts | ||
| └── tree.ts" | ||
| ├── serve-static-file.ts | ||
| └── start-dev-server.ts" | ||
| `; |
17 changes: 17 additions & 0 deletions
17
packages/react-email/src/utils/get-preview-server-location.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { installPreviewServer } from './get-preview-server-location.js'; | ||
| import { packageJson } from './packageJson.js'; | ||
|
|
||
| test.sequential('installPreviewServer()', { timeout: 10_000 }, async () => { | ||
| const testDirectory = path.join(import.meta.dirname, '.test'); | ||
| await installPreviewServer(testDirectory, packageJson.version); | ||
| expect(fs.existsSync(testDirectory)).toBe(true); | ||
|
|
||
| // @ts-ignore The directory should exist at this point | ||
| const importedModule = await import(path.join(testDirectory, 'index.mjs')); | ||
| expect({ ...importedModule }).toEqual({ | ||
| version: packageJson.version, | ||
| }); | ||
| await fs.promises.rm(testDirectory, { recursive: true, force: true }); | ||
| }); |
139 changes: 102 additions & 37 deletions
139
packages/react-email/src/utils/get-preview-server-location.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,115 @@ | ||
| import child_process from 'node:child_process'; | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import url from 'node:url'; | ||
| import { createJiti } from 'jiti'; | ||
| import { addDevDependency } from 'nypm'; | ||
| import prompts from 'prompts'; | ||
| import logSymbols from 'log-symbols'; | ||
| import ora from 'ora'; | ||
| import { extract } from 'tar'; | ||
| import { packageJson } from './packageJson.js'; | ||
| import { registerSpinnerAutostopping } from './register-spinner-autostopping.js'; | ||
|
|
||
| const ensurePreviewServerInstalled = async ( | ||
| message: string, | ||
| ): Promise<never> => { | ||
| const response = await prompts({ | ||
| type: 'confirm', | ||
| name: 'installPreviewServer', | ||
| message, | ||
| initial: true, | ||
| export async function installPreviewServer(directory: string, version: string) { | ||
| const spinner = ora({ | ||
| text: 'Installing UI', | ||
| prefixText: ' ', | ||
| }); | ||
| if (response.installPreviewServer) { | ||
| console.log('Installing "@react-email/preview-server"'); | ||
| await addDevDependency( | ||
| `@react-email/preview-server@${packageJson.version}`, | ||
| ); | ||
| process.exit(0); | ||
| } else { | ||
| process.exit(0); | ||
| } | ||
| }; | ||
| spinner.start(); | ||
|
|
||
| export const getPreviewServerLocation = async () => { | ||
| const usersProject = createJiti(process.cwd()); | ||
| let previewServerLocation!: string; | ||
| registerSpinnerAutostopping(spinner); | ||
| if (fs.existsSync(directory)) { | ||
| await fs.promises.rm(directory, { recursive: true }); | ||
| } | ||
| await fs.promises.mkdir(directory); | ||
| // Download and unpack the package | ||
| const tempDir = await fs.promises.mkdtemp( | ||
| path.join(os.tmpdir(), 'react-email-'), | ||
| ); | ||
| try { | ||
| previewServerLocation = path.dirname( | ||
| url.fileURLToPath(usersProject.esmResolve('@react-email/preview-server')), | ||
| // Download package using npm pack | ||
| child_process.execSync(`npm pack @react-email/preview-server@${version}`, { | ||
| cwd: tempDir, | ||
| stdio: 'ignore', | ||
| }); | ||
|
|
||
| // Find the downloaded tarball | ||
| const files = await fs.promises.readdir(tempDir); | ||
| const tarball = files.find( | ||
| (file) => | ||
| file.startsWith('react-email-preview-server-') && file.endsWith('.tgz'), | ||
| ); | ||
|
|
||
| if (!tarball) { | ||
| spinner.stopAndPersist({ | ||
| symbol: logSymbols.error, | ||
| text: 'Failed to install UI', | ||
| }); | ||
| throw new Error('Failed to find tarball for UI', { | ||
| cause: { tempDir, files }, | ||
| }); | ||
| } | ||
|
|
||
| // Extract tarball to directory using tar package | ||
| const tarballPath = path.join(tempDir, tarball); | ||
| try { | ||
| await extract({ | ||
| cwd: directory, | ||
| strip: 1, | ||
| file: tarballPath, | ||
| z: true, | ||
| }); | ||
| } catch (exception) { | ||
| spinner.stopAndPersist({ | ||
| symbol: logSymbols.error, | ||
| text: 'Failed to install UI', | ||
| }); | ||
| throw new Error('Failed to extract UI package', { | ||
| cause: exception, | ||
| }); | ||
| } | ||
|
|
||
| const packageJsonPath = path.resolve(directory, './package.json'); | ||
| await fs.promises.cp( | ||
| packageJsonPath, | ||
| path.resolve(directory, './package.source.json'), | ||
| ); | ||
| const packageJson = JSON.parse( | ||
| await fs.promises.readFile(packageJsonPath, 'utf8'), | ||
| ); | ||
| } catch (_exception) { | ||
| await ensurePreviewServerInstalled( | ||
| 'To run the preview server, the package "@react-email/preview-server" must be installed. Would you like to install it?', | ||
| packageJson.dependencies = { | ||
gabrielmfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| next: packageJson.dependencies.next, | ||
| }; | ||
| packageJson.devDependencies = {}; | ||
| await fs.promises.writeFile( | ||
| packageJsonPath, | ||
| JSON.stringify(packageJson, null, 2), | ||
| 'utf8', | ||
| ); | ||
| child_process.execSync('npm install --silent', { | ||
gabrielmfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| stdio: 'ignore', | ||
| cwd: directory, | ||
| }); | ||
| } finally { | ||
| // Clean up temp directory | ||
| await fs.promises.rm(tempDir, { recursive: true, force: true }); | ||
| } | ||
| const { version } = await usersProject.import<{ | ||
| spinner.stopAndPersist({ | ||
| symbol: logSymbols.success, | ||
| text: `UI installed successfully (${directory})\n`, | ||
| }); | ||
| } | ||
|
|
||
| export async function getPreviewServerLocation() { | ||
| const directory = path.join(os.homedir(), '.react-email'); | ||
| if (!fs.existsSync(directory)) { | ||
| await installPreviewServer(directory, packageJson.version); | ||
| } | ||
|
|
||
| const { version } = (await import(path.join(directory, 'index.mjs'))) as { | ||
| version: string; | ||
| }>('@react-email/preview-server'); | ||
| }; | ||
| if (version !== packageJson.version) { | ||
| await ensurePreviewServerInstalled( | ||
| `To run the preview server, the version of "@react-email/preview-server" must match the version of "react-email" (${packageJson.version}). Would you like to install it?`, | ||
| ); | ||
| await installPreviewServer(directory, packageJson.version); | ||
| } | ||
|
|
||
| return previewServerLocation; | ||
| }; | ||
| return directory; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| import path from 'node:path'; | ||
| import { tree } from './tree.js'; | ||
|
|
||
| test('tree(__dirname, 2)', async () => { | ||
| expect(await tree(__dirname, 2)).toMatchSnapshot(); | ||
| expect( | ||
| await tree(path.resolve(import.meta.dirname, './preview'), 2), | ||
| ).toMatchSnapshot(); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.