Skip to content

Commit 8e90ad2

Browse files
committed
fix tests
1 parent eb852ae commit 8e90ad2

File tree

4 files changed

+133
-159
lines changed

4 files changed

+133
-159
lines changed

Diff for: packages/cli/src/commands/exportPreviews.ts

+97-106
Original file line numberDiff line numberDiff line change
@@ -60,113 +60,104 @@ export function previewFilename(moduleName: string, functionName: string) {
6060
);
6161
}
6262

63-
export const handler = buildHandler(
64-
async (argv: ExportPreviewsArgs) => {
65-
if (!argv.outDir) throw new Error("outDir option is not set");
66-
67-
const outDir = argv.outDir;
68-
69-
if (typeof outDir !== "string") {
70-
error("please specify an outDir like --outDir ./html");
71-
return;
72-
}
73-
74-
if (undefined === argv.emailsDir) {
75-
error("please specific an emailsDir like --emailsDir ./emails");
76-
return;
77-
}
78-
79-
const previewsPath = getPreviewsDirectory(argv.emailsDir);
80-
if (!previewsPath) {
81-
error(
82-
"Could not find emails directory. Have you initialized the project with `mailing init`?"
83-
);
84-
return;
85-
}
86-
87-
registerRequireHooks();
88-
89-
const previewText = argv.minify ? "minified preview html" : "preview html";
90-
91-
let count = 0;
92-
93-
const lint: { [filename: string]: HtmlLintError[] } = {};
94-
const toWrite: Array<() => Promise<void>> = [];
95-
const filenames: string[] = [];
96-
97-
const previewRenders = readdirSync(previewsPath)
98-
.filter((path) => !/^\./.test(path))
99-
.flatMap((p) => {
100-
const previewPath = resolve(previewsPath, p);
101-
const previewModule = require(previewPath);
102-
103-
return Object.keys(require(previewPath)).flatMap(
104-
async (previewFunction) => {
105-
const filename = previewFilename(p, previewFunction);
106-
count++;
107-
108-
const { html, errors, htmlLint } = render(
109-
await previewModule[previewFunction]()
110-
);
111-
if (errors.length) {
112-
error(`MJML errors rendering ${filename}:`, errors);
113-
}
114-
115-
if (htmlLint.length && !argv.skipLint) {
116-
lint[filename] = htmlLint;
117-
}
118-
119-
const minifyConfig = {
120-
collapseWhitespace: true,
121-
minifyCSS: false,
122-
caseSensitive: true,
123-
removeEmptyAttributes: true,
124-
};
125-
126-
const outHtml = argv.minify
127-
? await minify(html, minifyConfig)
128-
: html;
129-
filenames.push(filename);
130-
toWrite.push(async () =>
131-
outputFile(resolve(outDir, filename), outHtml)
132-
);
63+
export const handler = buildHandler(async (argv: ExportPreviewsArgs) => {
64+
if (!argv.outDir) throw new Error("outDir option is not set");
65+
66+
const outDir = argv.outDir;
67+
68+
if (typeof outDir !== "string") {
69+
error("please specify an outDir like --outDir ./html");
70+
return;
71+
}
72+
73+
if (undefined === argv.emailsDir) {
74+
error("please specific an emailsDir like --emailsDir ./emails");
75+
return;
76+
}
77+
78+
const previewsPath = getPreviewsDirectory(argv.emailsDir);
79+
if (!previewsPath) {
80+
error(
81+
"Could not find emails directory. Have you initialized the project with `mailing init`?"
82+
);
83+
return;
84+
}
85+
86+
registerRequireHooks();
87+
88+
const previewText = argv.minify ? "minified preview html" : "preview html";
89+
90+
let count = 0;
91+
92+
const lint: { [filename: string]: HtmlLintError[] } = {};
93+
const toWrite: Array<() => Promise<void>> = [];
94+
const filenames: string[] = [];
95+
96+
const previewRenders = readdirSync(previewsPath)
97+
.filter((path) => !/^\./.test(path))
98+
.flatMap((p) => {
99+
const previewPath = resolve(previewsPath, p);
100+
const previewModule = require(previewPath);
101+
102+
return Object.keys(require(previewPath)).flatMap(
103+
async (previewFunction) => {
104+
const filename = previewFilename(p, previewFunction);
105+
count++;
106+
107+
const { html, errors, htmlLint } = render(
108+
await previewModule[previewFunction]()
109+
);
110+
if (errors.length) {
111+
error(`MJML errors rendering ${filename}:`, errors);
133112
}
134-
);
135-
});
136-
await Promise.all(previewRenders);
137-
138-
const lintCount = Object.keys(lint).length;
139-
if (lintCount) {
140-
error(
141-
lintCount > 1
142-
? `Aborted because ${lintCount} files have lint errors:`
143-
: `Aborted because 1 file has lint errors:`,
144-
"\n\n",
145-
Object.entries(lint)
146-
.map(
147-
([filename, errors]) =>
148-
`${filename}\n${errors
149-
.map((e, i) => ` ${i + 1}. ${e.message}`)
150-
.join("\n\n")}`
151-
)
152-
.join("\n\n"),
153-
"\n\n"
154-
);
155113

156-
return;
157-
}
114+
if (htmlLint.length && !argv.skipLint) {
115+
lint[filename] = htmlLint;
116+
}
158117

159-
log(`Exporting ${previewText} to`);
160-
log(`${outDir}/`);
161-
await Promise.all(toWrite.map((f) => f()));
162-
for (const f of filenames.sort()) {
163-
log(` |-- ${f}`);
164-
}
165-
log(`✅ Processed ${count} previews\n`);
166-
},
167-
{
168-
captureOptions: () => {
169-
return { event: "export-previews invoked" };
170-
},
118+
const minifyConfig = {
119+
collapseWhitespace: true,
120+
minifyCSS: false,
121+
caseSensitive: true,
122+
removeEmptyAttributes: true,
123+
};
124+
125+
const outHtml = argv.minify ? await minify(html, minifyConfig) : html;
126+
filenames.push(filename);
127+
toWrite.push(async () =>
128+
outputFile(resolve(outDir, filename), outHtml)
129+
);
130+
}
131+
);
132+
});
133+
await Promise.all(previewRenders);
134+
135+
const lintCount = Object.keys(lint).length;
136+
if (lintCount) {
137+
error(
138+
lintCount > 1
139+
? `Aborted because ${lintCount} files have lint errors:`
140+
: `Aborted because 1 file has lint errors:`,
141+
"\n\n",
142+
Object.entries(lint)
143+
.map(
144+
([filename, errors]) =>
145+
`${filename}\n${errors
146+
.map((e, i) => ` ${i + 1}. ${e.message}`)
147+
.join("\n\n")}`
148+
)
149+
.join("\n\n"),
150+
"\n\n"
151+
);
152+
153+
return;
154+
}
155+
156+
log(`Exporting ${previewText} to`);
157+
log(`${outDir}/`);
158+
await Promise.all(toWrite.map((f) => f()));
159+
for (const f of filenames.sort()) {
160+
log(` |-- ${f}`);
171161
}
172-
);
162+
log(`✅ Processed ${count} previews\n`);
163+
});

Diff for: packages/cli/src/commands/preview/preview.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,9 @@ export const builder = {
2929
},
3030
};
3131

32-
export const handler = buildHandler(
33-
async (argv: PreviewArgs) => {
34-
if (undefined === argv.port) throw new Error("port option is not set");
35-
if (undefined === argv.quiet) throw new Error("quiet option is not set");
32+
export const handler = buildHandler(async (argv: PreviewArgs) => {
33+
if (undefined === argv.port) throw new Error("port option is not set");
34+
if (undefined === argv.quiet) throw new Error("quiet option is not set");
3635

37-
await startPreviewServer();
38-
},
39-
{
40-
captureOptions: () => {
41-
return { event: "preview invoked" };
42-
},
43-
}
44-
);
36+
await startPreviewServer();
37+
});

Diff for: packages/cli/src/commands/server.ts

+28-38
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,40 @@ export const builder = {
3838
},
3939
};
4040

