From 7eb65ad5dda793bf519d73aa117630bf8d95817f Mon Sep 17 00:00:00 2001 From: Charlie Greenman Date: Thu, 17 Aug 2023 15:42:29 -0700 Subject: [PATCH] ZETA-6456: Get package name and version. (#6) --- src/command-morph/command-morph.ts | 6 +-- src/npm/npm-command-morph.ts | 6 +-- src/npm/npm-install/npm-install.spec.ts | 8 ++-- src/npm/npm-install/npm-install.ts | 11 ++++- .../command-search/command-search.spec.ts | 42 +++++++++++++++++++ .../command-search.ts} | 13 ++++++ src/utils/word-search.spec.ts | 22 ---------- 7 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 src/utils/command-search/command-search.spec.ts rename src/utils/{word-search.ts => command-search/command-search.ts} (52%) delete mode 100644 src/utils/word-search.spec.ts diff --git a/src/command-morph/command-morph.ts b/src/command-morph/command-morph.ts index 897b28b..164bbcc 100644 --- a/src/command-morph/command-morph.ts +++ b/src/command-morph/command-morph.ts @@ -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); } } diff --git a/src/npm/npm-command-morph.ts b/src/npm/npm-command-morph.ts index cfd167e..ce50149 100644 --- a/src/npm/npm-command-morph.ts +++ b/src/npm/npm-command-morph.ts @@ -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 {} } diff --git a/src/npm/npm-install/npm-install.spec.ts b/src/npm/npm-install/npm-install.spec.ts index 87b4632..2d9e3b5 100644 --- a/src/npm/npm-install/npm-install.spec.ts +++ b/src/npm/npm-install/npm-install.spec.ts @@ -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); }); }); \ No newline at end of file diff --git a/src/npm/npm-install/npm-install.ts b/src/npm/npm-install/npm-install.ts index 65b18f4..5693a44 100644 --- a/src/npm/npm-install/npm-install.ts +++ b/src/npm/npm-install/npm-install.ts @@ -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 { + const packageName = extractPackageName(commandText); + const {name, version} = await getNameAndVersion(packageName); + console.log('name'); + console.log(name); + console.log('version'); + console.log(version); return commandText; } \ No newline at end of file diff --git a/src/utils/command-search/command-search.spec.ts b/src/utils/command-search/command-search.spec.ts new file mode 100644 index 0000000..4bbfb05 --- /dev/null +++ b/src/utils/command-search/command-search.spec.ts @@ -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'); + }); + }) + +}); \ No newline at end of file diff --git a/src/utils/word-search.ts b/src/utils/command-search/command-search.ts similarity index 52% rename from src/utils/word-search.ts rename to src/utils/command-search/command-search.ts index 280acd5..c19d5cb 100644 --- a/src/utils/word-search.ts +++ b/src/utils/command-search/command-search.ts @@ -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 ''; +} + diff --git a/src/utils/word-search.spec.ts b/src/utils/word-search.spec.ts deleted file mode 100644 index 8b3b4bc..0000000 --- a/src/utils/word-search.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { getFirstWordOfCommand, getSecondWordOfCommand } from "./word-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); - }); - }); - -}); \ No newline at end of file