Update readme.md to include project overview, setup instructions, directory structure, and usage with Docker for Node + TypeScript project.
Some checks failed
ci / build-test-pack (push) Has been cancelled
Some checks failed
ci / build-test-pack (push) Has been cancelled
This commit is contained in:
40
.gitea/workflows/ci.yml
Normal file
40
.gitea/workflows/ci.yml
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
name: ci
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ["**"]
|
||||||
|
pull_request:
|
||||||
|
branches: ["**"]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-test-pack:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: "20"
|
||||||
|
cache: npm
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci || npm i
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: npm test
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: npm run build
|
||||||
|
|
||||||
|
- name: Pack
|
||||||
|
run: npm pack
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: npm-package
|
||||||
|
path: "*.tgz"
|
||||||
|
|
||||||
|
|
||||||
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
*.tgz
|
||||||
|
npm-debug.log*
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
|
||||||
9
.idea/hello-world.iml
generated
Normal file
9
.idea/hello-world.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="corretto-11" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/hello-world.iml" filepath="$PROJECT_DIR$/.idea/hello-world.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
FROM node:20-alpine
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package.json package-lock.json* tsconfig.json ./
|
||||||
|
RUN npm install --no-audit --progress=false
|
||||||
|
|
||||||
|
COPY src ./src
|
||||||
|
|
||||||
|
RUN npm run build && npm pack
|
||||||
|
|
||||||
|
CMD ["node", "dist/index.js"]
|
||||||
|
|
||||||
|
|
||||||
22
package.json
Normal file
22
package.json
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "hello-world",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"private": false,
|
||||||
|
"description": "Node + TypeScript OOP test environment for Gitea build and pack",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"clean": "rimraf dist",
|
||||||
|
"build": "npm run clean && tsc -p tsconfig.json",
|
||||||
|
"test": "vitest run"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist",
|
||||||
|
"README.md"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=20"
|
||||||
|
},
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
|
||||||
51
readme.md
51
readme.md
@@ -0,0 +1,51 @@
|
|||||||
|
项目:用于在 Gitea 中测试 Node + TypeScript 项目的构建与打包(含 OOP 示例、CI、Docker)。
|
||||||
|
|
||||||
|
### 快速开始
|
||||||
|
|
||||||
|
1) 安装 Node 20(建议使用 nvm)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
nvm use || nvm install
|
||||||
|
```
|
||||||
|
|
||||||
|
2) 安装依赖并运行测试、构建与打包
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm i
|
||||||
|
npm test
|
||||||
|
npm run build
|
||||||
|
npm pack
|
||||||
|
```
|
||||||
|
|
||||||
|
生成的 npm 包形如:`hello-world-0.1.0.tgz`。
|
||||||
|
|
||||||
|
### 目录结构
|
||||||
|
|
||||||
|
- `src/`:TypeScript 源码,OOP 风格示例位于 `src/core/Greeter.ts`
|
||||||
|
- `tests/`:Vitest 单元测试
|
||||||
|
- `.gitea/workflows/ci.yml`:Gitea Actions 工作流(安装、测试、构建、打包、上传构件)
|
||||||
|
- `Dockerfile`:容器内构建与打包
|
||||||
|
|
||||||
|
### 本地使用(Docker)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker build -t node-oop-ci .
|
||||||
|
docker run --rm node-oop-ci
|
||||||
|
```
|
||||||
|
|
||||||
|
镜像在构建阶段会运行 `npm run build && npm pack`,启动时会执行编译产物 `dist/index.js`。
|
||||||
|
|
||||||
|
### 在 Gitea 上运行
|
||||||
|
|
||||||
|
将仓库推送到 Gitea,确保实例启用了 Actions,并且 Runner 提供了 `ubuntu-latest` 或兼容标签。
|
||||||
|
工作流位于 `.gitea/workflows/ci.yml`,包含以下阶段:
|
||||||
|
|
||||||
|
- Checkout 代码
|
||||||
|
- 安装 Node 20 与依赖
|
||||||
|
- 运行测试(Vitest)
|
||||||
|
- 构建(TypeScript 到 `dist/`)
|
||||||
|
- 打包(`npm pack`)
|
||||||
|
- 上传构件(若实例提供 `actions/upload-artifact` 镜像)
|
||||||
|
|
||||||
|
如你的 Runner 标签不同,可调整 `runs-on`。
|
||||||
|
|
||||||
|
|||||||
14
src/core/Greeter.ts
Normal file
14
src/core/Greeter.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
export class Greeter {
|
||||||
|
private readonly greetingPrefix: string;
|
||||||
|
|
||||||
|
constructor(greetingPrefix: string = "Hello") {
|
||||||
|
this.greetingPrefix = greetingPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public greet(name: string): string {
|
||||||
|
const sanitizedName = (name ?? "").trim() || "World";
|
||||||
|
return `${this.greetingPrefix}, ${sanitizedName}!`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
12
src/index.ts
Normal file
12
src/index.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { Greeter } from "./core/Greeter";
|
||||||
|
|
||||||
|
export { Greeter };
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
const greeter = new Greeter();
|
||||||
|
// Example run output
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(greeter.greet("Gitea"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
16
tests/Greeter.test.ts
Normal file
16
tests/Greeter.test.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { Greeter } from "../src/core/Greeter";
|
||||||
|
|
||||||
|
describe("Greeter", () => {
|
||||||
|
it("greets with default prefix", () => {
|
||||||
|
const greeter = new Greeter();
|
||||||
|
expect(greeter.greet("Alice")).toBe("Hello, Alice!");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("trims name and handles empty name", () => {
|
||||||
|
const greeter = new Greeter("Hi");
|
||||||
|
expect(greeter.greet(" ")).toBe("Hi, World!");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
15
tsconfig.json
Normal file
15
tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2020",
|
||||||
|
"module": "CommonJS",
|
||||||
|
"moduleResolution": "Node",
|
||||||
|
"declaration": true,
|
||||||
|
"outDir": "dist",
|
||||||
|
"rootDir": "src",
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"skipLibCheck": true
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user