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.
- package/lib/executable/TypiaSetupWizard.js +48 -25
- package/lib/executable/TypiaSetupWizard.js.map +1 -1
- package/lib/executable/setup/PackageManager.js +12 -12
- package/lib/executable/setup/PackageManager.js.map +1 -1
- package/package.json +2 -1
- package/src/executable/TypiaGenerateWizard.ts +83 -83
- package/src/executable/TypiaPatchWizard.ts +42 -42
- package/src/executable/TypiaSetupWizard.ts +160 -151
- package/src/executable/setup/ArgumentParser.ts +43 -43
- package/src/executable/setup/CommandExecutor.ts +8 -8
- package/src/executable/setup/FileRetriever.ts +22 -22
- package/src/executable/setup/PackageManager.ts +86 -86
- package/src/executable/setup/PluginConfigurator.ts +69 -69
- package/src/executable/typia.ts +55 -55
|
@@ -1,151 +1,160 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
console.log("
|
|
17
|
-
console.log("
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
pack.install({ dev: true, modulo: "
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
data.scripts.prepare
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
data.scripts.prepare.indexOf("
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"ts-patch install
|
|
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
|
-
command.option("--
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
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
|
-
str.substring(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public
|
|
30
|
-
const
|
|
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
|
-
}
|
|
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>;
|