Skip to content

Commit

Permalink
ZETA-6456: Get package name and version. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieGreenman authored Aug 17, 2023
1 parent 6d7fc33 commit 7eb65ad
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/command-morph/command-morph.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getFirstWordOfCommand } from "../utils/word-search";
import { getFirstWordOfCommand } from "../utils/command-search/command-search";
import { npmCommandMorph } from "../npm/npm-command-morph";
import { CommandMorph } from "./command-morph.interface";

export function morphCommand(commandText: string): any {
export async function morphCommand(commandText: string): any {
const commandTool = getFirstWordOfCommand(commandText);
switch(commandTool) {
case CommandMorph.npm:
return npmCommandMorph(commandText);
return await npmCommandMorph(commandText);
}
}

6 changes: 3 additions & 3 deletions src/npm/npm-command-morph.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getSecondWordOfCommand } from "../utils/word-search"
import { getSecondWordOfCommand } from "../utils/command-search/command-search";
import { NpmCommandMorph } from "./interfaces/npm-command-morph.interface";
import { npmInstallCodemorph } from "./npm-install/npm-install";

export function npmCommandMorph(commandText: string): any {
export async function npmCommandMorph(commandText: string): any {
const commandType = getSecondWordOfCommand(commandText);
switch(commandType) {
case NpmCommandMorph.install:
return npmInstallCodemorph(commandText)
return await npmInstallCodemorph(commandText, '');
default:
return {}
}
Expand Down
8 changes: 4 additions & 4 deletions src/npm/npm-install/npm-install.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { morphCommand } from "../../command-morph/command-morph";

describe('npmInstall', () => {
it('should return npm install with needed codemod', () => {
const npmInstallCommandText = 'npm install test';
const result = morphCommand(npmInstallCommandText);
const expected = 'npm install test';
it('should return npm install with needed codemod', async() => {
const npmInstallCommandText = 'npm install apollo-angular';
const result = await morphCommand(npmInstallCommandText);
const expected = 'npm install apollo-angular';
expect(result).toEqual(expected);
});
});
11 changes: 9 additions & 2 deletions src/npm/npm-install/npm-install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// import axios from 'axios';
import { extractPackageName } from "../../utils/command-search/command-search";
import { getNameAndVersion } from "../../utils/version/version";

export function npmInstallCodemorph(commandText: string): string {
export async function npmInstallCodemorph(commandText: string, stringToUpdate: string): Promise<string> {
const packageName = extractPackageName(commandText);
const {name, version} = await getNameAndVersion(packageName);
console.log('name');
console.log(name);
console.log('version');
console.log(version);
return commandText;
}
42 changes: 42 additions & 0 deletions src/utils/command-search/command-search.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { extractPackageName, getFirstWordOfCommand, getSecondWordOfCommand } from "./command-search";

describe('WordSearch', () => {
describe('getFirstWordOfCommand', () => {
it('should get the first word of the command tool', () => {
const commandText = 'npm install text';
const result = getFirstWordOfCommand(commandText);
const expected = 'npm';
expect(result).toEqual(expected);
});
});

describe('getSecondWordOfCommand', () => {
it('should get the second word of the command tool', () => {
const commandText = 'npm install text';
const result = getSecondWordOfCommand(commandText);
const expected = 'install';
expect(result).toEqual(expected);
});
});

describe('extractPackageName', () => {
it('should extract the package name from a command', () => {
const sampleCommand = 'npm install apollo-angular';
const result = extractPackageName(sampleCommand);
expect(result).toEqual('apollo-angular');
});

it('should extract the package name from a command if has semicolon in front of it', () => {
const sampleCommand = 'npm install apollo-angular;';
const result = extractPackageName(sampleCommand);
expect(result).toEqual('apollo-angular');
});

it('should extract the package name from a command if has version and keep version in tact', () => {
const sampleCommand = 'npm install apollo-angular@^3.0.1;';
const result = extractPackageName(sampleCommand);
expect(result).toEqual('apollo-angular@^3.0.1');
});
})

});
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ export function getSecondWordOfCommand(commandText: string): NpmCommandMorph {
return words[1] as unknown as NpmCommandMorph;
}

export function extractPackageName(installCommand: string): string {
const matchesWithVersion = installCommand.match(/npm\s+install\s+([\w-]+@[\w.^-]+)/);
const matchesWithoutVersion = installCommand.match(/npm\s+install\s+([\w-]+)/);

if (matchesWithVersion && matchesWithVersion.length >= 2) {
return matchesWithVersion[1];
} else if (matchesWithoutVersion && matchesWithoutVersion.length >= 2) {
return matchesWithoutVersion[1];
}

return '';
}

22 changes: 0 additions & 22 deletions src/utils/word-search.spec.ts

This file was deleted.

0 comments on commit 7eb65ad

Please sign in to comment.