Skip to content

Commit

Permalink
Merge pull request #254 from samchon/features/yarn
Browse files Browse the repository at this point in the history
Fix #251 - support yarn monorepo in setup wizard
  • Loading branch information
samchon authored Feb 20, 2023
2 parents 8f41010 + d11f193 commit b93be57
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestia",
"version": "4.0.6",
"version": "4.0.7",
"description": "Nestia CLI",
"main": "bin/index.js",
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/core",
"version": "1.0.11",
"version": "1.0.12",
"description": "Super-fast validation decorators of NestJS",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -63,7 +63,7 @@
"raw-body": "*",
"reflect-metadata": "*",
"rxjs": "*",
"typia": "^3.5.3"
"typia": "^3.5.5"
},
"peerDependencies": {
"ttypescript": ">= 1.5.15",
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/executable/internal/ArgumentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type CommanderModule from "commander";
import fs from "fs";
import type * as InquirerModule from "inquirer";
import path from "path";
import { FileRetriever } from "./FileRetriever";

import { PackageManager } from "./PackageManager";

Expand Down Expand Up @@ -49,12 +50,13 @@ export namespace ArgumentParser {

async function _Parse(pack: PackageManager): Promise<IArguments> {
// PREPARE ASSETS
const { createPromptModule }: typeof InquirerModule = await import(
path.join(pack.directory, "node_modules", "inquirer")
);
const { program }: typeof CommanderModule = await import(
path.join(pack.directory, "node_modules", "commander")
);
const { createPromptModule }: typeof InquirerModule =
await FileRetriever.require(path.join("node_modules", "inquirer"))(
pack.directory,
);
const { program }: typeof CommanderModule = await FileRetriever.require(
path.join("node_modules", "commander"),
)(pack.directory);

program.option("--compiler [compiler]", "compiler type");
program.option("--manager [manager", "package manager");
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/executable/internal/FileRetriever.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from "fs";
import path from "path";

export namespace FileRetriever {
export const directory =
(name: string) =>
(directory: string, depth: number = 0): string | null => {
const location: string = path.join(directory, name);
if (fs.existsSync(location)) return directory;
else if (depth > 2) return null;
return file(name)(path.join(directory, ".."), depth + 1);
};

export const file =
(name: string) =>
(directory: string, depth: number = 0): string | null => {
const location: string = path.join(directory, name);
if (fs.existsSync(location)) return location;
else if (depth > 2) return null;
return file(name)(path.join(directory, ".."), depth + 1);
};

export const require =
(name: string) =>
async (directory: string, depth: number = 0) => {
const location: string | null = file(name)(directory, depth);
if (location === null)
throw new Error(
`Unable to find installed module. Please report to the nestia - https://github.com/samchon/nestia/issues`,
);
return import(location);
};
}
21 changes: 7 additions & 14 deletions packages/core/src/executable/internal/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs";
import path from "path";

import { CommandExecutor } from "./CommandExecutor";
import { FileRetriever } from "./FileRetriever";

export class PackageManager {
public manager: string = "npm";
Expand All @@ -10,7 +11,9 @@ export class PackageManager {
}

public static async mount(): Promise<PackageManager> {
const location: string | null = await find(process.cwd());
const location: string | null = await FileRetriever.directory(
"package.json",
)(process.cwd());
if (location === null)
throw new Error(`Unable to find "package.json" file`);

Expand Down Expand Up @@ -43,9 +46,9 @@ export class PackageManager {
: this.data.dependencies;
if (
!!container?.[props.modulo] &&
fs.existsSync(
path.join(this.directory, "node_modules", props.modulo),
)
FileRetriever.file(path.join("node_modules", props.modulo))(
this.directory,
) !== null
)
return false;

Expand Down Expand Up @@ -87,13 +90,3 @@ export namespace Package {
devDependencies?: Record<string, string>;
}
}

async function find(
directory: string = process.cwd(),
depth: number = 0,
): Promise<string | null> {
const location: string = path.join(directory, "package.json");
if (fs.existsSync(location)) return directory;
else if (depth > 1) return null;
return find(path.join(directory, ".."), depth + 1);
}
8 changes: 5 additions & 3 deletions packages/core/src/executable/internal/PluginConfigurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs";
import path from "path";

import { ArgumentParser } from "./ArgumentParser";
import { FileRetriever } from "./FileRetriever";
import { PackageManager } from "./PackageManager";

export namespace PluginConfigurator {
Expand Down Expand Up @@ -42,9 +43,10 @@ export namespace PluginConfigurator {
args: ArgumentParser.IArguments,
): Promise<void> {
// GET COMPILER-OPTIONS
const Comment: typeof import("comment-json") = await import(
path.join(pack.directory, "node_modules", "comment-json")
);
const Comment: typeof import("comment-json") =
await FileRetriever.require(
path.join("node_modules", "comment-json"),
)(pack.directory);

const config: Comment.CommentObject = Comment.parse(
await fs.promises.readFile(args.project!, "utf8"),
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/sdk",
"version": "1.0.7",
"version": "1.0.8",
"description": "Nestia SDK and Swagger generator",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -61,7 +61,7 @@
"tsconfck": "^2.0.1",
"tsconfig-paths": "^4.1.1",
"tstl": "^2.5.13",
"typia": "^3.4.22"
"typia": "^3.5.5"
},
"files": [
"assets",
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env node
import path from "path";
import { FileRetriever } from "./internal/FileRetriever";
import { NestiaSetupWizard } from "./NestiaSetupWizard";
import { NestiaStarter } from "./NestiaStarter";

Expand Down Expand Up @@ -49,16 +51,14 @@ async function main(): Promise<void> {
type === "sdk" ||
type === "swagger"
) {
try {
await import(process.cwd() + "/node_modules/@nestia/sdk");
} catch {
const location: string | null = FileRetriever.file(
path.join("node_modules", "@nestia", "sdk"),
)(process.cwd());
if (location === null)
halt(
`@nestia/sdk has not been installed. Run "npx nestia setup" before.`,
);
}
await import(
process.cwd() + "/node_modules/@nestia/sdk/lib/executable/sdk"
);
await import(path.join(location, "lib", "executable", "sdk"));
} else halt(USAGE);
}
main().catch((exp) => {
Expand Down
14 changes: 8 additions & 6 deletions src/internal/ArgumentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type CommanderModule from "commander";
import fs from "fs";
import type * as InquirerModule from "inquirer";
import path from "path";
import { FileRetriever } from "./FileRetriever";

import { PackageManager } from "./PackageManager";

Expand Down Expand Up @@ -49,12 +50,13 @@ export namespace ArgumentParser {

async function _Parse(pack: PackageManager): Promise<IArguments> {
// PREPARE ASSETS
const { createPromptModule }: typeof InquirerModule = await import(
path.join(pack.directory, "node_modules", "inquirer")
);
const { program }: typeof CommanderModule = await import(
path.join(pack.directory, "node_modules", "commander")
);
const { createPromptModule }: typeof InquirerModule =
await FileRetriever.require(path.join("node_modules", "inquirer"))(
pack.directory,
);
const { program }: typeof CommanderModule = await FileRetriever.require(
path.join("node_modules", "commander"),
)(pack.directory);

program.option("--compiler [compiler]", "compiler type");
program.option("--manager [manager", "package manager");
Expand Down
33 changes: 33 additions & 0 deletions src/internal/FileRetriever.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from "fs";
import path from "path";

export namespace FileRetriever {
export const directory =
(name: string) =>
(directory: string, depth: number = 0): string | null => {
const location: string = path.join(directory, name);
if (fs.existsSync(location)) return directory;
else if (depth > 2) return null;
return file(name)(path.join(directory, ".."), depth + 1);
};

export const file =
(name: string) =>
(directory: string, depth: number = 0): string | null => {
const location: string = path.join(directory, name);
if (fs.existsSync(location)) return location;
else if (depth > 2) return null;
return file(name)(path.join(directory, ".."), depth + 1);
};

export const require =
(name: string) =>
async (directory: string, depth: number = 0) => {
const location: string | null = file(name)(directory, depth);
if (location === null)
throw new Error(
`Unable to find installed module. Please report to the nestia - https://github.com/samchon/nestia/issues`,
);
return import(location);
};
}
21 changes: 7 additions & 14 deletions src/internal/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs";
import path from "path";

import { CommandExecutor } from "./CommandExecutor";
import { FileRetriever } from "./FileRetriever";

export class PackageManager {
public manager: string = "npm";
Expand All @@ -10,7 +11,9 @@ export class PackageManager {
}

public static async mount(): Promise<PackageManager> {
const location: string | null = await find(process.cwd());
const location: string | null = await FileRetriever.directory(
"package.json",
)(process.cwd());
if (location === null)
throw new Error(`Unable to find "package.json" file`);

Expand Down Expand Up @@ -43,9 +46,9 @@ export class PackageManager {
: this.data.dependencies;
if (
!!container?.[props.modulo] &&
fs.existsSync(
path.join(this.directory, "node_modules", props.modulo),
)
FileRetriever.file(path.join("node_modules", props.modulo))(
this.directory,
) !== null
)
return false;

Expand Down Expand Up @@ -87,13 +90,3 @@ export namespace Package {
devDependencies?: Record<string, string>;
}
}

async function find(
directory: string = process.cwd(),
depth: number = 0,
): Promise<string | null> {
const location: string = path.join(directory, "package.json");
if (fs.existsSync(location)) return directory;
else if (depth > 1) return null;
return find(path.join(directory, ".."), depth + 1);
}
8 changes: 5 additions & 3 deletions src/internal/PluginConfigurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs";
import path from "path";

import { ArgumentParser } from "./ArgumentParser";
import { FileRetriever } from "./FileRetriever";
import { PackageManager } from "./PackageManager";

export namespace PluginConfigurator {
Expand Down Expand Up @@ -42,9 +43,10 @@ export namespace PluginConfigurator {
args: ArgumentParser.IArguments,
): Promise<void> {
// GET COMPILER-OPTIONS
const Comment: typeof import("comment-json") = await import(
path.join(pack.directory, "node_modules", "comment-json")
);
const Comment: typeof import("comment-json") =
await FileRetriever.require(
path.join("node_modules", "comment-json"),
)(pack.directory);

const config: Comment.CommentObject = Comment.parse(
await fs.promises.readFile(args.project!, "utf8"),
Expand Down

0 comments on commit b93be57

Please sign in to comment.