41-
export const handler = buildHandler(
42-
async (argv: ServerArguments) => {
43-
if (undefined === argv.port) throw new Error("port option is not set");
44-
if (undefined === argv.quiet) throw new Error("quiet option is not set");
45-
if (undefined === argv.emailsDir)
46-
throw new Error("emailsDir option is not set");
41+
export const handler = buildHandler(async (argv: ServerArguments) => {
42+
if (undefined === argv.port) throw new Error("port option is not set");
43+
if (undefined === argv.quiet) throw new Error("quiet option is not set");
44+
if (undefined === argv.emailsDir)
45+
throw new Error("emailsDir option is not set");
4746

48-
log("bootstrapping your Mailing server in .mailing...");
49-
await bootstrapMailingDir();
50-
await linkEmailsDirectory(argv.emailsDir);
47+
log("bootstrapping your Mailing server in .mailing...");
48+
await bootstrapMailingDir();
49+
await linkEmailsDirectory(argv.emailsDir);
5150

52-
const shouldStart = argv.subcommand === "start" || !argv.subcommand;
53-
const shouldBuild = argv.subcommand === "build" || !argv.subcommand;
51+
const shouldStart = argv.subcommand === "start" || !argv.subcommand;
52+
const shouldBuild = argv.subcommand === "build" || !argv.subcommand;
5453

55-
// "build" subcommand + default
56-
if (shouldBuild) {
57-
log("building .mailing...");
54+
// "build" subcommand + default
55+
if (shouldBuild) {
56+
log("building .mailing...");
5857

59-
execSync("cd .mailing && npx prisma generate", {
60-
stdio: "inherit",
61-
});
62-
63-
if (process.env.MAILING_DATABASE_URL)
64-
execSync("cd .mailing && npx prisma migrate deploy", {
65-
stdio: "inherit",
66-
});
58+
execSync("cd .mailing && npx prisma generate", {
59+
stdio: "inherit",
60+
});
6761

68-
execSync("cd .mailing && npx next build", {
62+
if (process.env.MAILING_DATABASE_URL)
63+
execSync("cd .mailing && npx prisma migrate deploy", {
6964
stdio: "inherit",
7065
});
71-
}
7266

73-
// "start" subcommand + default
74-
if (shouldStart) {
75-
log("starting .mailing...");
76-
execSync("npx next start .mailing", { stdio: "inherit" });
77-
}
78-
},
79-
{
80-
captureOptions: (argv) => {
81-
return {
82-
event: "server invoked",
83-
properties: { subcommand: argv.subcommand },
84-
};
85-
},
67+
execSync("cd .mailing && npx next build", {
68+
stdio: "inherit",
69+
});
70+
}
71+
72+
// "start" subcommand + default
73+
if (shouldStart) {
74+
log("starting .mailing...");
75+
execSync("npx next start .mailing", { stdio: "inherit" });
8676
}
87-
);
77+
});

Diff for: packages/cli/src/util/__test__/buildHandler.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("buildHandler", () => {
1414

1515
describe("shared guards and setup", () => {
1616
it("returns early if ./package.json is not found", async () => {
17-
const handler = buildHandler(async () => {}, {});
17+
const handler = buildHandler(async () => {});
1818
const argv = {
1919
emailsDir: "./emails",
2020
};
@@ -37,7 +37,7 @@ describe("buildHandler", () => {
3737
});
3838

3939
it("throws an error if emailsDir is not specified in argv", async () => {
40-
const handler = buildHandler(async () => {}, {});
40+
const handler = buildHandler(async () => {});
4141
const argv = {};
4242

4343
await expect(async () => {
@@ -46,7 +46,7 @@ describe("buildHandler", () => {
4646
});
4747

4848
it("calls setConfig and writeDefaultConfigFile", async () => {
49-
const handler = buildHandler(async () => {}, {});
49+
const handler = buildHandler(async () => {});
5050
const argv = {
5151
emailsDir: "./emails",
5252
port: 3883,

0 commit comments

Comments
 (0)