Skip to content

Commit

Permalink
Merge pull request #1021 from samchon/feature/dependencies
Browse files Browse the repository at this point in the history
Package manager detection like samchon/typia#1245.
  • Loading branch information
samchon authored Sep 10, 2024
2 parents 41615f6 + 73fb13e commit 83fab54
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 27 deletions.
11 changes: 10 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
version: 2
updates:
#######################################################
- package-ecosystem: 'npm'
directory: '/packages/test'
directory: '/packages/cli'
schedule:
interval: 'daily'
open-pull-requests-limit: 25
allow:
- dependency-name: typescript
#######################################################
- package-ecosystem: 'npm'
directory: 'test'
schedule:
interval: 'daily'
versioning-strategy: increase
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestia",
"version": "6.0.2",
"version": "6.1.0",
"description": "Nestia CLI tool",
"main": "bin/index.js",
"bin": {
Expand Down Expand Up @@ -33,15 +33,16 @@
"dependencies": {
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5"
"inquirer": "^8.2.5",
"package-manager-detector": "^0.2.0"
},
"devDependencies": {
"@nestia/core": "^3.8.0",
"@nestia/sdk": "^3.8.0",
"@types/inquirer": "^9.0.3",
"@types/node": "^18.11.16",
"rimraf": "^3.0.2",
"typescript": "^5.5.3"
"typescript": "^5.6.2"
},
"files": [
"bin",
Expand Down
18 changes: 17 additions & 1 deletion packages/cli/src/NestiaSetupWizard.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from "fs";

import { ArgumentParser } from "./internal/ArgumentParser";
import { CommandExecutor } from "./internal/CommandExecutor";
import { PackageManager } from "./internal/PackageManager";
Expand Down Expand Up @@ -29,7 +31,11 @@ export namespace NestiaSetupWizard {
// INSTALL TYPESCRIPT COMPILERS
pack.install({ dev: true, modulo: "ts-patch", version: "latest" });
pack.install({ dev: true, modulo: "ts-node", version: "latest" });
pack.install({ dev: true, modulo: "typescript", version: "5.5.2" });
pack.install({
dev: true,
modulo: "typescript",
version: await getTypeScriptVersion(),
});
args.project ??= (() => {
const runner: string = pack.manager === "npm" ? "npx" : pack.manager;
CommandExecutor.run(`${runner} tsc --init`);
Expand Down Expand Up @@ -75,4 +81,14 @@ export namespace NestiaSetupWizard {
// CONFIGURE PLUGIN
await PluginConfigurator.configure(args);
}

const getTypeScriptVersion = async (): Promise<string> => {
const content: string = await fs.promises.readFile(
`${__dirname}/../package.json`,
"utf-8",
);
const json: { devDependencies: { typescript: string } } =
JSON.parse(content);
return json.devDependencies.typescript;
};
}
2 changes: 0 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ npx nestia [command] [options?]
5. npx nestia init
6. npx nestia sdk
7. npx nestia swagger
8. npx nestia openai
9. npx nestia e2e
10. npx nestia all
`;
Expand Down Expand Up @@ -45,7 +44,6 @@ async function main(): Promise<void> {
type === "dependencies" ||
type === "init" ||
type === "sdk" ||
type === "openai" ||
type === "swagger" ||
type === "e2e" ||
type === "all"
Expand Down
30 changes: 21 additions & 9 deletions packages/cli/src/internal/ArgumentParser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import commander from "commander";
import fs from "fs";
import inquirer from "inquirer";
import { DetectResult, detect } from "package-manager-detector";

import { PackageManager } from "./PackageManager";

export namespace ArgumentParser {
export interface IArguments {
manager: "npm" | "pnpm" | "yarn";
manager: "npm" | "pnpm" | "yarn" | "bun";
project: string | null;
runtime: boolean;
}
Expand Down Expand Up @@ -85,14 +86,17 @@ export namespace ArgumentParser {

// DO CONSTRUCT
return action(async (options) => {
options.manager ??= await select("manager")("Package Manager")(
[
"npm" as const,
"pnpm" as const,
"yarn (berry is not supported)" as "yarn",
],
(value) => value.split(" ")[0] as "yarn",
);
options.manager ??=
(await detectManager()) ??
(await select("manager")("Package Manager")(
[
"npm" as const,
"pnpm" as const,
"yarn (berry is not supported)" as "yarn",
"bun" as const,
],
(value) => value.split(" ")[0] as "yarn",
));
pack.manager = options.manager;
options.project ??= await configure();
options.runtime =
Expand All @@ -106,4 +110,12 @@ export namespace ArgumentParser {
return options as IArguments;
});
}

const detectManager = async (): Promise<
"npm" | "pnpm" | "yarn" | "bun" | null
> => {
const result: DetectResult | null = await detect({ cwd: process.cwd() });
if (result?.name === "npm") return null; // NPM case is still selectable
return result?.name ?? null;
};
}
41 changes: 30 additions & 11 deletions packages/cli/src/internal/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CommandExecutor } from "./CommandExecutor";
import { FileRetriever } from "./FileRetriever";

export class PackageManager {
public manager: string = "npm";
public manager: Manager = "npm";
public get file(): string {
return path.join(this.directory, "package.json");
}
Expand Down Expand Up @@ -38,18 +38,21 @@ export class PackageManager {
public install(props: {
dev: boolean;
modulo: string;
version?: `latest` | `next` | `${number}.${number}.${number}`;
version?:
| `latest`
| `next`
| `^${number}.${number}.${number}`
| (string & {});
force?: boolean;
}): boolean {
const symbol: string =
this.manager === "yarn"
? `add${props.dev ? " -D" : ""}`
: `install ${props.dev ? "--save-dev" : "--save"}`;
CommandExecutor.run(
`${this.manager} ${symbol}${props.force === true ? " --force" : ""} ${
props.modulo
}${props.version ? `@${props.version}` : ""}`,
);
const middle: string = [
installCmdTable[this.manager],
props.dev ? devOptionTable[this.manager] : "",
]
.filter((str) => !!str.length)
.join(" ");
const modulo: string = `${props.modulo}${props.version ? `@${props.version}` : ""}`;
CommandExecutor.run([this.manager, middle, modulo].join(" "));
return true;
}

Expand All @@ -70,3 +73,19 @@ export namespace Package {
devDependencies?: Record<string, string>;
}
}

type Manager = "npm" | "pnpm" | "yarn" | "bun";

const installCmdTable = {
npm: "i",
pnpm: "add",
yarn: "add",
bun: "add",
} as const satisfies Record<Manager, string>;

const devOptionTable = {
npm: "-D",
pnpm: "-D",
yarn: "-D",
bun: "-d",
} as const satisfies Record<Manager, string>;

0 comments on commit 83fab54

Please sign in to comment.