diff --git a/__tests__/Manager.spec.ts b/__tests__/Manager.spec.ts index 3016502..58ed3eb 100644 --- a/__tests__/Manager.spec.ts +++ b/__tests__/Manager.spec.ts @@ -31,19 +31,23 @@ describe("PackageManager", () => { it("should call setup when manager is set with yarn", async () => { jest.spyOn(manager, "initializeYarn").mockImplementation(() => {}); jest.spyOn(manager, "initializeNPM").mockImplementation(() => {}); + jest.spyOn(manager, "initializePNPM").mockImplementation(() => {}); await manager.setup(); expect(manager.initializeYarn).toHaveBeenCalled(); expect(manager.initializeNPM).not.toHaveBeenCalled(); + expect(manager.initializePNPM).not.toHaveBeenCalled(); }); it("should call setup with initializeNPM", async () => { jest.spyOn(manager, "initializeNPM").mockImplementation(() => {}); + jest.spyOn(manager, "initializePNPM").mockImplementation(() => {}); jest.spyOn(manager, "initializeYarn").mockImplementation(() => {}); config.manager = "npm"; manager.initialize(config, fakePath); await manager.setup(); expect(manager.initializeNPM).toHaveBeenCalled(); expect(manager.initializeYarn).not.toHaveBeenCalled(); + expect(manager.initializePNPM).not.toHaveBeenCalled(); }); it("call initializeNPM", () => { @@ -68,6 +72,18 @@ describe("PackageManager", () => { expect(manager.installDependencies).toHaveBeenCalledTimes(1); }); + it("call initializePNPM", () => { + jest.spyOn(manager, "installDependencies").mockImplementation(() => {}); + config.manager = "pnpm"; + manager.initialize(config, fakePath); + manager.initializePNPM(); + expect(child_process.execSync).toHaveBeenCalledTimes(1); + expect(child_process.execSync).toHaveBeenCalledWith("pnpm init -y", { + cwd: fakePath, + }); + expect(manager.installDependencies).toHaveBeenCalledTimes(1); + }); + it("should call installDependencies and call installTypes when language is typescript", () => { jest.spyOn(manager, "installDiscordJS").mockImplementation(jest.fn()); jest.spyOn(manager, "installNodemon").mockImplementation(jest.fn()); @@ -108,8 +124,18 @@ describe("PackageManager", () => { }); }); + it("should call installTypescript with pnpm", async () => { + const cmd = "pnpm add -D typescript"; + manager.installTypescript(); + expect(child_process.execSync).toHaveBeenCalledTimes(1); + expect(child_process.execSync).toHaveBeenCalledWith(cmd, { + cwd: fakePath, + stdio: "ignore", + }); + }); + it("should call installTypescript with npm", async () => { - const cmd = "npm i -D typescript"; + const cmd = "npm add -D typescript"; config.manager = "npm"; manager.initialize(config, fakePath); manager.installTypescript(); @@ -121,7 +147,7 @@ describe("PackageManager", () => { }); it("should call installDiscordJS with npm", async () => { - const cmd = "npm i discord.js@latest"; + const cmd = "npm add discord.js@latest"; manager.installDiscordJS(); expect(child_process.execSync).toHaveBeenCalledTimes(1); expect(child_process.execSync).toHaveBeenCalledWith(cmd, { @@ -129,6 +155,7 @@ describe("PackageManager", () => { stdio: "ignore", }); }); + it("should call installDiscordJS with yarn", async () => { const cmd = "yarn add discord.js@latest"; config.manager = "yarn"; @@ -140,6 +167,19 @@ describe("PackageManager", () => { stdio: "ignore", }); }); + + it("should call installDiscordJS with pnpm", async () => { + const cmd = "pnpm add discord.js@latest"; + config.manager = "pnpm"; + manager.initialize(config, fakePath); + manager.installDiscordJS(); + expect(child_process.execSync).toHaveBeenCalledTimes(1); + expect(child_process.execSync).toHaveBeenCalledWith(cmd, { + cwd: fakePath, + stdio: "ignore", + }); + }); + it("should call installNodemon with yarn", async () => { const cmd = "yarn add -D nodemon"; manager.installNodemon(); @@ -149,8 +189,19 @@ describe("PackageManager", () => { stdio: "ignore", }); }); + + it("should call installNodemon with pnpm", async () => { + const cmd = "pnpm add -D nodemon"; + manager.installNodemon(); + expect(child_process.execSync).toHaveBeenCalledTimes(1); + expect(child_process.execSync).toHaveBeenCalledWith(cmd, { + cwd: fakePath, + stdio: "ignore", + }); + }); + it("should call installNodemon with npm", async () => { - const cmd = "npm i -D nodemon"; + const cmd = "npm add -D nodemon"; config.manager = "npm"; manager.initialize(config, fakePath); manager.installNodemon(); @@ -160,8 +211,9 @@ describe("PackageManager", () => { stdio: "ignore", }); }); + it("should call installNodeTypes with npm", async () => { - const cmd = "npm i -D @types/node"; + const cmd = "npm add -D @types/node"; manager.installNodeTypes(); expect(child_process.execSync).toHaveBeenCalledTimes(1); expect(child_process.execSync).toHaveBeenCalledWith(cmd, { @@ -169,6 +221,7 @@ describe("PackageManager", () => { stdio: "ignore", }); }); + it("should call installNodeTypes with yarn", async () => { const cmd = "yarn add -D @types/node"; config.manager = "yarn"; @@ -180,4 +233,16 @@ describe("PackageManager", () => { stdio: "ignore", }); }); + + it("should call installNodeTypes with pnpm", async () => { + const cmd = "pnpm add -D @types/node"; + config.manager = "pnpm"; + manager.initialize(config, fakePath); + manager.installNodeTypes(); + expect(child_process.execSync).toHaveBeenCalledTimes(1); + expect(child_process.execSync).toHaveBeenCalledWith(cmd, { + cwd: fakePath, + stdio: "ignore", + }); + }); }); diff --git a/src/Manager.ts b/src/Manager.ts index 302e79a..882e531 100644 --- a/src/Manager.ts +++ b/src/Manager.ts @@ -10,14 +10,17 @@ export class PackageManager implements Initializer { async initialize(config: SlappeyConfig, filePath: string): Promise { this.config = config; this.filePath = filePath; - this.prefix = this.config.manager === 'npm' ? 'npm i' : 'yarn add'; + this.prefix = `${this.config.manager} add`; } async setup() { if (!this.config) throw new Error('Config Not Initialized.'); - return this.config.manager === 'npm' - ? this.initializeNPM() - : this.initializeYarn(); + const initialize = { + npm: this.initializeNPM(), + yarn: this.initializeYarn(), + pnpm: this.initializePNPM(), + } + return initialize[this.config.manager]; } public initializeNPM() { @@ -30,9 +33,14 @@ export class PackageManager implements Initializer { return this.installDependencies(); } + public initializePNPM() { + execSync(`${this.config?.manager} init -y`, { cwd: this.filePath }); + return this.installDependencies(); + } + public createTsconfig() { return execSync( - `${this.config?.manager} run tsc --init --resolveJsonModule --target es6`, + `${this.config?.manager === 'npm' ? 'npx' : this.config?.manager} tsc --init --resolveJsonModule --target es6`, { cwd: this.filePath } ); } diff --git a/src/utils/questions.ts b/src/utils/questions.ts index 3a53207..07d7959 100644 --- a/src/utils/questions.ts +++ b/src/utils/questions.ts @@ -75,6 +75,10 @@ export const packageManager: Array = [ title: 'yarn', value: 'yarn', }, + { + title: 'pnpm', + value: 'pnpm', + }, { title: 'npm', value: 'npm', diff --git a/src/utils/types.ts b/src/utils/types.ts index 7199a48..894d666 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1,7 +1,7 @@ export type Action = 'new' | 'gen'; export type CLIArguments = [option: Action, data: string]; export type Language = 'typescript' | 'javascript'; -export type PackageManagerType = 'npm' | 'yarn'; +export type PackageManagerType = 'npm' | 'yarn' | 'pnpm'; export type FileExtension = 'js' | 'ts'; export type StructureType = 'command' | 'event'; export type Credentials = {