Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions __tests__/Manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -121,14 +147,15 @@ 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, {
cwd: fakePath,
stdio: "ignore",
});
});

it("should call installDiscordJS with yarn", async () => {
const cmd = "yarn add discord.js@latest";
config.manager = "yarn";
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -160,15 +211,17 @@ 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, {
cwd: fakePath,
stdio: "ignore",
});
});

it("should call installNodeTypes with yarn", async () => {
const cmd = "yarn add -D @types/node";
config.manager = "yarn";
Expand All @@ -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",
});
});
});
18 changes: 13 additions & 5 deletions src/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ export class PackageManager implements Initializer {
async initialize(config: SlappeyConfig, filePath: string): Promise<void> {
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() {
Expand All @@ -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 }
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export const packageManager: Array<PromptObject> = [
title: 'yarn',
value: 'yarn',
},
{
title: 'pnpm',
value: 'pnpm',
},
{
title: 'npm',
value: 'npm',
Expand Down
2 changes: 1 addition & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down