-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathindex.ts
118 lines (101 loc) · 3.11 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
import path from "path";
import { execa } from "execa";
import fs from "fs-extra";
import { type PackageJson } from "type-fest";
import { runCli } from "~/cli/index.js";
import { createProject } from "~/helpers/createProject.js";
import { initializeGit } from "~/helpers/git.js";
import { logNextSteps } from "~/helpers/logNextSteps.js";
import { setImportAlias } from "~/helpers/setImportAlias.js";
import { buildPkgInstallerMap } from "~/installers/index.js";
import { getUserPkgManager } from "~/utils/getUserPkgManager.js";
import { logger } from "~/utils/logger.js";
import { parseNameAndPath } from "~/utils/parseNameAndPath.js";
import { renderTitle } from "~/utils/renderTitle.js";
import { installDependencies } from "./helpers/installDependencies.js";
import { moveProjectSrc } from "./helpers/moveProjectSrc.js";
import { getVersion } from "./utils/getT3Version.js";
import {
getNpmVersion,
renderVersionWarning,
} from "./utils/renderVersionWarning.js";
type CT3APackageJSON = PackageJson & {
ct3aMetadata?: {
initVersion: string;
};
};
const main = async () => {
const npmVersion = await getNpmVersion();
const pkgManager = getUserPkgManager();
renderTitle();
npmVersion && renderVersionWarning(npmVersion);
const {
appName,
packages,
flags: { noGit, noInstall, importAlias, appRouter, srcDir },
databaseProvider,
} = await runCli();
const usePackages = buildPkgInstallerMap(packages, databaseProvider);
// e.g. dir/@mono/app returns ["@mono/app", "dir/app"]
const [scopedAppName, appDir] = parseNameAndPath(appName);
const projectDir = await createProject({
projectName: appDir,
scopedAppName,
packages: usePackages,
databaseProvider,
importAlias,
noInstall,
appRouter,
});
// Write name to package.json
const pkgJson = fs.readJSONSync(
path.join(projectDir, "package.json")
) as CT3APackageJSON;
pkgJson.name = scopedAppName;
pkgJson.ct3aMetadata = { initVersion: getVersion() };
// ? Bun doesn't support this field (yet)
if (pkgManager !== "bun") {
const { stdout } = await execa(pkgManager, ["-v"], {
cwd: projectDir,
});
pkgJson.packageManager = `${pkgManager}@${stdout.trim()}`;
}
fs.writeJSONSync(path.join(projectDir, "package.json"), pkgJson, {
spaces: 2,
});
if (!srcDir) {
await moveProjectSrc({ projectDir, packages: usePackages, appRouter });
}
// update import alias in any generated files if not using the default
if (importAlias !== "~/") {
setImportAlias(projectDir, importAlias);
}
if (!noInstall) {
await installDependencies({ projectDir });
}
if (!noGit) {
await initializeGit(projectDir);
}
await logNextSteps({
projectName: appDir,
packages: usePackages,
appRouter,
noInstall,
projectDir,
databaseProvider,
});
process.exit(0);
};
main().catch((err) => {
logger.error("Aborting installation...");
if (err instanceof Error) {
logger.error(err);
} else {
logger.error(
"An unknown error has occurred. Please open an issue on github with the below:"
);
console.log(err);
}
process.exit(1);
});