Skip to content

Commit eefadd8

Browse files
committed
feat(packager): add feature to detect Yarn version
1 parent ba38705 commit eefadd8

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { getFileStats, withCurrentDir } from "./process";
1+
import { getFileStats, readCurrentDir, withCurrentDir } from "./process";
22

33
export async function checkPackageJson() {
44
const stats = await getFileStats(withCurrentDir("package.json"));
55
if (!stats) {
66
throw new Error("package.json is not found. Must be run in existing project directory.");
77
}
88
}
9+
10+
export async function detectYarnVersion(): Promise<"yarn" | "yarn2"> {
11+
const dirents = await readCurrentDir();
12+
const yarnConfigs = dirents.find((dirent) => dirent.name === ".yarn");
13+
return yarnConfigs && yarnConfigs.isDirectory() ? "yarn2" : "yarn";
14+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CreateJavascriptProject extends Command {
3636
const formatter = formatterFactory.createFormatter(answers.format);
3737

3838
const packagerFactory = PackagerFactory.getInstance();
39-
const packager = packagerFactory.createPackager(answers);
39+
const packager = await packagerFactory.createPackager(answers.packager);
4040

4141
const generatorFactory = GeneratorFactory.getInstance();
4242
const generators = answers.configurations.map((type) =>

src/packagers/PackagerFactory.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { InquirerConfigs } from "../types";
1+
import { detectYarnVersion } from "../checker";
2+
import { PackagerType } from "../types";
23
import { NpmPackager } from "./NpmPackager";
34
import { Packager } from "./Packager";
45
import { Yarn2Packager } from "./Yarn2Packager";
@@ -14,19 +15,21 @@ export class PackagerFactory {
1415
return this.factory;
1516
}
1617

17-
createPackager(configs: InquirerConfigs): Packager {
18-
if (configs.configurations.includes("yarn2")) {
19-
return new Yarn2Packager();
20-
}
18+
async createPackager(type: PackagerType): Promise<Packager> {
19+
if (type === "yarn") {
20+
const yarnVersion = await detectYarnVersion();
21+
22+
if (yarnVersion === "yarn2") {
23+
return new Yarn2Packager();
24+
}
2125

22-
if (configs.packager === "yarn") {
2326
return new YarnPackager();
2427
}
2528

26-
if (configs.packager === "npm") {
29+
if (type === "npm") {
2730
return new NpmPackager();
2831
}
2932

30-
throw new Error(`"${configs.packager}" packager not implemented.`);
33+
throw new Error(`"${type}" packager not implemented.`);
3134
}
3235
}

src/process.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChildProcessWithoutNullStreams } from "child_process";
22
import { spawn } from "cross-spawn";
3-
import { readFile, rm, stat, writeFile } from "fs/promises";
3+
import { readdir, readFile, rm, stat, writeFile } from "fs/promises";
44
import { Stats } from "node:fs";
55
import { join } from "path";
66

@@ -83,6 +83,10 @@ export async function removeFile(file: string) {
8383
await rm(file, { force: true });
8484
}
8585

86+
export async function readCurrentDir() {
87+
return readdir(currentDir, { withFileTypes: true });
88+
}
89+
8690
export function formatJson(obj: object): string {
8791
return JSON.stringify(obj, null, 2);
8892
}

0 commit comments

Comments
 (0)