Skip to content

Commit

Permalink
Merge monorepo changes (#1)
Browse files Browse the repository at this point in the history
* feat: Add Next.js codebase scanning and server action generation
- Add Next.js specific types, scanner, and generator

* feat: add type support for jest

- Add new function `analyzeNextjsSourceFiles` to handle the source files
- Update `scanNextjsCodebase` to call `analyzeNextjsSourceFiles` and return the result
- Add tests

* chore: bump version
* switch to bun and update CI
* chore: Update ESLint configuration and dependencies
* chore: Update dependencies and add TypeScript support for Jest
  • Loading branch information
ozhanefemeral authored Jul 24, 2024
1 parent 4ac1682 commit ae4b8ad
Show file tree
Hide file tree
Showing 22 changed files with 332 additions and 88 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-boats-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ozhanefe/ts-codegenerator": minor
---

add eslint config
5 changes: 5 additions & 0 deletions .changeset/tame-worms-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ozhanefe/ts-codegenerator": minor
---

nextjs support
5 changes: 5 additions & 0 deletions .changeset/warm-pots-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ozhanefe/ts-codegenerator": minor
---

add nextjs parsing/generator features
37 changes: 37 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { resolve } = require("node:path");

const project = resolve(process.cwd(), "tsconfig.json");

/** @type {import("eslint").Linter.Config} */
module.exports = {
root: true,
extends: ["eslint:recommended", "prettier"],
plugins: ["only-warn", "@typescript-eslint"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
tsconfigRootDir: __dirname,
},
env: {
node: true,
},
settings: {
"import/resolver": {
typescript: {
project,
},
},
},
ignorePatterns: [".*.js", "node_modules/", "dist/"],
overrides: [
{
files: ["*.js?(x)", "*.ts?(x)"],
},
{
files: ["**/*.test.ts", "**/*.test.tsx"],
env: {
jest: true,
},
},
],
};
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
branches:
- "**"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- uses: actions/setup-node@v3
with:
node-version: 18.x

- name: Install dependencies
run: bun install

- name: Lint
run: bun run lint

- name: Build
run: bun run build

- name: Test
run: bun run test
21 changes: 0 additions & 21 deletions .github/workflows/main.yml

This file was deleted.

36 changes: 0 additions & 36 deletions .github/workflows/publish.yml

This file was deleted.

36 changes: 36 additions & 0 deletions .github/workflows/release-ts-codegenerator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Release ts-codegenerator

on:
push:
branches:
- main
paths:
- "packages/ts-generator/**"

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0

- name: Setup Bun
uses: oven-sh/setup-bun@v1

- name: Install Dependencies
run: bun install

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
with:
# This expects your changesets to be in the root packages/ts-generator/.changeset
cwd: ./packages/ts-generator
publish: bun run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
17 changes: 0 additions & 17 deletions .turbo/turbo-build.log

This file was deleted.

16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# @ozhanefe/ts-codegenerator

## 1.7.0

### Minor Changes

- add eslint config
- nextjs support
- add nextjs parsing/generator features

## 1.6.0

### Minor Changes

- add eslint config
- nextjs support
- add nextjs parsing/generator features

## 1.5.1

### Patch Changes
Expand Down
Binary file added bun.lockb
Binary file not shown.
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
{
"name": "@ozhanefe/ts-codegenerator",
"version": "1.7.0",
"license": "MIT",
"version": "1.5.1",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --minify --clean",
"release": "pnpm run build && changeset publish",
"lint": "tsc",
"test": "jest"
"lint": "eslint .",
"test": "jest",
"changeset": "changeset",
"version-packages": "changeset version",
"release": "bun run build && bun run lint && bun run test && changeset publish"
},
"devDependencies": {
"@changesets/cli": "^2.27.7",
"@jest/types": "^29.6.3",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.11",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-only-warn": "^1.1.0",
"jest": "^29.7.0",
"jest-mock-fs": "^1.0.2",
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2",
"tsup": "^8.1.2",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/codebase-scanner.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { scanCodebase } from "../codebase-scanner";
import { CodebaseInfo, FunctionInfo, TypeInfo } from "../types";
import * as path from "path";
import { scanCodebase } from "../codebase-scanner";
import { CodebaseInfo } from "../types";

describe("Codebase Scanner", () => {
it("should scan codebase", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/codebase-scanner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Project, Node, Type, Symbol as TsSymbol } from "ts-morph";
import { FunctionInfo, TypeInfo, CodebaseInfo } from "./types";
import { Node, Project } from "ts-morph";
import { CodebaseInfo, FunctionInfo, TypeInfo } from "./types";

export function scanCodebase(projectPath: string): CodebaseInfo {
const project = new Project();
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export type {
} from "./types";

export { scanCodebase } from "./codebase-scanner";

export type { NextCodebaseInfo, ServerActionInfo } from "./nextjs";
export { scanNextjsCodebase, generateServerAction } from "./nextjs";
export * as NextJS from "./nextjs";
2 changes: 1 addition & 1 deletion src/module-parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getFunctionInfoFromNode } from "./parser";
export { getFunctionInfoFromNode, parseFunctionsFromText } from "./parser";
export { getFunctionVariables } from "./utils";
96 changes: 96 additions & 0 deletions src/nextjs/__tests__/nextjs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Project } from "ts-morph";
import {
analyzeNextjsSourceFiles,
generateServerAction,
ServerActionInfo,
} from "../index";

describe("Next.js functionality", () => {
let project: Project;

beforeEach(() => {
project = new Project({ useInMemoryFileSystem: true });
});

describe("analyzeNextjsSourceFiles", () => {
it("should correctly identify and parse server actions", () => {
const serverActionCode = `
"use server";
export async function submitForm(data: FormData) {
// Server action logic
}
export async function deleteItem(id: string) {
// Delete item logic
}
`;

const sourceFile = project.createSourceFile(
"app/actions.ts",
serverActionCode
);

const result = analyzeNextjsSourceFiles([sourceFile]);

expect(result.serverActions).toHaveLength(2);
expect(result.serverActions[0]?.name).toBe("submitForm");
expect(result.serverActions[1]?.name).toBe("deleteItem");
});

it("should not identify non-server actions", () => {
const regularCode = `
export function regularFunction() {
// Regular function logic
}
`;

const sourceFile = project.createSourceFile(
"app/regular.ts",
regularCode
);

const result = analyzeNextjsSourceFiles([sourceFile]);

expect(result.serverActions).toHaveLength(0);
});
});

describe("generateServerAction", () => {
it("should generate correct server action code", () => {
const serverActionInfo: ServerActionInfo = {
name: "testAction",
returnType: "Promise<string>", // This could be 'string' or 'Promise<string>', the function should handle both
parameters: [
{ name: "data", type: "FormData" },
{ name: "userId", type: "string" },
],
filePath: "app/actions.ts",
};

const generatedCode = generateServerAction(serverActionInfo);

expect(generatedCode).toContain('"use server";');
expect(generatedCode).toContain(
"export async function testAction(data: FormData, userId: string): Promise<string>"
);
expect(generatedCode).toContain("// TODO: Implement server action logic");
expect(generatedCode).toContain('throw new Error("Not implemented");');
});

it("should handle non-Promise return types", () => {
const serverActionInfo: ServerActionInfo = {
name: "testAction",
returnType: "void",
parameters: [],
filePath: "app/actions.ts",
};

const generatedCode = generateServerAction(serverActionInfo);

expect(generatedCode).toContain(
"export async function testAction(): Promise<void>"
);
});
});
});
Loading

0 comments on commit ae4b8ad

Please sign in to comment.