From 067a0d73ddc9d4b7c95f3e670c66322b1a7643bf Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:39:28 -0700 Subject: [PATCH 1/8] Add pnpm to prompts --- src/utils/questions.ts | 4 ++++ 1 file changed, 4 insertions(+) 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', From 562d6366260118672389585e97bb7159146cba53 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:39:54 -0700 Subject: [PATCH 2/8] add pnpm to types --- src/utils/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 = { From b1baaed6d88277af656eb42e751c19035e6a8d2b Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:42:36 -0700 Subject: [PATCH 3/8] Fix bug with NPM "Missing script: "tsc"" --- src/Manager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Manager.ts b/src/Manager.ts index 302e79a..f117287 100644 --- a/src/Manager.ts +++ b/src/Manager.ts @@ -32,7 +32,7 @@ export class PackageManager implements Initializer { 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 } ); } From 174e55435abaeb7c5ff7820b5b2bf852751c7852 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:43:56 -0700 Subject: [PATCH 4/8] Set all prefix's to add. NPM has had alias for add since v6 --- src/Manager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Manager.ts b/src/Manager.ts index f117287..44da7a8 100644 --- a/src/Manager.ts +++ b/src/Manager.ts @@ -10,7 +10,7 @@ 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() { From 0fb4450f1a6efc975a62ca591b6e2663f265b012 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:44:34 -0700 Subject: [PATCH 5/8] Add initializer for PNPM --- src/Manager.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Manager.ts b/src/Manager.ts index 44da7a8..30a8cff 100644 --- a/src/Manager.ts +++ b/src/Manager.ts @@ -30,6 +30,11 @@ 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 === 'npm' ? 'npx' : this.config?.manager} tsc --init --resolveJsonModule --target es6`, From 48814f4d87186ea067d23647b3ea8d6ff651bc63 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:45:27 -0700 Subject: [PATCH 6/8] Added support for multiple initializer's & added PNPM --- src/Manager.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Manager.ts b/src/Manager.ts index 30a8cff..882e531 100644 --- a/src/Manager.ts +++ b/src/Manager.ts @@ -15,9 +15,12 @@ export class PackageManager implements Initializer { 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() { From f590b00ec554b7fab5987539e6a3c78c4aba5a68 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:46:23 -0700 Subject: [PATCH 7/8] Update "npm i" to "npm add" --- __tests__/Manager.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/__tests__/Manager.spec.ts b/__tests__/Manager.spec.ts index 3016502..8705760 100644 --- a/__tests__/Manager.spec.ts +++ b/__tests__/Manager.spec.ts @@ -109,7 +109,7 @@ describe("PackageManager", () => { }); 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 +121,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, { @@ -150,7 +150,7 @@ describe("PackageManager", () => { }); }); 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(); @@ -161,7 +161,7 @@ describe("PackageManager", () => { }); }); 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, { From f1eba298a41af69941840b310db913955b09ce9d Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 2 Apr 2022 23:47:03 -0700 Subject: [PATCH 8/8] Added tests for PNPM --- __tests__/Manager.spec.ts | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/__tests__/Manager.spec.ts b/__tests__/Manager.spec.ts index 8705760..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,6 +124,16 @@ 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 add -D typescript"; config.manager = "npm"; @@ -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,6 +189,17 @@ 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 add -D nodemon"; config.manager = "npm"; @@ -160,6 +211,7 @@ describe("PackageManager", () => { stdio: "ignore", }); }); + it("should call installNodeTypes with npm", async () => { const cmd = "npm add -D @types/node"; manager.installNodeTypes(); @@ -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", + }); + }); });