This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format-project.ts
57 lines (53 loc) · 1.61 KB
/
format-project.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import fs from "fs/promises";
import path from "path";
import fetch from "node-fetch";
import { customAlphabet } from "nanoid/async";
import { lowercase, numbers } from "nanoid-dictionary";
import { toJson } from "./to-json";
const nanoid = customAlphabet(lowercase + numbers, 5);
async function createAppName(): Promise<string> {
const name = `nbundle-app-${await nanoid()}`;
const res = await fetch(`https://developers.nbundle.com/api/v1/apps/${name}`);
if (res.status === 404) return name;
if (res.ok) return createAppName();
throw new Error("Couldn't generate app name.");
}
export async function formatProject(projectDirectory: string): Promise<void> {
await Promise.all(
[
".github",
".vscode",
".husky",
"commitlint.config.js",
"cspell.json",
"LICENSE",
"lint-staged.config.js",
".prettierignore",
"yarn.lock",
].map((file) =>
fs.rm(path.join(projectDirectory, file), { recursive: true })
)
);
const name = path.basename(projectDirectory);
const pkgJsonPath = path.join(projectDirectory, "package.json");
const pkg = JSON.parse(await fs.readFile(pkgJsonPath, "utf8"));
pkg.name = await createAppName();
pkg.productName = name;
pkg.devDependencies = Object.fromEntries(
Object.entries(pkg.devDependencies).filter(([key]) =>
key.startsWith("@nbundle/")
)
);
if (pkg.scripts.prepare) delete pkg.scripts.prepare;
await fs.writeFile(
pkgJsonPath,
toJson({
name: pkg.name,
version: pkg.version,
productName: pkg.productName,
description: pkg.description,
...pkg,
}),
"utf8"
);
}