Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhaadmin authored Jan 28, 2025
0 parents commit 496b3dd
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# dependencies
node_modules

# sst
.sst

# tmp files
.#*

# env
.env*.local
.env

# opennext
.open-next

# misc
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 SST

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Monorepo Template

A template to create a monorepo SST v3 project. [Learn more](https://sst.dev/docs/set-up-a-monorepo).

## Get started

1. Use this template to [create your own repo](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template).

2. Clone the new repo.

```bash
git clone <REPO_URL> MY_APP
cd MY_APP
```

3. Rename the files in the project to the name of your app.

```bash
npx replace-in-file '/monorepo-template/g' 'MY_APP' '**/*.*' --verbose
```

4. Deploy!

```bash
npm install
npx sst deploy
```

5. Optionally, enable [_git push to deploy_](https://sst.dev/docs/console/#autodeploy).

## Usage

This template uses [npm Workspaces](https://docs.npmjs.com/cli/v8/using-npm/workspaces). It has 3 packages to start with and you can add more it.

1. `core/`

This is for any shared code. It's defined as modules. For example, there's the `Example` module.

```ts
export module Example {
export function hello() {
return "Hello, world!";
}
}
```

That you can use across other packages using.

```ts
import { Example } from "@aws-monorepo/core/example";

Example.hello();
```

We also have [Vitest](https://vitest.dev/) configured for testing this package with the `sst shell` CLI.

```bash
npm test
```

2. `functions/`

This is for your Lambda functions and it uses the `core` package as a local dependency.

3. `scripts/`

This is for any scripts that you can run on your SST app using the `sst shell` CLI and [`tsx`](https://www.npmjs.com/package/tsx). For example, you can run the example script using:

```bash
npm run shell src/example.ts
```

### Infrastructure

The `infra/` directory allows you to logically split the infrastructure of your app into separate files. This can be helpful as your app grows.

In the template, we have an `api.ts`, and `storage.ts`. These export the created resources. And are imported in the `sst.config.ts`.

---

**Join our community** [Discord](https://sst.dev/discord) | [YouTube](https://www.youtube.com/c/sst-dev) | [X.com](https://x.com/SST_dev)
7 changes: 7 additions & 0 deletions infra/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bucket } from "./storage";

export const myApi = new sst.aws.Function("MyApi", {
url: true,
link: [bucket],
handler: "packages/functions/src/api.handler"
});
1 change: 1 addition & 0 deletions infra/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const bucket = new sst.aws.Bucket("MyBucket");
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "monorepo-template",
"version": "0.0.0",
"workspaces": [
"packages/*"
],
"scripts": {},
"devDependencies": {
"@tsconfig/node22": "^22",
"typescript": "^5"
},
"dependencies": {
"sst": "^3"
}
}
20 changes: 20 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@monorepo-template/core",
"type": "module",
"version": "0.0.0",
"scripts": {
"test": "sst shell vitest"
},
"exports": {
"./*": [
"./src/*/index.ts",
"./src/*.ts"
]
},
"dependencies": {
"sst": "*"
},
"devDependencies": {
"vitest": "^2"
}
}
5 changes: 5 additions & 0 deletions packages/core/src/example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export module Example {
export function hello() {
return "Hello, world!";
}
}
8 changes: 8 additions & 0 deletions packages/core/src/example/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from "vitest";
import { Example } from "../";

test("Hello test", () => {
const expected = "Hello, world!";

expect(Example.hello()).toEqual(expected);
});
7 changes: 7 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Bundler",
},
}
12 changes: 12 additions & 0 deletions packages/functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@monorepo-template/functions",
"version": "0.0.0",
"type": "module",
"dependencies": {
"@monorepo-template/core": "*",
"sst": "*"
},
"devDependencies": {
"@types/aws-lambda": "^8"
}
}
10 changes: 10 additions & 0 deletions packages/functions/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Resource } from "sst";
import { Handler } from "aws-lambda";
import { Example } from "@monorepo-template/core/example";

export const handler: Handler = async (_event) => {
return {
statusCode: 200,
body: `${Example.hello()} Linked to ${Resource.MyBucket.name}.`,
};
};
7 changes: 7 additions & 0 deletions packages/functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Bundler",
},
}
16 changes: 16 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@monorepo-template/scripts",
"version": "0.0.0",
"type": "module",
"dependencies": {
"@monorepo-template/core": "*",
"sst": "*"
},
"scripts": {
"shell": "sst shell tsx"
},
"devDependencies": {
"@types/node": "^22",
"tsx": "^4"
}
}
4 changes: 4 additions & 0 deletions packages/scripts/src/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Resource } from "sst";
import { Example } from "@monorepo-template/core/example";

console.log(`${Example.hello()} Linked to ${Resource.MyBucket.name}.`);
7 changes: 7 additions & 0 deletions packages/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Bundler",
},
}
20 changes: 20 additions & 0 deletions sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
app(input) {
return {
name: "monorepo-template",
removal: input?.stage === "production" ? "retain" : "remove",
protect: ["production"].includes(input?.stage),
home: "aws",
};
},
async run() {
const storage = await import("./infra/storage");
await import("./infra/api");

return {
MyBucket: storage.bucket.name,
};
},
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 496b3dd

Please sign in to comment.