@nestia/core 1.0.10 → 1.0.12

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.
Files changed (43) hide show
  1. package/README.md +15 -9
  2. package/lib/decorators/EncryptedBody.d.ts +1 -1
  3. package/lib/decorators/EncryptedBody.js +1 -1
  4. package/lib/decorators/EncryptedRoute.d.ts +4 -4
  5. package/lib/decorators/EncryptedRoute.js +4 -4
  6. package/lib/decorators/TypedRoute.d.ts +3 -2
  7. package/lib/decorators/TypedRoute.js +3 -2
  8. package/lib/decorators/TypedRoute.js.map +1 -1
  9. package/lib/executable/core.js +45 -23
  10. package/lib/executable/core.js.map +1 -1
  11. package/lib/executable/internal/ArgumentParser.d.ts +9 -0
  12. package/lib/executable/internal/ArgumentParser.js +246 -0
  13. package/lib/executable/internal/ArgumentParser.js.map +1 -0
  14. package/lib/executable/internal/CommandExecutor.d.ts +3 -0
  15. package/lib/executable/internal/CommandExecutor.js +17 -0
  16. package/lib/executable/internal/CommandExecutor.js.map +1 -0
  17. package/lib/executable/internal/FileRetriever.d.ts +5 -0
  18. package/lib/executable/internal/FileRetriever.js +109 -0
  19. package/lib/executable/internal/FileRetriever.js.map +1 -0
  20. package/lib/executable/internal/PackageManager.d.ts +27 -0
  21. package/lib/executable/internal/PackageManager.js +126 -0
  22. package/lib/executable/internal/PackageManager.js.map +1 -0
  23. package/lib/executable/internal/PluginConfigurator.d.ts +5 -0
  24. package/lib/executable/internal/PluginConfigurator.js +145 -0
  25. package/lib/executable/internal/PluginConfigurator.js.map +1 -0
  26. package/package.json +7 -3
  27. package/src/decorators/EncryptedBody.ts +1 -1
  28. package/src/decorators/EncryptedRoute.ts +4 -4
  29. package/src/decorators/TypedRoute.ts +3 -2
  30. package/src/executable/core.ts +39 -18
  31. package/src/executable/internal/ArgumentParser.ts +146 -0
  32. package/src/executable/internal/CommandExecutor.ts +8 -0
  33. package/src/executable/internal/FileRetriever.ts +33 -0
  34. package/src/executable/internal/PackageManager.ts +92 -0
  35. package/src/executable/internal/PluginConfigurator.ts +130 -0
  36. package/lib/executable/internal/CommandParser.d.ts +0 -3
  37. package/lib/executable/internal/CommandParser.js +0 -21
  38. package/lib/executable/internal/CommandParser.js.map +0 -1
  39. package/lib/executable/internal/CoreSetupWizard.d.ts +0 -8
  40. package/lib/executable/internal/CoreSetupWizard.js +0 -271
  41. package/lib/executable/internal/CoreSetupWizard.js.map +0 -1
  42. package/src/executable/internal/CommandParser.ts +0 -15
  43. package/src/executable/internal/CoreSetupWizard.ts +0 -225
