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

refactor(create-jsx-email): use esm, add tests #234

Merged
merged 4 commits into from
Nov 4, 2024
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
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dist
node_modules

# Starter template files
packages/create-jsx-email/starter
packages/create-jsx-email/starter
2 changes: 1 addition & 1 deletion .github/workflows/test-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ jobs:
# We'll need that for the preview tests below
run: |
pnpm i
moon repo:test.cli
moon test-cli:test.run
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ jobs:
# We'll need that for the preview tests below
run: |
pnpm i
moon smoke:run.ci
moon test-smoke:run.ci
1 change: 1 addition & 0 deletions .moon/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ projects:
globs:
- 'apps/*'
- 'packages/*'
- 'test/cli'
- 'test/smoke'
sources:
root: '.'
Expand Down
12 changes: 7 additions & 5 deletions assets/templates/email.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Section,
Text
} from 'jsx-email';

{{ typeProps }}
const main = {
backgroundColor: '#f6f9fc',
fontFamily:
Expand Down Expand Up @@ -46,8 +46,8 @@ const anchor = {

const button = {
fontWeight: 'bold',
textDecoration: 'none',
padding: '10px'
padding: '10px',
textDecoration: 'none'
};

export const previewProps = {
Expand All @@ -66,13 +66,15 @@ export const Template = ({ email, name }{{ propsType }}) => (
<Section style={box}>
<Text style={paragraph}>This is our email body text</Text>
<Button
style={button}
href="https://example.com"
align={'center'}
backgroundColor={'#777'}
borderRadius={5}
fontSize={16}
height={60}
href="https://example.com"
style={button}
textColor={'#fff'}
width={160}
>
Action Button
</Button>
Expand Down
13 changes: 0 additions & 13 deletions moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,3 @@ tasks:
cache: false
outputStyle: 'stream'
runDepsInParallel: false

test.cli:
command: vitest --config ./shared/vitest.config.ts test/cli --no-threads
deps:
- jsx-email:build
- plugin-inline:build
- plugin-minify:build
- plugin-pretty:build
inputs:
- test/cli
options:
cache: false
outputStyle: 'stream'
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
"csstype": "3.1.2",
"eslint-config-shellscape": "^6.0.2",
"eslint-import-resolver-typescript": "^3.6.1",
"execa": "^9.4.1",
"gh-pages": "^6.0.0",
"happy-dom": "^12.2.1",
"husky": "^8.0.3",
"jsx-email": "workspace:*",
"lint-staged": "14.0.1",
"strip-ansi": "^7.1.0",
"ts-node": "10.9.1",
"tshy": "^1.14.0",
"typescript": "^5.2.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/create-jsx-email/bin/create-jsx-email
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env -S node --enable-source-maps --no-warnings=ExperimentalWarning
import './../dist/index.js';
13 changes: 9 additions & 4 deletions packages/create-jsx-email/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
"url": "https://github.com/shellscape/jsx-email.git",
"directory": "packages/create-jsx-email"
},
"homepage": "https://jsx.email/",
"main": "dist/src/index.js",
"bin": {
"create-jsx-email": "./dist/src/index.js"
"create-jsx-email": "./bin/create-jsx-email"
},
"type": "module",
"engines": {
"node": ">=18.0.0"
},
"files": [
"bin/**",
"dist/**"
],
"keywords": [
Expand All @@ -27,11 +30,13 @@
"email"
],
"dependencies": {
"chalk": "4.1.2",
"@types/yargs-parser": "^21.0.3",
"chalk-template": "^1.1.0",
"detect-package-manager": "^3.0.1",
"globby": "11.0.4",
"globby": "14.0.2",
"mustache": "^4.2.0",
"prompts": "^2.4.2"
"prompts": "^2.4.2",
"yargs-parser": "^21.1.1"
},
"devDependencies": {
"@types/fs-extra": "^11.0.2",
Expand Down
35 changes: 22 additions & 13 deletions packages/create-jsx-email/src/index.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
#!/usr/bin/env node
/* eslint-disable no-await-in-loop */
/* eslint-disable no-await-in-loop, no-underscore-dangle */

import { existsSync, readdirSync, rmSync } from 'fs';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { basename, dirname, join, resolve, win32, posix } from 'path';
import { basename, dirname, join, relative, resolve, win32, posix } from 'path';
import { fileURLToPath } from 'node:url';

import chalk from 'chalk';
import chalk from 'chalk-template';
// Note: https://github.com/egoist/detect-package-manager/issues/18
// @ts-ignore
import { detect } from 'detect-package-manager';
import globby from 'globby';
import { globby } from 'globby';
import mustache from 'mustache';
import prompts from 'prompts';
import yargs from 'yargs-parser';

import pkg from '../package.json';
import * as pkg from './package-info.cjs';

interface CreateEmailArgs {
jsx: boolean;
name: string;
outputPath: string;
}

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const cancelled = () => {
throw new Error(chalk`{red ✖} Operation cancelled`);
};
Expand All @@ -42,10 +48,12 @@ const isEmpty = (path: string) => {
const { log } = console;
const normalizePath = (filename: string) => filename.split(win32.sep).join(posix.sep);
const typeDep = ',\n"@types/react": "^18.2.0",\n"typescript": "^5.2.2"';
const typeProps = `\nexport type TemplateProps = {
const typeProps = `\ninterface TemplateProps {
email: string;
name: string;
}`;
}\n`;
const argv = yargs(process.argv.slice(2), { configuration: { 'strip-dashed': true } });
const argTargetDir: string = argv._[0] as string;

export const createEmail = async ({ jsx, name, outputPath }: CreateEmailArgs) => {
const data = {
Expand All @@ -59,9 +67,10 @@ export const createEmail = async ({ jsx, name, outputPath }: CreateEmailArgs) =>
const fileName = basename(templatePath)
.replace('_', '')
.replace('.mustache', jsx ? '.jsx' : '.tsx');
const outPath = join(outputPath, fileName);
const relativePath = relative(process.cwd(), outputPath);
const outPath = join(relativePath, fileName);

log('Creating a new template at', outPath);
log(chalk`{blue Creating a new template} at: {cyan ${outPath}}`);

await writeFile(outPath, contents, 'utf8');

Expand All @@ -72,7 +81,6 @@ const run = async () => {
log(intro);

const skip = process.argv.some((arg) => arg === '--yes');
const argTargetDir = formatTargetDir(process.argv[2]);
let targetPath = argTargetDir || defaultTargetDir;
let result: prompts.Answers<'projectName' | 'projectType' | 'overwrite'>;
const defaults: typeof result = {
Expand Down Expand Up @@ -134,9 +142,10 @@ const run = async () => {
const templates = await globby([normalizePath(join(generatorsPath, '/*.*'))]);
const outputPath = join(root, 'templates');
const templateData = { name: projectName, typeDep: jsx ? '' : typeDep };
const relativePath = relative(process.cwd(), outputPath);

log('');
log(chalk`{blue Creating Project} at: {dim ${root}}`);
log(chalk`{blue Creating Project} at: {dim ${relativePath}}`);

if (overwrite && existsSync(root)) clearDirectory(root);

Expand All @@ -156,7 +165,7 @@ const run = async () => {

await createEmail({ jsx, name: projectName, outputPath });

const packageManager = await detect();
const packageManager = process.env.IS_CLI_TEST ? 'pnpm' : await detect();
const install =
packageManager === 'yarn'
? ` $ yarn\n $ yarn dev`
Expand Down
5 changes: 5 additions & 0 deletions packages/create-jsx-email/src/package-info.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Note: This is a workaround for Node v18 and Node v20 having different import assertion formats
// eslint-disable-next-line
const pkg = require('../package.json') as { description: string; name: string; version: string };

export const { description, name, version } = pkg;
Loading
Loading