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
/
create.ts
83 lines (69 loc) · 2.33 KB
/
create.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import fs from "fs/promises";
import chalk from "chalk";
import {
mustBeEmpty,
downloadAndExtractRepo,
formatProject,
installDependencies,
tryGitInit,
toJson,
} from "./utils";
import path from "path";
export interface CreateOptions {
typescript?: boolean;
devtool?: boolean;
}
export default async function create(
projectDirectory: string,
options: CreateOptions
): Promise<void> {
await fs.mkdir(projectDirectory, { recursive: true });
await mustBeEmpty(projectDirectory);
const example = `example${options.typescript ? "-ts" : ""}`;
console.log(
`\nDownloading template ${chalk.cyan(example)}. This might take a moment.\n`
);
await downloadAndExtractRepo(projectDirectory, {
username: "nbundle",
name: example,
branch: "main",
});
await formatProject(projectDirectory, options);
if ((await tryGitInit(projectDirectory)) && options.devtool) {
const pkgJsonPath = path.join(projectDirectory, "package.json");
const pkg = JSON.parse(await fs.readFile(pkgJsonPath, "utf8"));
if (pkg.devDependencies.husky) {
pkg.scripts.prepare = "husky install";
await fs.writeFile(pkgJsonPath, toJson(pkg), "utf8");
}
}
console.log("Installing packages. This might take up to a few minutes.");
await installDependencies(projectDirectory);
console.log();
const packageManager = "yarn";
const useYarn = packageManager === "yarn";
const name = path.basename(projectDirectory);
let cdPath: string;
if (path.join(process.cwd(), name) === projectDirectory) {
cdPath = name;
} else {
cdPath = projectDirectory;
}
console.log(
`${chalk.green("Success!")} Created ${chalk.blue(projectDirectory)}\n`
);
console.log("Inside that directory, you can run several commands:\n");
console.log(
chalk.cyan(` ${packageManager} ${useYarn ? "" : "run "}develop`)
);
console.log(" Starts the development server.\n");
console.log(chalk.cyan(` ${packageManager} ${useYarn ? "" : "run "}build`));
console.log(" Builds the app for production.\n");
console.log(chalk.cyan(` ${packageManager} preview`));
console.log(" Runs the built app in production mode.\n");
console.log("We suggest that you begin by typing:\n");
console.log(chalk.cyan(" cd"), cdPath);
console.log(
` ${chalk.cyan(`${packageManager} ${useYarn ? "" : "run "}develop`)}\n`
);
}