Skip to content

Commit

Permalink
feat: introduce knip (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy committed Feb 11, 2025
1 parent eb0c7c4 commit 7448d36
Show file tree
Hide file tree
Showing 40 changed files with 431 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-db/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
runs:
using: composite
steps:
- run: cp ./.env.sample ./.env
- run: cp ./.env.test ./.env
shell: bash
- run: pnpm db:up && pnpm db:migrate
if: ${{ inputs.run_db == 'true' }}
Expand Down
2 changes: 2 additions & 0 deletions .github/actions/setup-node/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ runs:
shell: bash
- run: pnpm i
shell: bash
- run: cp ./.env.test ./.env
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
runs-on: ubuntu-latest
env:
# should store them to github.secrets
DATABASE_URL: postgresql://dev:[email protected]:5432/dev?schema=public
DATABASE_URL: postgresql://test:[email protected]:5432/dev?schema=public
NEXTAUTH_SECRET: UfxvOS6HetHOFkL44YTITYgc0DOuOlz5TBp3jkbnZ3w=
NEXT_PUBLIC_SITE_URL: http://localhost:3000
steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/internal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ jobs:
env:
# to fetch this repo via CLI on main branch
IS_MAIN_BRANCH: ${{ env.IS_MAIN_BRANCH }}
IS_LOCAL: false
6 changes: 5 additions & 1 deletion .internal/create-app-foundation/src/git.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export function tryGitInit(root) {

try {
execSync("git --version", { stdio: "ignore" });
if (isInGitRepository() || isInMercurialRepository()) {

if (
process.env.DEBUG !== "true" &&
(isInGitRepository() || isInMercurialRepository())
) {
return false;
}

Expand Down
11 changes: 9 additions & 2 deletions .internal/setup/code/app-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { clsx } from "clsx";
import type { Metadata, Viewport } from "next";
import { Inter } from "next/font/google";
import type { PropsWithChildren } from "react";
import { Footer } from "./_components/Footer";
import { Header } from "./_components/Header";
import "./globals.css";

Expand All @@ -20,9 +21,15 @@ export const viewport: Viewport = {
export default function Layout({ children }: PropsWithChildren) {
return (
<html lang="en">
<body className={clsx("bg-gray-800 text-gray-200", inter.className)}>
<body
className={clsx(
"bg-gray-800 text-gray-200 flex flex-col min-h-screen",
inter.className,
)}
>
<Header />
<main>{children}</main>
<main className="flex-1">{children}</main>
<Footer />
</body>
</html>
);
Expand Down
4 changes: 4 additions & 0 deletions .internal/setup/common-processing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ export const modifiedFiles = /** @type {const} */ ([
".gitignore",
".github/workflows/ci.yml",
"README.md",
"knip.config.ts",
]);

export async function executeCommonProcessing() {
const fences = [
["####### 👉 remove #######", "########################"],
["<!-- 👉 remove -->", "<!-- ######## -->"],
["// 👉 remove", "///////////"],
];

await Promise.all([
removeLines([
[modifiedFiles[0], fences[0]],
[modifiedFiles[1], fences[0]],
[modifiedFiles[2], fences[1]],
[modifiedFiles[2], fences[1]],
[modifiedFiles[3], fences[2]],
]),
(async () => {
title("Updating package.json");
Expand Down
4 changes: 3 additions & 1 deletion .internal/setup/db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export async function generateMigrationFiles() {
resolve();
} else {
reject(
new Error(`[docker compose]: command failed with code ${code}`),
new Error(
`[docker compose]: "${command}" failed with code ${code}`,
),
);
}
});
Expand Down
2 changes: 2 additions & 0 deletions .internal/setup/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ await removeDirs([".internal"]);

await format();

await execAsync("npx lefthook install");

console.info("done! please commit them 🐶");
7 changes: 2 additions & 5 deletions .internal/setup/questions/e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
removeNpmScripts,
} from "../utils.mjs";

export const removedFiles = /** @type {const} */ ([
"playwright.config.ts",
"./src/instrumentation.ts",
]);
export const removedFiles = /** @type {const} */ (["playwright.config.ts"]);

export const removedDirs = /** @type {const} */ (["e2e"]);

Expand All @@ -27,7 +24,7 @@ export async function e2e(answer, isSkipQuestion) {
];

await executeOptionalQuestion({
question: "> Do you want to remove openTelemetry files? (y/N) ",
question: "> Do you want to remove e2e test? (y/N) ",
answer,
isSkipQuestion,
codeAndFenceList: [
Expand Down
9 changes: 9 additions & 0 deletions .internal/setup/questions/sample-code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from "node:path";
import {
executeOptionalQuestion,
readFileFromCopiedDir,
removeDeps,
removeDirs,
removeFiles,
writeFileToCopiedDir,
Expand Down Expand Up @@ -39,6 +40,13 @@ export const modifiedFiles = /** @type {const} */ ([
"src/middleware.test.ts",
]);

// TODO: need to consider keeping them or not
const removedDeps = /** @type {const} */ [
"@hookform/resolvers",
"react-hook-form",
"@testing-library/react",
];

export async function sampleCode(answer, isSkipQuestion) {
const fences = [["/* start: sample */", "/* end: sample */"]];

Expand All @@ -57,6 +65,7 @@ export async function sampleCode(answer, isSkipQuestion) {
removeItemModelFromPrisma(),
removeFiles(removedFiles),
removeDirs(removedDirs),
removeDeps(removedDeps),
writeFileToCopiedDir(
modifiedFiles[1],
await readReplacedCode("app-layout.tsx"),
Expand Down
14 changes: 7 additions & 7 deletions .internal/site/src/features/code-quality-automation.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Code Quality Automation

| | | | | | |
| :-------------------------------------------------------: | :-------------------------------------------------------------: | :---------------------------------------------------------: | :---------------------------------------------------------------------: | --------------------------------------------------------------- | --------------------------------------------------------------- |
| <img src="/images/libs/biome.png" alt="biome" width="40"> | <img src="/images/libs/prettier.png" alt="prettier" width="40"> | <img src="/images/libs/prisma.png" alt="prisma" width="40"> | <img src="/images/libs/editorconfig.png" alt="editorconfig" width="40"> | <img src="/images/libs/lefthook.png" alt="lefthook" width="40"> | <img src="/images/libs/renovate.png" alt="renovate" width="40"> |
| | | | | | | |
| :-------------------------------------------------------: | :-------------------------------------------------------------: | :---------------------------------------------------------: | :-----------------------------------------------------: | ----------------------------------------------------------------------- | --------------------------------------------------------------- | --------------------------------------------------------------- |
| <img src="/images/libs/biome.png" alt="biome" width="40"> | <img src="/images/libs/prettier.png" alt="prettier" width="40"> | <img src="/images/libs/prisma.png" alt="prisma" width="40"> | <img src="/images/libs/knip.png" alt="knip" width="40"> | <img src="/images/libs/editorconfig.png" alt="editorconfig" width="40"> | <img src="/images/libs/lefthook.png" alt="lefthook" width="40"> | <img src="/images/libs/renovate.png" alt="renovate" width="40"> |

### Code Quality

Expand All @@ -24,14 +24,14 @@ Lefthook deploys shell scripts to the `.githooks` directory during the postinsta

No Configuration File.

### EditorConfig

<<< ../../../../.editorconfig

### Prisma

No Configuration File.

### EditorConfig

<<< ../../../../.editorconfig

### Renovate

<<< ../../../../renovate.json
22 changes: 11 additions & 11 deletions .internal/site/src/introduction/what-is-web-app-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ You can choose `--remove-sample-code` when creating a project and the applicatio

These are the main libraries that are installed. However, Playwright and OpenTelemetry can be opted out at install time. If you have any unnecessary libraries, please delete them after the initial creation.

| | | | | |
| ----------- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| **App** | <div align="center"><img src="/images/libs/nextjs.png" alt="nextjs" width="44"><br>Next.js</div> | <div align="center"><img src="/images/libs/tailwind.png" alt="tailwind" width="44"><br>Tailwind CSS</div> | <div align="center"><img src="/images/libs/next-auth.png" alt="next-auth" width="44"><br>NextAuth.js</div> | <div align="center"><img src="/images/libs/react-hook-form.png" alt="react-hook-form" width="44"><br>React Hook Form</div> |
| | <div align="center"><img src="/images/libs/zod.svg" alt="zod" width="44"><br>Zod </div> | <div align="center"><img src="/images/libs/otel.png" alt="otel" width="44"><br>OpenTelemetry </div> | <div align="center"><img src="/images/libs/prisma.png" alt="prisma" width="44"><br>Prisma </div> | <div align="center"><img src="/images/libs/postgresql.png" alt="prisma" width="44"><br>PostgreSQL</div> |
| | | | |
| **Tools** | <div align="center"><img src="/images/libs/typescript.png" alt="typescirpt" width="44"><br>TypeScript</div> | <div align="center"><img src="/images/libs/pnpm.svg" alt="pnpm" width="44"><br>pnpm</div> | <div align="center"><img src="/images/libs/biome.png" alt="biome" width="44"><br>Biome </div> | <div align="center"><img src="/images/libs/prettier.png" alt="prettier" width="44"><br> Prettier</div> |
| | <div align="center"><img src="/images/libs/editorconfig.png" alt="editorconfig" width="44"><br> EditorConfig </div> | <div align="center"><img src="/images/libs/lefthook.png" alt="lefthook" width="44"><br> lefthook</div> | <div align="center"><img src="/images/libs/docker.png" alt="docker" width="44"><br> Docker </div> | |
| | | | |
| **Testing** | <div align="center"><img src="/images/libs/vitest.png" alt="vitest" width="44"><br> Vitest</div> | <div align="center"><img src="/images/libs/testing-library.png" alt="testing-library" width="44"><br> Testing Library</div> | <div align="center"><img src="/images/libs/playwright.png" alt="playwright" width="44"><br> Playwright</div> | <div align="center"><img src="/images/libs/testcontainers.png" alt="testcontainers" width="44"><br> Testcontainers</div> |
| | | |
| **Others** | <div align="center"><img src="/images/libs/github-actions.png" alt="actions" width="44"><br> GitHub Actions</div> | <div align="center"><img src="/images/libs/renovate.png" alt="renovate" width="44"><br> Renovate</div> | <div align="center"><img src="/images/libs/vscode.png" alt="vscode" width="44"><br> VSCode</div> | <div align="center"><img src="/images/libs/copilot.png" alt="copilot edits" width="44"><br> Copilot Edits</div> |
| | | | | |
| ----------- | ----------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| **App** | <div align="center"><img src="/images/libs/nextjs.png" alt="nextjs" width="44"><br>Next.js</div> | <div align="center"><img src="/images/libs/tailwind.png" alt="tailwind" width="44"><br>Tailwind CSS</div> | <div align="center"><img src="/images/libs/next-auth.png" alt="next-auth" width="44"><br>NextAuth.js</div> | <div align="center"><img src="/images/libs/react-hook-form.png" alt="react-hook-form" width="44"><br>React Hook Form</div> |
| | <div align="center"><img src="/images/libs/zod.svg" alt="zod" width="44"><br>Zod </div> | <div align="center"><img src="/images/libs/otel.png" alt="otel" width="44"><br>OpenTelemetry </div> | <div align="center"><img src="/images/libs/prisma.png" alt="prisma" width="44"><br>Prisma </div> | <div align="center"><img src="/images/libs/postgresql.png" alt="prisma" width="44"><br>PostgreSQL</div> |
| | | | |
| **Tools** | <div align="center"><img src="/images/libs/typescript.png" alt="typescirpt" width="44"><br>TypeScript</div> | <div align="center"><img src="/images/libs/pnpm.svg" alt="pnpm" width="44"><br>pnpm</div> | <div align="center"><img src="/images/libs/biome.png" alt="biome" width="44"><br>Biome </div> | <div align="center"><img src="/images/libs/prettier.png" alt="prettier" width="44"><br> Prettier</div> |
| | <div align="center"><img src="/images/libs/knip.png" alt="knip" width="44"><br>Knip</div> | <div align="center"><img src="/images/libs/editorconfig.png" alt="editorconfig" width="44"><br> EditorConfig </div> | <div align="center"><img src="/images/libs/lefthook.png" alt="lefthook" width="44"><br> lefthook</div> | <div align="center"><img src="/images/libs/docker.png" alt="docker" width="44"><br> Docker </div> |
| | | | |
| **Testing** | <div align="center"><img src="/images/libs/vitest.png" alt="vitest" width="44"><br> Vitest</div> | <div align="center"><img src="/images/libs/testing-library.png" alt="testing-library" width="44"><br> Testing Library</div> | <div align="center"><img src="/images/libs/playwright.png" alt="playwright" width="44"><br> Playwright</div> | <div align="center"><img src="/images/libs/testcontainers.png" alt="testcontainers" width="44"><br> Testcontainers</div> |
| | | |
| **Others** | <div align="center"><img src="/images/libs/github-actions.png" alt="actions" width="44"><br> GitHub Actions</div> | <div align="center"><img src="/images/libs/renovate.png" alt="renovate" width="44"><br> Renovate</div> | <div align="center"><img src="/images/libs/vscode.png" alt="vscode" width="44"><br> VSCode</div> | <div align="center"><img src="/images/libs/copilot.png" alt="copilot edits" width="44"><br> Copilot Edits</div> |
Binary file added .internal/site/src/public/images/libs/knip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 31 additions & 3 deletions .internal/tests/Basetest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export class BaseTest {
process.env.LOCAL_FROM_PATH = resolvePath(process.cwd(), "../..");
}

// for local debug
// const child = exec("DEBUG=true create-app-foundation");
const child = exec("npx create-app-foundation@latest");
const child =
process.env.IS_LOCAL === "false"
? exec("npx create-app-foundation@latest")
: exec("DEBUG=true create-app-foundation");

child.stdout.on("data", (data) => {
function debug(message) {
Expand Down Expand Up @@ -149,6 +150,12 @@ export class BaseTest {
) {
return false;
}
if (
dirent.parentPath.startsWith(`${this.outputDir}/.git`) &&
!dirent.parentPath.startsWith(`${this.outputDir}/.github`)
) {
return false;
}

return true;
}
Expand Down Expand Up @@ -224,6 +231,16 @@ export class BaseTest {
});
}

async testLint() {
test("should test lint", async (t) => {
await execAsync("npm run lint", {
cwd: this.outputPath,
});

t.assert.ok(true);
});
}

async testBuild(hasE2e = true) {
const command = !hasE2e ? "npm run build" : "npm run build:test";

Expand All @@ -245,6 +262,7 @@ export class BaseTest {
t.assert.ok(true);
});
}

async testE2e() {
test("should test e2e", async (t) => {
await execAsync("npm run test:e2e", {
Expand All @@ -255,6 +273,16 @@ export class BaseTest {
});
}

async allTests({ hasE2e }) {
await this.testBuild(hasE2e);

await Promise.all([this.testLint(), this.testUnit()]);

if (hasE2e) {
await this.testE2e(hasE2e);
}
}

async getFileContent(filePath) {
return await readFile(join(this.outputPath, filePath), "utf-8");
}
Expand Down
2 changes: 1 addition & 1 deletion .internal/tests/all-opt-out.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ describe("all-opt-out", async () => {
noOtel: true,
});

await baseTest.testBuild(false);
await baseTest.allTests({ hasE2e: false });
});
1 change: 1 addition & 0 deletions .internal/tests/common.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ describe("common", async () => {
]);

await baseTest.testFileContent(modifiedAllFiles);
await baseTest.allTests({ hasE2e: true });
});
Loading

0 comments on commit 7448d36

Please sign in to comment.