nestia 10.0.2 → 11.0.0-dev.20260305
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/LICENSE +21 -21
- package/README.md +93 -93
- package/package.json +25 -12
- package/src/NestiaSetupWizard.ts +90 -90
- package/src/NestiaStarter.ts +41 -41
- package/src/NestiaTemplate.ts +37 -37
- package/src/boot.js +15 -0
- package/src/index.ts +65 -64
- package/src/internal/ArgumentParser.ts +123 -123
- package/src/internal/CommandExecutor.ts +8 -8
- package/src/internal/FileRetriever.ts +22 -22
- package/src/internal/PackageManager.ts +91 -91
- package/src/internal/PluginConfigurator.ts +125 -125
- package/bin/NestiaSetupWizard.d.ts +0 -3
- package/bin/NestiaSetupWizard.js +0 -98
- package/bin/NestiaSetupWizard.js.map +0 -1
- package/bin/NestiaStarter.d.ts +0 -3
- package/bin/NestiaStarter.js +0 -49
- package/bin/NestiaStarter.js.map +0 -1
- package/bin/NestiaTemplate.d.ts +0 -3
- package/bin/NestiaTemplate.js +0 -46
- package/bin/NestiaTemplate.js.map +0 -1
- package/bin/index.d.ts +0 -4
- package/bin/index.js +0 -106
- package/bin/index.js.map +0 -1
- package/bin/internal/ArgumentParser.d.ts +0 -9
- package/bin/internal/ArgumentParser.js +0 -106
- package/bin/internal/ArgumentParser.js.map +0 -1
- package/bin/internal/CommandExecutor.d.ts +0 -3
- package/bin/internal/CommandExecutor.js +0 -16
- package/bin/internal/CommandExecutor.js.map +0 -1
- package/bin/internal/FileRetriever.d.ts +0 -4
- package/bin/internal/FileRetriever.js +0 -28
- package/bin/internal/FileRetriever.js.map +0 -1
- package/bin/internal/PackageManager.d.ts +0 -25
- package/bin/internal/PackageManager.js +0 -76
- package/bin/internal/PackageManager.js.map +0 -1
- package/bin/internal/PluginConfigurator.d.ts +0 -4
- package/bin/internal/PluginConfigurator.js +0 -99
- package/bin/internal/PluginConfigurator.js.map +0 -1
package/src/index.ts
CHANGED
|
@@ -1,64 +1,65 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
const USAGE = `Wrong command has been detected. Use like below:
|
|
3
|
-
|
|
4
|
-
npx nestia [command] [options?]
|
|
5
|
-
|
|
6
|
-
1. npx nestia start <directory>
|
|
7
|
-
2. npx nestia template <directory>
|
|
8
|
-
3. npx nestia setup
|
|
9
|
-
4. npx nestia dependencies
|
|
10
|
-
5. npx nestia init
|
|
11
|
-
6. npx nestia sdk
|
|
12
|
-
7. npx nestia swagger
|
|
13
|
-
9. npx nestia e2e
|
|
14
|
-
10. npx nestia all
|
|
15
|
-
`;
|
|
16
|
-
|
|
17
|
-
function halt(desc: string): never {
|
|
18
|
-
console.error(desc);
|
|
19
|
-
process.exit(-1);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async function main(): Promise<void> {
|
|
23
|
-
const type: string | undefined = process.argv[2];
|
|
24
|
-
const argv: string[] = process.argv.slice(3);
|
|
25
|
-
|
|
26
|
-
if (type === "start") {
|
|
27
|
-
await (
|
|
28
|
-
await import("./NestiaStarter")
|
|
29
|
-
).NestiaStarter.clone((msg) => halt(msg ?? USAGE))(argv);
|
|
30
|
-
} else if (type === "template") {
|
|
31
|
-
await (
|
|
32
|
-
await import("./NestiaTemplate")
|
|
33
|
-
).NestiaTemplate.clone((msg) => halt(msg ?? USAGE))(argv);
|
|
34
|
-
} else if (type === "setup") {
|
|
35
|
-
try {
|
|
36
|
-
await import("comment-json");
|
|
37
|
-
await import("inquirer");
|
|
38
|
-
await import("commander");
|
|
39
|
-
} catch {
|
|
40
|
-
halt(`nestia has not been installed. Run "npm i -D nestia" before.`);
|
|
41
|
-
}
|
|
42
|
-
await (await import("./NestiaSetupWizard")).NestiaSetupWizard.setup();
|
|
43
|
-
} else if (
|
|
44
|
-
type === "dependencies" ||
|
|
45
|
-
type === "init" ||
|
|
46
|
-
type === "sdk" ||
|
|
47
|
-
type === "swagger" ||
|
|
48
|
-
type === "e2e" ||
|
|
49
|
-
type === "all"
|
|
50
|
-
) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const USAGE = `Wrong command has been detected. Use like below:
|
|
3
|
+
|
|
4
|
+
npx nestia [command] [options?]
|
|
5
|
+
|
|
6
|
+
1. npx nestia start <directory>
|
|
7
|
+
2. npx nestia template <directory>
|
|
8
|
+
3. npx nestia setup
|
|
9
|
+
4. npx nestia dependencies
|
|
10
|
+
5. npx nestia init
|
|
11
|
+
6. npx nestia sdk
|
|
12
|
+
7. npx nestia swagger
|
|
13
|
+
9. npx nestia e2e
|
|
14
|
+
10. npx nestia all
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
function halt(desc: string): never {
|
|
18
|
+
console.error(desc);
|
|
19
|
+
process.exit(-1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function main(): Promise<void> {
|
|
23
|
+
const type: string | undefined = process.argv[2];
|
|
24
|
+
const argv: string[] = process.argv.slice(3);
|
|
25
|
+
|
|
26
|
+
if (type === "start") {
|
|
27
|
+
await (
|
|
28
|
+
await import("./NestiaStarter")
|
|
29
|
+
).NestiaStarter.clone((msg) => halt(msg ?? USAGE))(argv);
|
|
30
|
+
} else if (type === "template") {
|
|
31
|
+
await (
|
|
32
|
+
await import("./NestiaTemplate")
|
|
33
|
+
).NestiaTemplate.clone((msg) => halt(msg ?? USAGE))(argv);
|
|
34
|
+
} else if (type === "setup") {
|
|
35
|
+
try {
|
|
36
|
+
await import("comment-json");
|
|
37
|
+
await import("inquirer");
|
|
38
|
+
await import("commander");
|
|
39
|
+
} catch {
|
|
40
|
+
halt(`nestia has not been installed. Run "npm i -D nestia" before.`);
|
|
41
|
+
}
|
|
42
|
+
await (await import("./NestiaSetupWizard")).NestiaSetupWizard.setup();
|
|
43
|
+
} else if (
|
|
44
|
+
type === "dependencies" ||
|
|
45
|
+
type === "init" ||
|
|
46
|
+
type === "sdk" ||
|
|
47
|
+
type === "swagger" ||
|
|
48
|
+
type === "e2e" ||
|
|
49
|
+
type === "all"
|
|
50
|
+
) {
|
|
51
|
+
const location: string = "@nestia/sdk/lib/executable/sdk";
|
|
52
|
+
try {
|
|
53
|
+
require.resolve(location);
|
|
54
|
+
} catch {
|
|
55
|
+
halt(
|
|
56
|
+
`@nestia/sdk has not been installed. Run "npx nestia setup" before.`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
await import(location);
|
|
60
|
+
} else halt(USAGE);
|
|
61
|
+
}
|
|
62
|
+
main().catch((exp) => {
|
|
63
|
+
console.log(exp.message);
|
|
64
|
+
process.exit(-1);
|
|
65
|
+
});
|
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
import commander from "commander";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import inquirer from "inquirer";
|
|
4
|
-
import { DetectResult, detect } from "package-manager-detector";
|
|
5
|
-
|
|
6
|
-
import { PackageManager } from "./PackageManager";
|
|
7
|
-
|
|
8
|
-
export namespace ArgumentParser {
|
|
9
|
-
export interface IArguments {
|
|
10
|
-
manager: "npm" | "pnpm" | "yarn" | "bun";
|
|
11
|
-
project: string | null;
|
|
12
|
-
runtime: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export async function parse(pack: PackageManager): Promise<IArguments> {
|
|
16
|
-
// PREPARE ASSETS
|
|
17
|
-
commander.program.option("--manager [manager]", "package manager");
|
|
18
|
-
commander.program.option(
|
|
19
|
-
"--project [project]",
|
|
20
|
-
"tsconfig.json file location",
|
|
21
|
-
);
|
|
22
|
-
commander.program.option(
|
|
23
|
-
"--runtime [boolean]",
|
|
24
|
-
"transform runtime swagger",
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
// INTERNAL PROCEDURES
|
|
28
|
-
const questioned = { value: false };
|
|
29
|
-
const action = (
|
|
30
|
-
closure: (options: Partial<IArguments>) => Promise<IArguments>,
|
|
31
|
-
) => {
|
|
32
|
-
return new Promise<IArguments>((resolve, reject) => {
|
|
33
|
-
commander.program.action(async (options) => {
|
|
34
|
-
try {
|
|
35
|
-
resolve(await closure(options));
|
|
36
|
-
} catch (exp) {
|
|
37
|
-
reject(exp);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
commander.program.parseAsync().catch(reject);
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
const select =
|
|
44
|
-
(name: string) =>
|
|
45
|
-
(message: string) =>
|
|
46
|
-
async <Choice extends string>(
|
|
47
|
-
choices: Choice[],
|
|
48
|
-
filter?: (value: string) => Choice,
|
|
49
|
-
): Promise<Choice> => {
|
|
50
|
-
questioned.value = true;
|
|
51
|
-
return (
|
|
52
|
-
await inquirer.createPromptModule()({
|
|
53
|
-
type: "list",
|
|
54
|
-
name: name,
|
|
55
|
-
message: message,
|
|
56
|
-
choices: choices,
|
|
57
|
-
filter,
|
|
58
|
-
})
|
|
59
|
-
)[name];
|
|
60
|
-
};
|
|
61
|
-
const configure = async () => {
|
|
62
|
-
const fileList: string[] = await (
|
|
63
|
-
await fs.promises.readdir(process.cwd())
|
|
64
|
-
)
|
|
65
|
-
.filter(
|
|
66
|
-
(str) =>
|
|
67
|
-
str.substring(0, 8) === "tsconfig" &&
|
|
68
|
-
str.substring(str.length - 5) === ".json",
|
|
69
|
-
)
|
|
70
|
-
.sort((x, y) =>
|
|
71
|
-
x === "tsconfig.json"
|
|
72
|
-
? -1
|
|
73
|
-
: y === "tsconfig.json"
|
|
74
|
-
? 1
|
|
75
|
-
: x < y
|
|
76
|
-
? -1
|
|
77
|
-
: 1,
|
|
78
|
-
);
|
|
79
|
-
if (fileList.length === 0) {
|
|
80
|
-
if (process.cwd() !== pack.directory)
|
|
81
|
-
throw new Error(`Unable to find "tsconfig.json" file.`);
|
|
82
|
-
return null;
|
|
83
|
-
} else if (fileList.length === 1) return fileList[0];
|
|
84
|
-
return select("tsconfig")("TS Config File")(fileList);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
// DO CONSTRUCT
|
|
88
|
-
return action(async (options) => {
|
|
89
|
-
options.manager ??=
|
|
90
|
-
(await detectManager()) ??
|
|
91
|
-
(await select("manager")("Package Manager")(
|
|
92
|
-
[
|
|
93
|
-
"npm" as const,
|
|
94
|
-
"pnpm" as const,
|
|
95
|
-
"yarn (berry is not supported)" as "yarn",
|
|
96
|
-
"bun" as const,
|
|
97
|
-
],
|
|
98
|
-
(value) => value.split(" ")[0] as "yarn",
|
|
99
|
-
));
|
|
100
|
-
pack.manager = options.manager;
|
|
101
|
-
options.project ??= await configure();
|
|
102
|
-
options.runtime =
|
|
103
|
-
((options.runtime as string | undefined) ??
|
|
104
|
-
(await select("runtime")("Transform Runtime Swagger")([
|
|
105
|
-
"true",
|
|
106
|
-
"false",
|
|
107
|
-
]))) !== "false";
|
|
108
|
-
|
|
109
|
-
if (questioned.value) console.log("");
|
|
110
|
-
return options as IArguments;
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const detectManager = async (): Promise<
|
|
115
|
-
"npm" | "pnpm" | "yarn" | "bun" | null
|
|
116
|
-
> => {
|
|
117
|
-
const result: DetectResult | null = await detect({ cwd: process.cwd() });
|
|
118
|
-
if (result?.name === "npm")
|
|
119
|
-
return null; // NPM case is still selectable
|
|
120
|
-
else if (result?.name === "deno") return null; // Deno case is not supported
|
|
121
|
-
return result?.name ?? null;
|
|
122
|
-
};
|
|
123
|
-
}
|
|
1
|
+
import commander from "commander";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import inquirer from "inquirer";
|
|
4
|
+
import { DetectResult, detect } from "package-manager-detector";
|
|
5
|
+
|
|
6
|
+
import { PackageManager } from "./PackageManager";
|
|
7
|
+
|
|
8
|
+
export namespace ArgumentParser {
|
|
9
|
+
export interface IArguments {
|
|
10
|
+
manager: "npm" | "pnpm" | "yarn" | "bun";
|
|
11
|
+
project: string | null;
|
|
12
|
+
runtime: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function parse(pack: PackageManager): Promise<IArguments> {
|
|
16
|
+
// PREPARE ASSETS
|
|
17
|
+
commander.program.option("--manager [manager]", "package manager");
|
|
18
|
+
commander.program.option(
|
|
19
|
+
"--project [project]",
|
|
20
|
+
"tsconfig.json file location",
|
|
21
|
+
);
|
|
22
|
+
commander.program.option(
|
|
23
|
+
"--runtime [boolean]",
|
|
24
|
+
"transform runtime swagger",
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// INTERNAL PROCEDURES
|
|
28
|
+
const questioned = { value: false };
|
|
29
|
+
const action = (
|
|
30
|
+
closure: (options: Partial<IArguments>) => Promise<IArguments>,
|
|
31
|
+
) => {
|
|
32
|
+
return new Promise<IArguments>((resolve, reject) => {
|
|
33
|
+
commander.program.action(async (options) => {
|
|
34
|
+
try {
|
|
35
|
+
resolve(await closure(options));
|
|
36
|
+
} catch (exp) {
|
|
37
|
+
reject(exp);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
commander.program.parseAsync().catch(reject);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
const select =
|
|
44
|
+
(name: string) =>
|
|
45
|
+
(message: string) =>
|
|
46
|
+
async <Choice extends string>(
|
|
47
|
+
choices: Choice[],
|
|
48
|
+
filter?: (value: string) => Choice,
|
|
49
|
+
): Promise<Choice> => {
|
|
50
|
+
questioned.value = true;
|
|
51
|
+
return (
|
|
52
|
+
await inquirer.createPromptModule()({
|
|
53
|
+
type: "list",
|
|
54
|
+
name: name,
|
|
55
|
+
message: message,
|
|
56
|
+
choices: choices,
|
|
57
|
+
filter,
|
|
58
|
+
})
|
|
59
|
+
)[name];
|
|
60
|
+
};
|
|
61
|
+
const configure = async () => {
|
|
62
|
+
const fileList: string[] = await (
|
|
63
|
+
await fs.promises.readdir(process.cwd())
|
|
64
|
+
)
|
|
65
|
+
.filter(
|
|
66
|
+
(str) =>
|
|
67
|
+
str.substring(0, 8) === "tsconfig" &&
|
|
68
|
+
str.substring(str.length - 5) === ".json",
|
|
69
|
+
)
|
|
70
|
+
.sort((x, y) =>
|
|
71
|
+
x === "tsconfig.json"
|
|
72
|
+
? -1
|
|
73
|
+
: y === "tsconfig.json"
|
|
74
|
+
? 1
|
|
75
|
+
: x < y
|
|
76
|
+
? -1
|
|
77
|
+
: 1,
|
|
78
|
+
);
|
|
79
|
+
if (fileList.length === 0) {
|
|
80
|
+
if (process.cwd() !== pack.directory)
|
|
81
|
+
throw new Error(`Unable to find "tsconfig.json" file.`);
|
|
82
|
+
return null;
|
|
83
|
+
} else if (fileList.length === 1) return fileList[0];
|
|
84
|
+
return select("tsconfig")("TS Config File")(fileList);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// DO CONSTRUCT
|
|
88
|
+
return action(async (options) => {
|
|
89
|
+
options.manager ??=
|
|
90
|
+
(await detectManager()) ??
|
|
91
|
+
(await select("manager")("Package Manager")(
|
|
92
|
+
[
|
|
93
|
+
"npm" as const,
|
|
94
|
+
"pnpm" as const,
|
|
95
|
+
"yarn (berry is not supported)" as "yarn",
|
|
96
|
+
"bun" as const,
|
|
97
|
+
],
|
|
98
|
+
(value) => value.split(" ")[0] as "yarn",
|
|
99
|
+
));
|
|
100
|
+
pack.manager = options.manager;
|
|
101
|
+
options.project ??= await configure();
|
|
102
|
+
options.runtime =
|
|
103
|
+
((options.runtime as string | undefined) ??
|
|
104
|
+
(await select("runtime")("Transform Runtime Swagger")([
|
|
105
|
+
"true",
|
|
106
|
+
"false",
|
|
107
|
+
]))) !== "false";
|
|
108
|
+
|
|
109
|
+
if (questioned.value) console.log("");
|
|
110
|
+
return options as IArguments;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const detectManager = async (): Promise<
|
|
115
|
+
"npm" | "pnpm" | "yarn" | "bun" | null
|
|
116
|
+
> => {
|
|
117
|
+
const result: DetectResult | null = await detect({ cwd: process.cwd() });
|
|
118
|
+
if (result?.name === "npm")
|
|
119
|
+
return null; // NPM case is still selectable
|
|
120
|
+
else if (result?.name === "deno") return null; // Deno case is not supported
|
|
121
|
+
return result?.name ?? null;
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import cp from "child_process";
|
|
2
|
-
|
|
3
|
-
export namespace CommandExecutor {
|
|
4
|
-
export function 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 function 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,91 +1,91 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
|
|
4
|
-
import { CommandExecutor } from "./CommandExecutor";
|
|
5
|
-
import { FileRetriever } from "./FileRetriever";
|
|
6
|
-
|
|
7
|
-
export class PackageManager {
|
|
8
|
-
public manager: Manager = "npm";
|
|
9
|
-
public get file(): string {
|
|
10
|
-
return path.join(this.directory, "package.json");
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public static async mount(): Promise<PackageManager> {
|
|
14
|
-
const location: string | null = await FileRetriever.directory(
|
|
15
|
-
"package.json",
|
|
16
|
-
)(process.cwd());
|
|
17
|
-
if (location === null)
|
|
18
|
-
throw new Error(`Unable to find "package.json" file`);
|
|
19
|
-
|
|
20
|
-
return new PackageManager(
|
|
21
|
-
location,
|
|
22
|
-
await this.load(path.join(location, "package.json")),
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public async save(modifier: (data: Package.Data) => void): Promise<void> {
|
|
27
|
-
const content: string = await fs.promises.readFile(this.file, "utf8");
|
|
28
|
-
this.data = JSON.parse(content);
|
|
29
|
-
modifier(this.data);
|
|
30
|
-
|
|
31
|
-
return fs.promises.writeFile(
|
|
32
|
-
this.file,
|
|
33
|
-
JSON.stringify(this.data, null, 2),
|
|
34
|
-
"utf8",
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public install(props: {
|
|
39
|
-
dev: boolean;
|
|
40
|
-
modulo: string;
|
|
41
|
-
version?:
|
|
42
|
-
| `latest`
|
|
43
|
-
| `next`
|
|
44
|
-
| `^${number}.${number}.${number}`
|
|
45
|
-
| (string & {});
|
|
46
|
-
force?: boolean;
|
|
47
|
-
}): boolean {
|
|
48
|
-
const middle: string = [
|
|
49
|
-
installCmdTable[this.manager],
|
|
50
|
-
props.dev ? devOptionTable[this.manager] : "",
|
|
51
|
-
]
|
|
52
|
-
.filter((str) => !!str.length)
|
|
53
|
-
.join(" ");
|
|
54
|
-
const modulo: string = `${props.modulo}${props.version ? `@${props.version}` : ""}`;
|
|
55
|
-
CommandExecutor.run([this.manager, middle, modulo].join(" "));
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
private constructor(
|
|
60
|
-
public readonly directory: string,
|
|
61
|
-
public data: Package.Data,
|
|
62
|
-
) {}
|
|
63
|
-
|
|
64
|
-
private static async load(file: string): Promise<Package.Data> {
|
|
65
|
-
const content: string = await fs.promises.readFile(file, "utf8");
|
|
66
|
-
return JSON.parse(content);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
export namespace Package {
|
|
70
|
-
export interface Data {
|
|
71
|
-
scripts?: Record<string, string>;
|
|
72
|
-
dependencies?: Record<string, string>;
|
|
73
|
-
devDependencies?: Record<string, string>;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
type Manager = "npm" | "pnpm" | "yarn" | "bun";
|
|
78
|
-
|
|
79
|
-
const installCmdTable = {
|
|
80
|
-
npm: "i",
|
|
81
|
-
pnpm: "add",
|
|
82
|
-
yarn: "add",
|
|
83
|
-
bun: "add",
|
|
84
|
-
} as const satisfies Record<Manager, string>;
|
|
85
|
-
|
|
86
|
-
const devOptionTable = {
|
|
87
|
-
npm: "-D",
|
|
88
|
-
pnpm: "-D",
|
|
89
|
-
yarn: "-D",
|
|
90
|
-
bun: "-d",
|
|
91
|
-
} as const satisfies Record<Manager, string>;
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
import { CommandExecutor } from "./CommandExecutor";
|
|
5
|
+
import { FileRetriever } from "./FileRetriever";
|
|
6
|
+
|
|
7
|
+
export class PackageManager {
|
|
8
|
+
public manager: Manager = "npm";
|
|
9
|
+
public get file(): string {
|
|
10
|
+
return path.join(this.directory, "package.json");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public static async mount(): Promise<PackageManager> {
|
|
14
|
+
const location: string | null = await FileRetriever.directory(
|
|
15
|
+
"package.json",
|
|
16
|
+
)(process.cwd());
|
|
17
|
+
if (location === null)
|
|
18
|
+
throw new Error(`Unable to find "package.json" file`);
|
|
19
|
+
|
|
20
|
+
return new PackageManager(
|
|
21
|
+
location,
|
|
22
|
+
await this.load(path.join(location, "package.json")),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public async save(modifier: (data: Package.Data) => void): Promise<void> {
|
|
27
|
+
const content: string = await fs.promises.readFile(this.file, "utf8");
|
|
28
|
+
this.data = JSON.parse(content);
|
|
29
|
+
modifier(this.data);
|
|
30
|
+
|
|
31
|
+
return fs.promises.writeFile(
|
|
32
|
+
this.file,
|
|
33
|
+
JSON.stringify(this.data, null, 2),
|
|
34
|
+
"utf8",
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public install(props: {
|
|
39
|
+
dev: boolean;
|
|
40
|
+
modulo: string;
|
|
41
|
+
version?:
|
|
42
|
+
| `latest`
|
|
43
|
+
| `next`
|
|
44
|
+
| `^${number}.${number}.${number}`
|
|
45
|
+
| (string & {});
|
|
46
|
+
force?: boolean;
|
|
47
|
+
}): boolean {
|
|
48
|
+
const middle: string = [
|
|
49
|
+
installCmdTable[this.manager],
|
|
50
|
+
props.dev ? devOptionTable[this.manager] : "",
|
|
51
|
+
]
|
|
52
|
+
.filter((str) => !!str.length)
|
|
53
|
+
.join(" ");
|
|
54
|
+
const modulo: string = `${props.modulo}${props.version ? `@${props.version}` : ""}`;
|
|
55
|
+
CommandExecutor.run([this.manager, middle, modulo].join(" "));
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private constructor(
|
|
60
|
+
public readonly directory: string,
|
|
61
|
+
public data: Package.Data,
|
|
62
|
+
) {}
|
|
63
|
+
|
|
64
|
+
private static async load(file: string): Promise<Package.Data> {
|
|
65
|
+
const content: string = await fs.promises.readFile(file, "utf8");
|
|
66
|
+
return JSON.parse(content);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export namespace Package {
|
|
70
|
+
export interface Data {
|
|
71
|
+
scripts?: Record<string, string>;
|
|
72
|
+
dependencies?: Record<string, string>;
|
|
73
|
+
devDependencies?: Record<string, string>;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type Manager = "npm" | "pnpm" | "yarn" | "bun";
|
|
78
|
+
|
|
79
|
+
const installCmdTable = {
|
|
80
|
+
npm: "i",
|
|
81
|
+
pnpm: "add",
|
|
82
|
+
yarn: "add",
|
|
83
|
+
bun: "add",
|
|
84
|
+
} as const satisfies Record<Manager, string>;
|
|
85
|
+
|
|
86
|
+
const devOptionTable = {
|
|
87
|
+
npm: "-D",
|
|
88
|
+
pnpm: "-D",
|
|
89
|
+
yarn: "-D",
|
|
90
|
+
bun: "-d",
|
|
91
|
+
} as const satisfies Record<Manager, string>;
|