nestia 8.0.8 → 9.0.0-dev.20251107
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/bin/index.js +13 -13
- package/bin/internal/PluginConfigurator.js +25 -25
- package/package.json +4 -4
- package/src/NestiaSetupWizard.ts +90 -90
- package/src/NestiaStarter.ts +41 -41
- package/src/NestiaTemplate.ts +37 -37
- package/src/index.ts +64 -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/src/NestiaTemplate.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import cp from "child_process";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
|
|
4
|
-
export namespace NestiaTemplate {
|
|
5
|
-
export const clone =
|
|
6
|
-
(halter: (msg?: string) => never) =>
|
|
7
|
-
async (argv: string[]): Promise<void> => {
|
|
8
|
-
// VALIDATION
|
|
9
|
-
const dest: string | undefined = argv[0];
|
|
10
|
-
if (dest === undefined) halter();
|
|
11
|
-
else if (fs.existsSync(dest) === true)
|
|
12
|
-
halter("The target directory already exists.");
|
|
13
|
-
|
|
14
|
-
console.log("-----------------------------------------");
|
|
15
|
-
console.log(" Nestia Template Kit");
|
|
16
|
-
console.log("-----------------------------------------");
|
|
17
|
-
|
|
18
|
-
// COPY PROJECTS
|
|
19
|
-
execute(`git clone https://github.com/samchon/backend ${dest}`);
|
|
20
|
-
console.log(`cd "${dest}"`);
|
|
21
|
-
process.chdir(dest);
|
|
22
|
-
|
|
23
|
-
// INSTALL DEPENDENCIES
|
|
24
|
-
execute("npm install");
|
|
25
|
-
|
|
26
|
-
// BUILD TYPESCRIPT
|
|
27
|
-
execute("npm run build");
|
|
28
|
-
|
|
29
|
-
// REMOVE .GIT DIRECTORY
|
|
30
|
-
cp.execSync("npx rimraf .git");
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
function execute(command: string): void {
|
|
34
|
-
console.log(`\n$ ${command}`);
|
|
35
|
-
cp.execSync(command, { stdio: "inherit" });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
1
|
+
import cp from "child_process";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
export namespace NestiaTemplate {
|
|
5
|
+
export const clone =
|
|
6
|
+
(halter: (msg?: string) => never) =>
|
|
7
|
+
async (argv: string[]): Promise<void> => {
|
|
8
|
+
// VALIDATION
|
|
9
|
+
const dest: string | undefined = argv[0];
|
|
10
|
+
if (dest === undefined) halter();
|
|
11
|
+
else if (fs.existsSync(dest) === true)
|
|
12
|
+
halter("The target directory already exists.");
|
|
13
|
+
|
|
14
|
+
console.log("-----------------------------------------");
|
|
15
|
+
console.log(" Nestia Template Kit");
|
|
16
|
+
console.log("-----------------------------------------");
|
|
17
|
+
|
|
18
|
+
// COPY PROJECTS
|
|
19
|
+
execute(`git clone https://github.com/samchon/backend ${dest}`);
|
|
20
|
+
console.log(`cd "${dest}"`);
|
|
21
|
+
process.chdir(dest);
|
|
22
|
+
|
|
23
|
+
// INSTALL DEPENDENCIES
|
|
24
|
+
execute("npm install");
|
|
25
|
+
|
|
26
|
+
// BUILD TYPESCRIPT
|
|
27
|
+
execute("npm run build");
|
|
28
|
+
|
|
29
|
+
// REMOVE .GIT DIRECTORY
|
|
30
|
+
cp.execSync("npx rimraf .git");
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function execute(command: string): void {
|
|
34
|
+
console.log(`\n$ ${command}`);
|
|
35
|
+
cp.execSync(command, { stdio: "inherit" });
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,64 +1,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
|
-
try {
|
|
52
|
-
require.resolve("@nestia/sdk/lib/executable/sdk");
|
|
53
|
-
} catch {
|
|
54
|
-
halt(
|
|
55
|
-
`@nestia/sdk has not been installed. Run "npx nestia setup" before.`,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
await import("@nestia/sdk/lib/executable/sdk");
|
|
59
|
-
} else halt(USAGE);
|
|
60
|
-
}
|
|
61
|
-
main().catch((exp) => {
|
|
62
|
-
console.log(exp.message);
|
|
63
|
-
process.exit(-1);
|
|
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
|
+
try {
|
|
52
|
+
require.resolve("@nestia/sdk/lib/executable/sdk");
|
|
53
|
+
} catch {
|
|
54
|
+
halt(
|
|
55
|
+
`@nestia/sdk has not been installed. Run "npx nestia setup" before.`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
await import("@nestia/sdk/lib/executable/sdk");
|
|
59
|
+
} else halt(USAGE);
|
|
60
|
+
}
|
|
61
|
+
main().catch((exp) => {
|
|
62
|
+
console.log(exp.message);
|
|
63
|
+
process.exit(-1);
|
|
64
|
+
});
|
|
@@ -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
|
+
}
|