Skip to content

Commit

Permalink
Lint no floating promises 2 (#370)
Browse files Browse the repository at this point in the history
* await creating the default list

* for completeness: also assert organization

* lint for await

* ooops - put the eslint config for ts in the right place

* eslint should ignore .mailing directories

* add tsconfig's to parserOptions.project in @typescript-eslint/parser block

* await async functions

* don't ignore __test__ from tsconfig in core

* linter says that this expect must be awaited

* tsconfig covers all the .ts files in the root directory:

jest.config.ts			testSetup.integration.ts	testUtilIntegration.ts
jest.integration.config.ts	testSetup.ts

* tsconfig should include e2e/**/*

* src/cli/generator_templates/**/* is not a directory in packages/cli

* await some async functions

* we're using plain js in e2e/mailing_tests so don't need a tsconfig

* fix typo

* await analytics calls

* eslintignore these 2 files:
packages/cli/src/generator_templates
packages/cli/src/emails

because they are ignored in tsconfig.json

* update files attribute in package.json

* don't actually want to ignore those

* this is my workaround for typescript-eslint/typescript-eslint#2987

* noop?

* simplify tsconfigs?

* fix all the lints

* oops

* try void

* async await

* smaller change

* fix server build

* Revert "fix server build"

This reverts commit e24e0ed.

* await -> void in init

* remove trailing comma in json

* cypress does not actually need to reset the web db, so remove

* server: try cd into .mailing and then npx next build with the current directory

* pull posthog api key into its own file so it does not bloat the next build by pulling in posthog-node

* import from the righ tplaÿce
  • Loading branch information
alexfarrill authored Dec 1, 2022
1 parent e155434 commit 4fe6211
Show file tree
Hide file tree
Showing 30 changed files with 65 additions and 61 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,23 @@
{
"files": ["**/*.ts", "**/*.tsx"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": [
"./tsconfig.json",
"./packages/cli/tsconfig.json",
"./packages/core/tsconfig.json",
"./packages/web/tsconfig.json",
"./packages/cli/cypress/tsconfig.json"
]
},
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": [
"error",
Expand Down
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"

yarn lint:unlink
yarn lint-staged
yarn lint:link
1 change: 0 additions & 1 deletion .lintstagedrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
"*.rb": "rubocop --autocorrect",
"packages/cli/src/generator_templates/ts/**/*.{ts,tsx}": "scripts/build-js-templates"
}

8 changes: 0 additions & 8 deletions e2e/mailing_tests/cypress/tsconfig.json

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"packages/*"
],
"scripts": {
"lint": "eslint .",
"lint": "yarn lint:unlink; eslint .; yarn lint:link",
"lint:unlink": "rm packages/cli/src/emails",
"lint:link": "ln -s 'generator_templates/ts/emails/' packages/cli/src/emails",
"dev": "cd packages/cli && yarn dev",
"dev:js": "yarn link:emails:js && yarn dev",
"dev:init": "cd packages/cli && rm -rf previews_html; rm -rf .mailing emails mailing.config.json; src/dev.js",
Expand Down
12 changes: 5 additions & 7 deletions packages/cli/cypressIntegration.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { defineConfig } from "cypress";
import cliPrisma from "./prisma";
import webPrisma from "../../packages/web/prisma";
import type { PrismaTableName } from "../../testUtilIntegration";

interface PrismaTableName {
table_name: string;
}

const resetDb = async () => {
await truncateDatabases();
Expand Down Expand Up @@ -31,12 +33,8 @@ export async function truncateCliDatabase() {
await truncateTables(cliPrisma);
}

export async function truncateWebDatabase() {
await truncateTables(webPrisma);
}

export async function truncateDatabases() {
return Promise.all([truncateCliDatabase(), truncateWebDatabase()]);
await truncateCliDatabase();
}

async function truncateTables(client: any) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const handler = buildHandler(
try {
const url = `${getMailingAPIBaseURL()}/api/newsletterSubscribers`;

fetch(url, {
void fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
Expand All @@ -109,7 +109,7 @@ export const handler = buildHandler(
_: argv._,
};

previewHandler(previewHandlerArgv);
await previewHandler(previewHandlerArgv);
},
{
captureOptions: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("livereload", () => {
done();
});

touchTemplate();
void touchTemplate();
});
});
});
2 changes: 1 addition & 1 deletion packages/cli/src/commands/preview/server/livereload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function startChangeWatcher(server: Server, emailsDir: string) {
if (WATCH_IGNORE.test(filename)) return;
log(`detected ${eventType} on ${filename}, reloading`);
delete require.cache[resolve(changeWatchPath, filename)];
reload();
void reload();
}
);

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const handler = buildHandler(
stdio: "inherit",
});

