typia 6.10.0-dev.20240910 → 6.10.0-dev.20240910-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,151 +1,160 @@
1
- import fs from "fs";
2
-
3
- import { ArgumentParser } from "./setup/ArgumentParser";
4
- import { CommandExecutor } from "./setup/CommandExecutor";
5
- import { PackageManager } from "./setup/PackageManager";
6
- import { PluginConfigurator } from "./setup/PluginConfigurator";
7
-
8
- export namespace TypiaSetupWizard {
9
- export interface IArguments {
10
- manager: "npm" | "pnpm" | "yarn" | "bun";
11
- project: string | null;
12
- }
13
-
14
- export async function setup(): Promise<void> {
15
- console.log("----------------------------------------");
16
- console.log(" Typia Setup Wizard");
17
- console.log("----------------------------------------");
18
-
19
- // PREPARE ASSETS
20
- const pack: PackageManager = await PackageManager.mount();
21
- const args: IArguments = await ArgumentParser.parse(pack)(inquiry);
22
-
23
- // INSTALL TYPESCRIPT COMPILERS
24
- pack.install({ dev: true, modulo: "typescript", version: "5.5.2" });
25
- pack.install({ dev: true, modulo: "ts-patch", version: "latest" });
26
- args.project ??= (() => {
27
- const runner: string = pack.manager === "npm" ? "npx" : pack.manager;
28
- CommandExecutor.run(`${runner} tsc --init`);
29
- return (args.project = "tsconfig.json");
30
- })();
31
-
32
- // SETUP TRANSFORMER
33
- await pack.save((data) => {
34
- // COMPOSE PREPARE COMMAND
35
- data.scripts ??= {};
36
- if (
37
- typeof data.scripts.prepare === "string" &&
38
- data.scripts.prepare.trim().length
39
- ) {
40
- if (
41
- data.scripts.prepare.indexOf("ts-patch install") === -1 &&
42
- data.scripts.prepare.indexOf("typia patch") === -1
43
- )
44
- data.scripts.prepare =
45
- "ts-patch install && typia patch && " + data.scripts.prepare;
46
- else if (data.scripts.prepare.indexOf("ts-patch install") === -1)
47
- data.scripts.prepare = "ts-patch install && " + data.scripts.prepare;
48
- else if (data.scripts.prepare.indexOf("typia patch") === -1)
49
- data.scripts.prepare = data.scripts.prepare.replace(
50
- "ts-patch install",
51
- "ts-patch install && typia patch",
52
- );
53
- } else data.scripts.prepare = "ts-patch install && typia patch";
54
-
55
- // FOR OLDER VERSIONS
56
- if (typeof data.scripts.postinstall === "string") {
57
- data.scripts.postinstall = data.scripts.postinstall
58
- .split("&&")
59
- .map((str) => str.trim())
60
- .filter((str) => str.indexOf("ts-patch install") === -1)
61
- .join(" && ");
62
- if (data.scripts.postinstall.length === 0)
63
- delete data.scripts.postinstall;
64
- }
65
- });
66
-
67
- // CONFIGURE TYPIA
68
- await PluginConfigurator.configure(args);
69
- CommandExecutor.run(`${pack.manager} run prepare`);
70
- }
71
-
72
- const inquiry: ArgumentParser.Inquiry<IArguments> = async (
73
- pack,
74
- command,
75
- prompt,
76
- action,
77
- ) => {
78
- // PREPARE ASSETS
79
- command.option("--manager [manager", "package manager");
80
- command.option("--project [project]", "tsconfig.json file location");
81
-
82
- // INTERNAL PROCEDURES
83
- const questioned = { value: false };
84
- const select =
85
- (name: string) =>
86
- (message: string) =>
87
- async <Choice extends string>(
88
- choices: Choice[],
89
- filter?: (choice: string) => Choice,
90
- ): Promise<Choice> => {
91
- questioned.value = true;
92
- return (
93
- await prompt()({
94
- type: "list",
95
- name: name,
96
- message: message,
97
- choices: choices,
98
- ...(filter
99
- ? {
100
- filter,
101
- }
102
- : {}),
103
- })
104
- )[name];
105
- };
106
- const configure = async (): Promise<string | null> => {
107
- const fileList: string[] = await (
108
- await fs.promises.readdir(process.cwd())
109
- )
110
- .filter(
111
- (str) =>
112
- str.substring(0, 8) === "tsconfig" &&
113
- str.substring(str.length - 5) === ".json",
114
- )
115
- .sort((x, y) =>
116
- x === "tsconfig.json"
117
- ? -1
118
- : y === "tsconfig.json"
119
- ? 1
120
- : x < y
121
- ? -1
122
- : 1,
123
- );
124
- if (fileList.length === 0) {
125
- if (process.cwd() !== pack.directory)
126
- throw new URIError(`Unable to find "tsconfig.json" file.`);
127
- return null;
128
- } else if (fileList.length === 1) return fileList[0]!;
129
- return select("tsconfig")("TS Config File")(fileList);
130
- };
131
-
132
- // DO CONSTRUCT
133
- return action(async (options) => {
134
- pack.manager = options.manager ??= await select("manager")(
135
- "Package Manager",
136
- )(
137
- [
138
- "npm" as const,
139
- "pnpm" as const,
140
- "bun" as const,
141
- "yarn (berry is not supported)" as "yarn",
142
- ],
143
- (value) => value.split(" ")[0] as "yarn",
144
- );
145
- options.project ??= await configure();
146
-
147
- if (questioned.value) console.log("");
148
- return options as IArguments;
149
- });
150
- };
151
- }
1
+ import fs from "fs";
2
+ import { DetectResult, detect } from "package-manager-detector";
3
+
4
+ import { ArgumentParser } from "./setup/ArgumentParser";
5
+ import { CommandExecutor } from "./setup/CommandExecutor";
6
+ import { PackageManager } from "./setup/PackageManager";
7
+ import { PluginConfigurator } from "./setup/PluginConfigurator";
8
+
9
+ export namespace TypiaSetupWizard {
10
+ export interface IArguments {
11
+ manager: "npm" | "pnpm" | "yarn" | "bun";
12
+ project: string | null;
13
+ }
14
+
15
+ export async function setup(): Promise<void> {
16
+ console.log("----------------------------------------");
17
+ console.log(" Typia Setup Wizard");
18
+ console.log("----------------------------------------");
19
+
20
+ // PREPARE ASSETS
21
+ const pack: PackageManager = await PackageManager.mount();
22
+ const args: IArguments = await ArgumentParser.parse(pack)(inquiry);
23
+
24
+ // INSTALL TYPESCRIPT COMPILERS
25
+ pack.install({ dev: true, modulo: "typescript", version: "5.5.2" });
26
+ pack.install({ dev: true, modulo: "ts-patch", version: "latest" });
27
+ args.project ??= (() => {
28
+ const runner: string = pack.manager === "npm" ? "npx" : pack.manager;
29
+ CommandExecutor.run(`${runner} tsc --init`);
30
+ return (args.project = "tsconfig.json");
31
+ })();
32
+
33
+ // SETUP TRANSFORMER
34
+ await pack.save((data) => {
35
+ // COMPOSE PREPARE COMMAND
36
+ data.scripts ??= {};
37
+ if (
38
+ typeof data.scripts.prepare === "string" &&
39
+ data.scripts.prepare.trim().length
40
+ ) {
41
+ if (
42
+ data.scripts.prepare.indexOf("ts-patch install") === -1 &&
43
+ data.scripts.prepare.indexOf("typia patch") === -1
44
+ )
45
+ data.scripts.prepare =
46
+ "ts-patch install && typia patch && " + data.scripts.prepare;
47
+ else if (data.scripts.prepare.indexOf("ts-patch install") === -1)
48
+ data.scripts.prepare = "ts-patch install && " + data.scripts.prepare;
49
+ else if (data.scripts.prepare.indexOf("typia patch") === -1)
50
+ data.scripts.prepare = data.scripts.prepare.replace(
51
+ "ts-patch install",
52
+ "ts-patch install && typia patch",
53
+ );
54
+ } else data.scripts.prepare = "ts-patch install && typia patch";
55
+
56
+ // FOR OLDER VERSIONS
57
+ if (typeof data.scripts.postinstall === "string") {
58
+ data.scripts.postinstall = data.scripts.postinstall
59
+ .split("&&")
60
+ .map((str) => str.trim())
61
+ .filter((str) => str.indexOf("ts-patch install") === -1)
62
+ .join(" && ");
63
+ if (data.scripts.postinstall.length === 0)
64
+ delete data.scripts.postinstall;
65
+ }
66
+ });
67
+
68
+ // CONFIGURE TYPIA
69
+ await PluginConfigurator.configure(args);
70
+ CommandExecutor.run(`${pack.manager} run prepare`);
71
+ }
72
+
73
+ const inquiry: ArgumentParser.Inquiry<IArguments> = async (
74
+ pack,
75
+ command,
76
+ prompt,
77
+ action,
78
+ ) => {
79
+ // PREPARE ASSETS
80
+ command.option("--manager [manager", "package manager");
81
+ command.option("--project [project]", "tsconfig.json file location");
82
+
83
+ // INTERNAL PROCEDURES
84
+ const questioned = { value: false };
85
+ const select =
86
+ (name: string) =>
87
+ (message: string) =>
88
+ async <Choice extends string>(
89
+ choices: Choice[],
90
+ filter?: (choice: string) => Choice,
91
+ ): Promise<Choice> => {
92
+ questioned.value = true;
93
+ return (
94
+ await prompt()({
95
+ type: "list",
96
+ name: name,
97
+ message: message,
98
+ choices: choices,
99
+ ...(filter
100
+ ? {
101
+ filter,
102
+ }
103
+ : {}),
104
+ })
105
+ )[name];
106
+ };
107
+ const configure = async (): Promise<string | null> => {
108
+ const fileList: string[] = await (
109
+ await fs.promises.readdir(process.cwd())
110
+ )
111
+ .filter(
112
+ (str) =>
113
+ str.substring(0, 8) === "tsconfig" &&
114
+ str.substring(str.length - 5) === ".json",
115
+ )
116
+ .sort((x, y) =>
117
+ x === "tsconfig.json"
118
+ ? -1
119
+ : y === "tsconfig.json"
120
+ ? 1
121
+ : x < y
122
+ ? -1
123
+ : 1,
124
+ );
125
+ if (fileList.length === 0) {
126
+ if (process.cwd() !== pack.directory)
127
+ throw new URIError(`Unable to find "tsconfig.json" file.`);
128
+ return null;
129
+ } else if (fileList.length === 1) return fileList[0]!;
130
+ return select("tsconfig")("TS Config File")(fileList);
131
+ };
132
+
133
+ // DO CONSTRUCT
134
+ return action(async (options) => {
135
+ pack.manager = options.manager ??=
136
+ (await detectManager()) ??
137
+ (await select("manager")("Package Manager")(
138
+ [
139
+ "npm" as const,
140
+ "pnpm" as const,
141
+ "bun" as const,
142
+ "yarn (berry is not supported)" as "yarn",
143
+ ],
144
+ (value) => value.split(" ")[0] as "yarn",
145
+ ));
146
+ options.project ??= await configure();
147
+
148
+ if (questioned.value) console.log("");
149
+ return options as IArguments;
150
+ });
151
+ };
152
+
153
+ const detectManager = async (): Promise<
154
+ "npm" | "pnpm" | "yarn" | "bun" | null
155
+ > => {
156
+ const result: DetectResult | null = await detect({ cwd: process.cwd() });
157
+ if (result?.name === "npm") return null; // NPM case is still selectable
158
+ return result?.name ?? null;
159
+ };
160
+ }
@@ -1,43 +1,43 @@
1
- import commander from "commander";
2
- import inquirer from "inquirer";
3
-
4
- import { PackageManager } from "./PackageManager";
5
-
6
- export namespace ArgumentParser {
7
- export type Inquiry<T> = (
8
- pack: PackageManager,
9
- command: commander.Command,
10
- prompt: (opt?: inquirer.StreamOptions) => inquirer.PromptModule,
11
- action: (closure: (options: Partial<T>) => Promise<T>) => Promise<T>,
12
- ) => Promise<T>;
13
-
14
- export const parse =
15
- (pack: PackageManager) =>
16
- async <T>(
17
- inquiry: (
18
- pack: PackageManager,
19
- command: commander.Command,
20
- prompt: (opt?: inquirer.StreamOptions) => inquirer.PromptModule,
21
- action: (closure: (options: Partial<T>) => Promise<T>) => Promise<T>,
22
- ) => Promise<T>,
23
- ): Promise<T> => {
24
- // TAKE OPTIONS
25
- const action = (closure: (options: Partial<T>) => Promise<T>) =>
26
- new Promise<T>((resolve, reject) => {
27
- commander.program.action(async (options) => {
28
- try {
29
- resolve(await closure(options));
30
- } catch (exp) {
31
- reject(exp);
32
- }
33
- });
34
- commander.program.parseAsync().catch(reject);
35
- });
36
- return inquiry(
37
- pack,
38
- commander.program,
39
- inquirer.createPromptModule,
40
- action,
41
- );
42
- };
43
- }
1
+ import commander from "commander";
2
+ import inquirer from "inquirer";
3
+
4
+ import { PackageManager } from "./PackageManager";
5
+
6
+ export namespace ArgumentParser {
7
+ export type Inquiry<T> = (
8
+ pack: PackageManager,
9
+ command: commander.Command,
10
+ prompt: (opt?: inquirer.StreamOptions) => inquirer.PromptModule,
11
+ action: (closure: (options: Partial<T>) => Promise<T>) => Promise<T>,
12
+ ) => Promise<T>;
13
+
14
+ export const parse =
15
+ (pack: PackageManager) =>
16
+ async <T>(
17
+ inquiry: (
18
+ pack: PackageManager,
19
+ command: commander.Command,
20
+ prompt: (opt?: inquirer.StreamOptions) => inquirer.PromptModule,
21
+ action: (closure: (options: Partial<T>) => Promise<T>) => Promise<T>,
22
+ ) => Promise<T>,
23
+ ): Promise<T> => {
24
+ // TAKE OPTIONS
25
+ const action = (closure: (options: Partial<T>) => Promise<T>) =>
26
+ new Promise<T>((resolve, reject) => {
27
+ commander.program.action(async (options) => {
28
+ try {
29
+ resolve(await closure(options));
30
+ } catch (exp) {
31
+ reject(exp);
32
+ }
33
+ });
34
+ commander.program.parseAsync().catch(reject);
35
+ });
36
+ return inquiry(
37
+ pack,
38
+ commander.program,
39
+ inquirer.createPromptModule,
40
+ action,
41
+ );
42
+ };
43
+ }
@@ -1,8 +1,8 @@
1
- import cp from "child_process";
2
-
3
- export namespace CommandExecutor {
4
- export const run = (str: string): void => {
5
- console.log(`\n$ ${str}`);
6
- cp.execSync(str, { stdio: "inherit" });
7
- };
8
- }
1
+ import cp from "child_process";
2
+
3
+ export namespace CommandExecutor {
4
+ export const run = (str: string): void => {
5
+ console.log(`\n$ ${str}`);
6
+ cp.execSync(str, { stdio: "inherit" });
7
+ };
8
+ }
@@ -1,22 +1,22 @@
1
- import fs from "fs";
2
- import path from "path";
3
-
4
- export namespace FileRetriever {
5
- export const directory =
6
- (name: string) =>
7
- (dir: string, depth: number = 0): string | null => {
8
- const location: string = path.join(dir, name);
9
- if (fs.existsSync(location)) return dir;
10
- else if (depth > 2) return null;
11
- return directory(name)(path.join(dir, ".."), depth + 1);
12
- };
13
-
14
- export const file =
15
- (name: string) =>
16
- (directory: string, depth: number = 0): string | null => {
17
- const location: string = path.join(directory, name);
18
- if (fs.existsSync(location)) return location;
19
- else if (depth > 2) return null;
20
- return file(name)(path.join(directory, ".."), depth + 1);
21
- };
22
- }
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ export namespace FileRetriever {
5
+ export const directory =
6
+ (name: string) =>
7
+ (dir: string, depth: number = 0): string | null => {
8
+ const location: string = path.join(dir, name);
9
+ if (fs.existsSync(location)) return dir;
10
+ else if (depth > 2) return null;
11
+ return directory(name)(path.join(dir, ".."), depth + 1);
12
+ };
13
+
14
+ export const file =
15
+ (name: string) =>
16
+ (directory: string, depth: number = 0): string | null => {
17
+ const location: string = path.join(directory, name);
18
+ if (fs.existsSync(location)) return location;
19
+ else if (depth > 2) return null;
20
+ return file(name)(path.join(directory, ".."), depth + 1);
21
+ };
22
+ }
@@ -1,86 +1,86 @@
1
- import fs from "fs";
2
- import path from "path";
3
-
4
- import { CommandExecutor } from "./CommandExecutor";
5
- import { FileRetriever } from "./FileRetriever";
6
-
7
- const managers = ["npm", "pnpm", "yarn", "bun"] as const;
8
- type Manager = (typeof managers)[number];
9
-
10
- const installCmdTable = {
11
- npm: "install",
12
- pnpm: "add",
13
- yarn: "add",
14
- bun: "add",
15
- } as const satisfies Record<Manager, string>;
16
- const devOptionTable = {
17
- npm: "--save-dev",
18
- pnpm: "--save-dev",
19
- yarn: "--dev",
20
- bun: "--dev",
21
- } as const satisfies Record<Manager, string>;
22
-
23
- export class PackageManager {
24
- public manager: Manager = "npm";
25
- public get file(): string {
26
- return path.join(this.directory, "package.json");
27
- }
28
-
29
- public static async mount(): Promise<PackageManager> {
30
- const location: string | null = await FileRetriever.directory(
31
- "package.json",
32
- )(process.cwd());
33
- if (location === null)
34
- throw new URIError(`Unable to find "package.json" file`);
35
-
36
- return new PackageManager(
37
- location,
38
- await this.load(path.join(location, "package.json")),
39
- );
40
- }
41
-
42
- public async save(modifier: (data: Package.Data) => void): Promise<void> {
43
- const content: string = await fs.promises.readFile(this.file, "utf8");
44
- this.data = JSON.parse(content);
45
- modifier(this.data);
46
-
47
- return fs.promises.writeFile(
48
- this.file,
49
- JSON.stringify(this.data, null, 2),
50
- "utf8",
51
- );
52
- }
53
-
54
- public install(props: {
55
- dev: boolean;
56
- modulo: string;
57
- version: string;
58
- }): boolean {
59
- const cmd = installCmdTable[this.manager];
60
- const option = props.dev ? devOptionTable[this.manager] : "";
61
- const middle: string = `${cmd} ${option}` as const;
62
- CommandExecutor.run(
63
- `${this.manager} ${middle} ${props.modulo}${
64
- props.version ? `@${props.version}` : ""
65
- }`,
66
- );
67
- return true;
68
- }
69
-
70
- private constructor(
71
- public readonly directory: string,
72
- public data: Package.Data,
73
- ) {}
74
-
75
- private static async load(file: string): Promise<Package.Data> {
76
- const content: string = await fs.promises.readFile(file, "utf8");
77
- return JSON.parse(content);
78
- }
79
- }
80
- export namespace Package {
81
- export interface Data {
82
- scripts?: Record<string, string>;
83
- dependencies?: Record<string, string>;
84
- devDependencies?: Record<string, string>;
85
- }
86
- }
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ import { CommandExecutor } from "./CommandExecutor";
5
+ import { FileRetriever } from "./FileRetriever";
6
+
7
+ const managers = ["npm", "pnpm", "yarn", "bun"] as const;
8
+ type Manager = (typeof managers)[number];
9
+
10
+ export class PackageManager {
11
+ public manager: Manager = "npm";
12
+ public get file(): string {
13
+ return path.join(this.directory, "package.json");
14
+ }
15
+
16
+ public static async mount(): Promise<PackageManager> {
17
+ const location: string | null = await FileRetriever.directory(
18
+ "package.json",
19
+ )(process.cwd());
20
+ if (location === null)
21
+ throw new URIError(`Unable to find "package.json" file`);
22
+
23
+ return new PackageManager(
24
+ location,
25
+ await this.load(path.join(location, "package.json")),
26
+ );
27
+ }
28
+
29
+ public async save(modifier: (data: Package.Data) => void): Promise<void> {
30
+ const content: string = await fs.promises.readFile(this.file, "utf8");
31
+ this.data = JSON.parse(content);
32
+ modifier(this.data);
33
+
34
+ return fs.promises.writeFile(
35
+ this.file,
36
+ JSON.stringify(this.data, null, 2),
37
+ "utf8",
38
+ );
39
+ }
40
+
41
+ public install(props: {
42
+ dev: boolean;
43
+ modulo: string;
44
+ version: string;
45
+ }): boolean {
46
+ const cmd = installCmdTable[this.manager];
47
+ const option = props.dev ? devOptionTable[this.manager] : "";
48
+ const middle: string = `${cmd} ${option}` as const;
49
+ CommandExecutor.run(
50
+ `${this.manager} ${middle} ${props.modulo}${
51
+ props.version ? `@${props.version}` : ""
52
+ }`,
53
+ );
54
+ return true;
55
+ }
56
+
57
+ private constructor(
58
+ public readonly directory: string,
59
+ public data: Package.Data,
60
+ ) {}
61
+
62
+ private static async load(file: string): Promise<Package.Data> {
63
+ const content: string = await fs.promises.readFile(file, "utf8");
64
+ return JSON.parse(content);
65
+ }
66
+ }
67
+ export namespace Package {
68
+ export interface Data {
69
+ scripts?: Record<string, string>;
70
+ dependencies?: Record<string, string>;
71
+ devDependencies?: Record<string, string>;
72
+ }
73
+ }
74
+
75
+ const installCmdTable = {
76
+ npm: "install",
77
+ pnpm: "add",
78
+ yarn: "add",
79
+ bun: "add",
80
+ } as const satisfies Record<Manager, string>;
81
+ const devOptionTable = {
82
+ npm: "--save-dev",
83
+ pnpm: "--save-dev",
84
+ yarn: "--dev",
85
+ bun: "--dev",
86
+ } as const satisfies Record<Manager, string>;