Skip to content

Commit

Permalink
refactor(create-jsx-email): use esm, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Nov 2, 2024
1 parent 9760d91 commit 3fe7a33
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 43 deletions.
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
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
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';
12 changes: 8 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,12 @@
"email"
],
"dependencies": {
"chalk": "4.1.2",
"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
19 changes: 13 additions & 6 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 { 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 Down Expand Up @@ -46,6 +52,8 @@ const typeProps = `\nexport type TemplateProps = {
email: string;
name: string;
}`;
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 @@ -72,7 +80,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
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;
70 changes: 59 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions shared/tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"../scripts",
"../shared",
"../test",
"../vitest.config.ts",
"../vitest.workspace.ts"
"./vitest.config.ts"
]
}
1 change: 0 additions & 1 deletion test/cli/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

import { execa } from 'execa';
import { describe, expect, test } from 'vitest';
import strip from 'strip-ansi';

process.chdir(__dirname);
Expand Down
16 changes: 16 additions & 0 deletions test/cli/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://moonrepo.dev/docs/config/tasks
$schema: 'https://moonrepo.dev/schemas/tasks.json'

workspace:
inheritedTasks:
exclude: ['build', 'compile', 'release', 'test']

tasks:
test:
command: vitest --config ../../shared/vitest.config.ts . --no-threads
inputs:
- ./**.*.test.ts
- package.json
options:
cache: false
outputStyle: 'stream'
11 changes: 11 additions & 0 deletions test/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-cli",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"execa": "^9.4.1",
"jsx-email": "workspace:^",
"strip-ansi": "^7.1.0"
}
}
2 changes: 1 addition & 1 deletion test/smoke/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "smoke-test",
"name": "test-smoke",
"version": "0.0.0",
"private": true,
"description": "A project to demo and test the `email preview` CLI command",
Expand Down

0 comments on commit 3fe7a33

Please sign in to comment.