execSync("npx next build .mailing", {
execSync("cd .mailing && npx next build", {
stdio: "inherit",
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type TreeRoute = {

const safeReplaceState = debounce((router: NextRouter | null, path: string) => {
try {
router?.replace(path, undefined, { shallow: true });
void router?.replace(path, undefined, { shallow: true });
} catch (e) {
// Debounce should be avoiding this error, but catch just in case:
// SecurityError: Attempt to use history.replaceState() more than 100 times per 30 seconds
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/components/PosthogScript.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { POSTHOG_API_KEY } from "../util/postHog/client";
import { POSTHOG_API_KEY } from "../util/postHog/posthogApiKey";

export default function PosthogScript() {
if (process.env.NODE_ENV !== "production") {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/components/PreviewViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const PreviewViewer: React.FC<PreviewViewerProps> = ({ initialData }) => {

const fetchData = useCallback(async () => {
// fire this one async, no loader
fetchPreviews();
await fetchPreviews();

if (!(previewClass && previewFunction)) {
setData((data: Data) => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const config = existsSync(MAILING_CONFIG_FILE)
: {};

// prettier-ignore
yargs(process.argv.slice(2))
void yargs(process.argv.slice(2))
.config(config)
.command(init)
.command(preview)
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/pages/api/lists/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ const ApiLists = withSessionAPIRoute(async function (

switch (req.method) {
case "GET":
handleGetLists(user, req, res);
await handleGetLists(user, req, res);
break;
case "POST":
handleCreateList(user, req, res);
await handleCreateList(user, req, res);
break;
default:
return res.status(404).end();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/pages/api/pageSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async function PageSnap(

// Create a page with the Open Graph image size best practice
const page = await context.newPage();
page.setViewportSize({ width: 1200, height: 630 });
await page.setViewportSize({ width: 1200, height: 630 });
await page.goto(url, { timeout: 15 * 1000, waitUntil: "networkidle" });
// Press Command+Period to go full screen
await page.keyboard.press("Meta+.");
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/pages/intercepts/[interceptId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ShowIntercept = () => {
setData(await res.json());
}, [query.interceptId]);
useEffect(() => {
if (query.interceptId) fetchData();
if (query.interceptId) void fetchData();
}, [query.interceptId, fetchData]);

return <Intercept data={data} />;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/pages/unsubscribe/[memberId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ const Unsubscribe = (props: UnsubscribeProps) => {

// the "Subscribe" or "Unsubscribe" button was clicked on the form with only the default list
const subscribeUnsubscribeButtonClick = useCallback(
(e: React.MouseEvent<HTMLElement>) => {
async (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
setFormSaving(true);
const newFormState = onChange(defaultList.id, true)();
serializeAndSubmitForm(newFormState);
await serializeAndSubmitForm(newFormState);
},
[defaultList.id, onChange, serializeAndSubmitForm]
);
Expand Down
11 changes: 7 additions & 4 deletions packages/cli/src/util/analytics/__test__/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe("analytics", () => {
});

describe("#trackMany", () => {
it("should call fetch with the correct arguments", () => {
analytics.trackMany([
it("should call fetch with the correct arguments", async () => {
await analytics.trackMany([
{ event: "test.event.one", properties: { foo: "bar.one" } },
{ event: "test.event.two", properties: { foo: "bar.two" } },
]);
Expand Down Expand Up @@ -75,8 +75,11 @@ describe("analytics", () => {
});

describe("#track", () => {
it("should call fetch with the correct arguments", () => {
analytics.track({ event: "test.event", properties: { foo: "bar" } });
it("should call fetch with the correct arguments", async () => {
await analytics.track({
event: "test.event",
properties: { foo: "bar" },
});
expect(fetch).toHaveBeenCalledTimes(2);

// Axiom call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe("Axiom", () => {
});

describe("#trackMany", () => {
it("should call fetch with the correct arguments", () => {
axiom.trackMany([
it("should call fetch with the correct arguments", async () => {
await axiom.trackMany([
{ event: "test.event.one", properties: { foo: "bar.one" } },
{ event: "test.event.two", properties: { foo: "bar.two" } },
]);
Expand Down Expand Up @@ -40,8 +40,8 @@ describe("Axiom", () => {
});

describe("#track", () => {
it("should call fetch with the correct arguments", () => {
axiom.track({ event: "test.event", properties: { foo: "bar" } });
it("should call fetch with the correct arguments", async () => {
await axiom.track({ event: "test.event", properties: { foo: "bar" } });
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(
"https://cloud.axiom.co/api/v1/datasets/datasetName/ingest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe("Posthog", () => {
});

describe("#trackMany", () => {
it("should call fetch with the correct arguments", () => {
posthog.trackMany([
it("should call fetch with the correct arguments", async () => {
await posthog.trackMany([
{ event: "test.event.one", properties: { foo: "bar.one" } },
{ event: "test.event.two", properties: { foo: "bar.two" } },
]);
Expand Down Expand Up @@ -39,8 +39,8 @@ describe("Posthog", () => {
});

describe("#track", () => {
it("should call fetch with the correct arguments", () => {
posthog.track({ event: "test.event", properties: { foo: "bar" } });
it("should call fetch with the correct arguments", async () => {
await posthog.track({ event: "test.event", properties: { foo: "bar" } });
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith("https://app.posthog.com/capture/", {
method: "POST",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/util/buildHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type BuildHandlerOptions = {
};

export function buildHandler(
handler: (argv: any) => void,
handler: (argv: any) => Promise<void>,
options: BuildHandlerOptions
) {
return async (argv: any) => {
Expand Down Expand Up @@ -51,7 +51,7 @@ export function buildHandler(

await handler(argv);
} finally {
shutdownAnalytics();
await shutdownAnalytics();
}
};
}
3 changes: 1 addition & 2 deletions packages/cli/src/util/postHog/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PostHog } from "posthog-node";

export const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY;
import { POSTHOG_API_KEY } from "./posthogApiKey";

let client: PostHog | undefined;

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/util/postHog/posthogApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY;
4 changes: 1 addition & 3 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
"baseUrl": "./"
},
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["src/**/*.ts", "src/**/*.tsx", "prisma/**/*.ts"],
"include": ["./*.ts", "src/**/*.ts", "src/**/*.tsx", "prisma/**/*.ts"],
"exclude": [
"src/emails/**/*",
"dist/**/*",
"src/cli/generator_templates/**/*",
"node_modules"
]
}
6 changes: 3 additions & 3 deletions packages/core/src/__test__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("index", () => {
});

describe("subject", () => {
it("throws an error if missing subject", () => {
it("throws an error if missing subject", async () => {
const sendMail = buildSendMail({
transport,
defaultFrom: "[email protected]",
Expand All @@ -50,7 +50,7 @@ describe("index", () => {
html: "ok",
});

expect(callSendMail()).rejects.toThrowError(
await expect(callSendMail()).rejects.toThrowError(
"sendMail couldn't find a subject for your email"
);
});
Expand Down Expand Up @@ -500,7 +500,7 @@ describe("index", () => {
subject: "hello",
});

expect(callSendMail).rejects.toThrowError(
await expect(callSendMail).rejects.toThrowError(
"Templates sent to a list must include an unsubscribe link. Add an unsubscribe link or remove the list parameter from your sendMail call."
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function buildSendMail<T>(options: BuildSendMailOptions<T>) {
});
if (response.status === 201) {
const { id } = (await response.json()) as { id: string };
open(`${PREVIEW_SERVER_URL}/${id}`);
await open(`${PREVIEW_SERVER_URL}/${id}`);
} else {
error(`Error hitting ${PREVIEW_SERVER_URL}`);
error(response);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
},
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["src/**/*"],
"exclude": ["src/**/__test__/*", "dist/**/*"]
"exclude": ["dist", "node_modules"]
}
2 changes: 1 addition & 1 deletion packages/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"incremental": true
},
"include": ["global.d.ts", "next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"exclude": ["dist", "node_modules"]
}
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
},
"$schema": "https://json.schemastore.org/tsconfig",
"include": [
"./*.ts",
"packages/*/src/**/*",
"packages/web/**/*",
"testSetup.ts",
"emails/**/*"
"emails/**/*",
"e2e"
],
"exclude": [
"packages/**/__test__/*",
"packages/**/__integration__/*",
"packages/*/src/emails/**/*",
"packages/*/dist/**/*",
"packages/cli/src/generator_templates/**/*"
"packages/*/dist/**/*"
],
"ts-node": {
"files": true
Expand Down

3 comments on commit 4fe6211

@vercel
Copy link

@vercel vercel bot commented on 4fe6211 Dec 1, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

web-emails – ./packages/web

web-emails-git-main-sofn.vercel.app
emails.mailing.run
web-emails-sofn.vercel.app
mailing-web.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4fe6211 Dec 1, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

web – ./packages/web

web-rho-puce.vercel.app
web-sofn.vercel.app
mailing.run
www.mailing.run
web-git-main-sofn.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4fe6211 Dec 1, 2022

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.