Skip to content

Commit

Permalink
feat: init script
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy committed Feb 16, 2024
1 parent ff0d179 commit 71d0080
Show file tree
Hide file tree
Showing 4 changed files with 3,464 additions and 1,384 deletions.
36 changes: 14 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,44 +74,36 @@ Just running create-next-app does not satisfy the dependencies, development envi

## Setup

**Installing Docker Compose**

Please check [Installation scenarios](https://docs.docker.com/compose/install/) section.

**Enabling git hook and corepack**

```sh
$ npm run setup
```

**Creating `.env.local` and modifying env**
**Installing Deps**

```sh
$ cp .env.sample .env.local
$ pnpm i
```

**Creating DB migration files**
**Creating `.env.local` and modifying env**

```sh
$ pnpm dev:db:setup
$ cp .env.sample .env.local
```

**Removing the below code and committing migration files**

```diff
.gitignore
**Running init.mjs**

- ### 👉 please remove ###
- migrations
- ########################
```
- generating DB migration files
- removing unnecessary code
- updating name in package.json using directory name

```diff
.github/workflows/ci.yml

- ### 👉 please remove ###
- - run: cp ./.env.sample ./.env.local
- - run: pnpm dev:db:setup
- env:
- POSTGRES_URL: "postgresql://dev:1234@localhost:5432/dev?schema=public"
- NEXT_PUBLIC_SITE_URL: "http://localhost:3000"
- ########################
```sh
$ node init.mjs
```

## Dev
Expand Down
88 changes: 88 additions & 0 deletions init.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { spawn } from "node:child_process";
import { readFile, unlink, writeFile } from "node:fs/promises";
import { basename } from "node:path";

await Promise.all([removeLines(), commands(), updatePackageJson()]);
await unlink(new URL(import.meta.url).pathname);

await updatePackageJson();

async function removeLines() {
async function removeLinesFromFile(filePath, fence) {
console.log(`removing lines from: ${filePath}`);

try {
const data = await readFile(filePath, "utf8");
const lines = data.split("\n");
const res = [];
let isInFence = false;

for (const line of lines) {
if (line.trim() === fence[0]) {
isInFence = true;
}
if (!isInFence) {
res.push(line);
}
if (line.trim() === fence[1]) {
isInFence = false;
}
}

await writeFile(filePath, res.join("\n"));
} catch (error) {
console.error(error);
}
}

const fences = [["### 👉 please remove ###", "########################"]];
const files = [
[".gitignore", fences[0]],
[".github/workflows/ci.yml", fences[0]],
];

await Promise.all(
files.map(async ([file, fence]) => await removeLinesFromFile(file, fence)),
);
}

async function commands() {
const commands = ["pnpm dev:db:setup", "pnpm fmt", "pnpm db:stop"];

for (const command of commands) {
console.log(`running: ${command}`);

const [cmd, ...args] = command.split(" ");

await new Promise((resolve, reject) => {
const child = spawn(cmd, args, { stdio: "inherit" });

child.stdout?.on("data", (data) => {
console.log(data);
});

child.stderr?.on("data", (data) => {
console.log(data);
});

child.on("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`command failed with code ${code}`));
}
});
});
}
}

async function updatePackageJson() {
const packageJsonPath = new URL("./package.json", import.meta.url);
const packageJson = await readFile(packageJsonPath, "utf8");
const parsed = JSON.parse(packageJson);
const currentDirectoryName = basename(process.cwd());

parsed.name = currentDirectoryName;

await writeFile(packageJsonPath, JSON.stringify(parsed, null, 2));
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "web-app-template",
"name": "",
"version": "0.0.0",
"description": "",
"packageManager": "[email protected]",
Expand All @@ -24,7 +24,7 @@
"build": "next build",
"start": "next start",
"lint": "next lint && biome lint .",
"fmt": "biome format --write .",
"fmt": "prettier -w . && biome format --write .",
"check": "biome check --apply ."
},
"dependencies": {
Expand Down
Loading

0 comments on commit 71d0080

Please sign in to comment.