@@ -0,0 +1,146 @@
1
+ import type CommanderModule from "commander";
2
+ import fs from "fs";
3
+ import type * as InquirerModule from "inquirer";
4
+ import path from "path";
5
+ import { FileRetriever } from "./FileRetriever";
6
+
7
+ import { PackageManager } from "./PackageManager";
8
+
9
+ export namespace ArgumentParser {
10
+ export interface IArguments {
11
+ compiler: "ts-patch" | "ttypescript";
12
+ manager: "npm" | "pnpm" | "yarn";
13
+ project: string | null;
14
+ }
15
+
16
+ export async function parse(pack: PackageManager): Promise<IArguments> {
17
+ // INSTALL TEMPORARY PACKAGES
18
+ const newbie = {
19
+ commander: pack.install({
20
+ dev: true,
21
+ modulo: "commander",
22
+ version: "10.0.0",
23
+ silent: true,
24
+ }),
25
+ inquirer: pack.install({
26
+ dev: true,
27
+ modulo: "inquirer",
28
+ version: "8.2.5",
29
+ silent: true,
30
+ }),
31
+ };
32
+
33
+ // TAKE OPTIONS
34
+ const output: IArguments | Error = await (async () => {
35
+ try {
36
+ return await _Parse(pack);
37
+ } catch (error) {
38
+ return error as Error;
39
+ }
40
+ })();
41
+
42
+ // REMOVE TEMPORARY PACKAGES
43
+ if (newbie.commander) pack.erase({ modulo: "commander", silent: true });
44
+ if (newbie.inquirer) pack.erase({ modulo: "inquirer", silent: true });
45
+
46
+ // RETURNS
47
+ if (output instanceof Error) throw output;
48
+ return output;
49
+ }
50
+
51
+ async function _Parse(pack: PackageManager): Promise<IArguments> {
52
+ // PREPARE ASSETS
53
+ const { createPromptModule }: typeof InquirerModule =
54
+ await FileRetriever.require(path.join("node_modules", "inquirer"))(
55
+ pack.directory,
56
+ );
57
+ const { program }: typeof CommanderModule = await FileRetriever.require(
58
+ path.join("node_modules", "commander"),
59
+ )(pack.directory);
60
+
61
+ program.option("--compiler [compiler]", "compiler type");
62
+ program.option("--manager [manager", "package manager");
63
+ program.option("--project [project]", "tsconfig.json file location");
64
+
65
+ // INTERNAL PROCEDURES
66
+ const questioned = { value: false };
67
+ const action = (
68
+ closure: (options: Partial<IArguments>) => Promise<IArguments>,
69
+ ) => {
70
+ return new Promise<IArguments>((resolve, reject) => {
71
+ program.action(async (options) => {
72
+ try {
73
+ resolve(await closure(options));
74
+ } catch (exp) {
75
+ reject(exp);
76
+ }
77
+ });
78
+ program.parseAsync().catch(reject);
79
+ });
80
+ };
81
+ const select =
82
+ (name: string) =>
83
+ (message: string) =>
84
+ async <Choice extends string>(
85
+ choices: Choice[],
86
+ ): Promise<Choice> => {
87
+ questioned.value = true;
88
+ return (
89
+ await createPromptModule()({
90
+ type: "list",
91
+ name: name,
92
+ message: message,
93
+ choices: choices,
94
+ })
95
+ )[name];
96
+ };
97
+ const configure = async () => {
98
+ const fileList: string[] = await (
99
+ await fs.promises.readdir(process.cwd())
100
+ ).filter(
101
+ (str) =>
102
+ str.substring(0, 8) === "tsconfig" &&
103
+ str.substring(str.length - 5) === ".json",
104
+ );
105
+ if (fileList.length === 0) {
106
+ if (process.cwd() !== pack.directory)
107
+ throw new Error(`Unable to find "tsconfig.json" file.`);
108
+ return null;
109
+ } else if (fileList.length === 1) return fileList[0];
110
+ return select("tsconfig")("TS Config File")(fileList);
111
+ };
112
+
113
+ // DO CONSTRUCT
114
+ return action(async (options) => {
115
+ if (options.compiler === undefined) {
116
+ console.log(COMPILER_DESCRIPTION);
117
+ options.compiler = await select("compiler")(`Compiler`)(
118
+ pack.data.scripts?.build === "nest build"
119
+ ? ["ts-patch" as const, "ttypescript" as const]
120
+ : ["ttypescript" as const, "ts-patch" as const],
121
+ );
122
+ }
123
+ options.manager ??= await select("manager")("Package Manager")([
124
+ "npm" as const,
125
+ "pnpm" as const,
126
+ "yarn" as const,
127
+ ]);
128
+ pack.manager = options.manager;
129
+ options.project ??= await configure();
130
+
131
+ if (questioned.value) console.log("");
132
+ return options as IArguments;
133
+ });
134
+ }
135
+ }
136
+
137
+ const COMPILER_DESCRIPTION = [
138
+ `About compiler, if you adapt "ttypescript", you should use "ttsc" instead.`,
139
+ ``,
140
+ `Otherwise, you choose "ts-patch", you can use the original "tsc" command.`,
141
+ `However, the "ts-patch" hacks "node_modules/typescript" source code.`,
142
+ `Also, whenever update "typescript", you've to run "npm run prepare" command.`,
143
+ ``,
144
+ `By the way, when using "@nest/cli", you must just choose "ts-patch".`,
145
+ ``,
146
+ ].join("\n");
@@ -0,0 +1,8 @@
1
+ import cp from "child_process";
2
+
3
+ export namespace CommandExecutor {
4
+ export function run(str: string, silent: boolean): void {
5
+ if (silent === false) console.log(str);
6
+ cp.execSync(str, { stdio: "ignore" });
7
+ }
8
+ }
@@ -0,0 +1,33 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ export namespace FileRetriever {
5
+ export const directory =
6
+ (name: string) =>
7
+ (directory: string, depth: number = 0): string | null => {
8
+ const location: string = path.join(directory, name);
9
+ if (fs.existsSync(location)) return directory;
10
+ else if (depth > 2) return null;
11
+ return file(name)(path.join(directory, ".."), 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
+
23
+ export const require =
24
+ (name: string) =>
25
+ async (directory: string, depth: number = 0) => {
26
+ const location: string | null = file(name)(directory, depth);
27
+ if (location === null)
28
+ throw new Error(
29
+ `Unable to find installed module. Please report to the nestia - https://github.com/samchon/nestia/issues`,
30
+ );
31
+ return import(location);
32
+ };
33
+ }
@@ -0,0 +1,92 @@
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: string = "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
+ silent?: boolean;
41
+ modulo: string;
42
+ version?: string;
43
+ }): boolean {
44
+ const container = props.dev
45
+ ? this.data.devDependencies
46
+ : this.data.dependencies;
47
+ if (
48
+ !!container?.[props.modulo] &&
49
+ FileRetriever.file(path.join("node_modules", props.modulo))(
50
+ this.directory,
51
+ ) !== null
52
+ )
53
+ return false;
54
+
55
+ const middle: string =
56
+ this.manager === "yarn"
57
+ ? `add${props.dev ? " -D" : ""}`
58
+ : `install ${props.dev ? "--save-dev" : "--save"}`;
59
+ CommandExecutor.run(
60
+ `${this.manager} ${middle} ${props.modulo}${
61
+ props.version ? `@${props.version}` : ""
62
+ }`,
63
+ !!props.silent,
64
+ );
65
+ return true;
66
+ }
67
+
68
+ public erase(props: { modulo: string; silent?: boolean }): void {
69
+ const middle: string = this.manager === "yarn" ? "remove" : "uninstall";
70
+ CommandExecutor.run(
71
+ `${this.manager} ${middle} ${props.modulo}`,
72
+ !!props.silent,
73
+ );
74
+ }
75
+
76
+ private constructor(
77
+ public readonly directory: string,
78
+ public data: Package.Data,
79
+ ) {}
80
+
81
+ private static async load(file: string): Promise<Package.Data> {
82
+ const content: string = await fs.promises.readFile(file, "utf8");
83
+ return JSON.parse(content);
84
+ }
85
+ }
86
+ export namespace Package {
87
+ export interface Data {
88
+ scripts?: Record<string, string>;
89
+ dependencies?: Record<string, string>;
90
+ devDependencies?: Record<string, string>;
91
+ }
92
+ }
@@ -0,0 +1,130 @@
1
+ import type Comment from "comment-json";
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+ import { ArgumentParser } from "./ArgumentParser";
6
+ import { FileRetriever } from "./FileRetriever";
7
+ import { PackageManager } from "./PackageManager";
8
+
9
+ export namespace PluginConfigurator {
10
+ export async function configure(
11
+ pack: PackageManager,
12
+ args: ArgumentParser.IArguments,
13
+ ): Promise<void> {
14
+ // INSTALL COMMENT-JSON
15
+ const installed: boolean = pack.install({
16
+ dev: true,
17
+ modulo: "comment-json",
18
+ version: "4.2.3",
19
+ silent: true,
20
+ });
21
+
22
+ // DO CONFIGURE
23
+ const error: Error | null = await (async () => {
24
+ try {
25
+ await _Configure(pack, args);
26
+ return null;
27
+ } catch (exp) {
28
+ return exp as Error;
29
+ }
30
+ })();
31
+
32
+ // REMOVE IT
33
+ if (installed)
34
+ pack.erase({
35
+ modulo: "comment-json",
36
+ silent: true,
37
+ });
38
+ if (error !== null) throw error;
39
+ }
40
+
41
+ async function _Configure(
42
+ pack: PackageManager,
43
+ args: ArgumentParser.IArguments,
44
+ ): Promise<void> {
45
+ // GET COMPILER-OPTIONS
46
+ const Comment: typeof import("comment-json") =
47
+ await FileRetriever.require(
48
+ path.join("node_modules", "comment-json"),
49
+ )(pack.directory);
50
+
51
+ const config: Comment.CommentObject = Comment.parse(
52
+ await fs.promises.readFile(args.project!, "utf8"),
53
+ ) as Comment.CommentObject;
54
+ const compilerOptions = config.compilerOptions as
55
+ | Comment.CommentObject
56
+ | undefined;
57
+ if (compilerOptions === undefined)
58
+ throw new Error(
59
+ `${args.project} file does not have "compilerOptions" property.`,
60
+ );
61
+
62
+ // PREPARE PLUGINS
63
+ const plugins: Comment.CommentArray<Comment.CommentObject> = (() => {
64
+ const plugins = compilerOptions.plugins as
65
+ | Comment.CommentArray<Comment.CommentObject>
66
+ | undefined;
67
+ if (plugins === undefined)
68
+ return (compilerOptions.plugins = [] as any);
69
+ else if (!Array.isArray(plugins))
70
+ throw new Error(
71
+ `"plugins" property of ${args.project} must be array type.`,
72
+ );
73
+ return plugins;
74
+ })();
75
+
76
+ // CHECK WHETHER CONFIGURED
77
+ const strict: boolean = compilerOptions.strict === true;
78
+ const core: Comment.CommentObject | undefined = plugins.find(
79
+ (p) =>
80
+ typeof p === "object" &&
81
+ p !== null &&
82
+ p.transform === "@nestia/core/lib/transform",
83
+ );
84
+ const typia: Comment.CommentObject | undefined = plugins.find(
85
+ (p) =>
86
+ typeof p === "object" &&
87
+ p !== null &&
88
+ p.transform === "typia/lib/transform",
89
+ );
90
+ if (strict && !!core && !!typia) return;
91
+
92
+ // DO CONFIGURE
93
+ compilerOptions.strict = true;
94
+ if (core === undefined)
95
+ plugins.push(
96
+ Comment.parse(`{
97
+ "transform": "@nestia/core/lib/transform",
98
+ /**
99
+ * Validate request body.
100
+ *
101
+ * - "assert": Use typia.assert() function
102
+ * - "is": Use typia.is() function
103
+ * - "validate": Use typia.validate() function
104
+ */
105
+ "validate": "assert",
106
+
107
+ /**
108
+ * Validate JSON typed response body.
109
+ *
110
+ * - null: Just use JSON.stringify() function, without boosting
111
+ * - "stringify": Use typia.stringify() function, but dangerous
112
+ * - "assert": Use typia.assertStringify() function
113
+ * - "is": Use typia.isStringify() function
114
+ * - "validate": Use typia.validateStringify() function
115
+ */
116
+ "stringify": "is"
117
+ }`) as Comment.CommentObject,
118
+ );
119
+ if (typia === undefined)
120
+ plugins.push(
121
+ Comment.parse(
122
+ `{ "transform": "typia/lib/transform" }`,
123
+ ) as Comment.CommentObject,
124
+ );
125
+ await fs.promises.writeFile(
126
+ args.project!,
127
+ Comment.stringify(config, null, 2),
128
+ );
129
+ }
130
+ }
@@ -1,3 +0,0 @@
1
- export declare namespace CommandParser {
2
- function parse(argList: string[]): Record<string, string>;
3
- }
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CommandParser = void 0;
4
- var CommandParser;
5
- (function (CommandParser) {
6
- function parse(argList) {
7
- var output = {};
8
- argList.forEach(function (arg, i) {
9
- if (arg.startsWith("--") === false)
10
- return;
11
- var key = arg.slice(2);
12
- var value = argList[i + 1];
13
- if (value === undefined || value.startsWith("--"))
14
- return;
15
- output[key] = value;
16
- });
17
- return output;
18
- }
19
- CommandParser.parse = parse;
20
- })(CommandParser = exports.CommandParser || (exports.CommandParser = {}));
21
- //# sourceMappingURL=CommandParser.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CommandParser.js","sourceRoot":"","sources":["../../../src/executable/internal/CommandParser.ts"],"names":[],"mappings":";;;AAAA,IAAiB,aAAa,CAc7B;AAdD,WAAiB,aAAa;IAC1B,SAAgB,KAAK,CAAC,OAAiB;QACnC,IAAM,MAAM,GAA2B,EAAE,CAAC;QAC1C,OAAO,CAAC,OAAO,CAAC,UAAC,GAAG,EAAE,CAAC;YACnB,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,KAAK;gBAAE,OAAO;YAE3C,IAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACzB,IAAM,KAAK,GAAuB,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;YACjD,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC;gBAAE,OAAO;YAE1D,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAClB,CAAC;IAZe,mBAAK,QAYpB,CAAA;AACL,CAAC,EAdgB,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAc7B"}
@@ -1,8 +0,0 @@
1
- export declare namespace CoreSetupWizard {
2
- interface IArguments {
3
- manager: "npm" | "pnpm" | "yarn";
4
- project: string;
5
- }
6
- function ttypescript(args: string | IArguments): Promise<void>;
7
- function tsPatch(args: string | IArguments): Promise<void>;
8
- }