-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
--tag
support to cli (#693)
- Loading branch information
Showing
8 changed files
with
258 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { QuestionCollection } from 'inquirer'; | ||
import { HttpFile, HttpRegion } from '../../store'; | ||
import { selectHttpFiles } from './selectHttpFiles'; | ||
|
||
function createHttpFile(name: string, httpRegions: Array<Record<string, string>>) { | ||
const httpFile = new HttpFile(name); | ||
httpRegions.forEach((metaData, index) => { | ||
const httpRegion = new HttpRegion(httpFile, index); | ||
httpFile.httpRegions.push(httpRegion); | ||
Object.assign(httpRegion.metaData, metaData); | ||
httpRegion.symbol.name = metaData.name; | ||
}); | ||
return httpFile; | ||
} | ||
|
||
describe('selectHttpFiles', () => { | ||
const defaultHttpFiles: Array<HttpFile> = [ | ||
createHttpFile('test1', [ | ||
{ | ||
name: 'foo1', | ||
tag: 'foo', | ||
}, | ||
{ | ||
name: 'foo2', | ||
tag: 'foo', | ||
}, | ||
{ | ||
name: 'foo3', | ||
tag: 'bar', | ||
}, | ||
{ | ||
name: 'foo4', | ||
tag: 'bar', | ||
}, | ||
{ | ||
name: 'foo5', | ||
tag: 'fuu', | ||
}, | ||
]), | ||
createHttpFile('test2', [ | ||
{ | ||
name: 'test1', | ||
tag: 'test', | ||
}, | ||
{ | ||
name: 'test2', | ||
tag: 'test', | ||
}, | ||
]), | ||
]; | ||
it('should export', () => { | ||
expect(selectHttpFiles).toBeDefined(); | ||
}); | ||
|
||
it('should return all values', async () => { | ||
const result = await selectHttpFiles(defaultHttpFiles, { all: true }); | ||
|
||
expect(result.length).toBe(2); | ||
expect(result.map(h => h.httpFile.fileName)).toEqual(['test1', 'test2']); | ||
expect(result.map(h => h.httpRegions)).toEqual([undefined, undefined]); | ||
}); | ||
it('should return values by name', async () => { | ||
const result = await selectHttpFiles(defaultHttpFiles, { name: 'foo1' }); | ||
|
||
expect(result.length).toBe(1); | ||
expect(result.map(h => h.httpFile.fileName)).toEqual(['test1']); | ||
expect(result.map(h => h.httpRegions?.map(hr => hr.metaData.name))).toEqual([['foo1']]); | ||
}); | ||
it('should return values by tag', async () => { | ||
const result = await selectHttpFiles(defaultHttpFiles, { tag: ['foo', 'fuu'] }); | ||
|
||
expect(result.length).toBe(1); | ||
expect(result.map(h => h.httpFile.fileName)).toEqual(['test1']); | ||
expect(result.map(h => h.httpRegions?.map(hr => hr.metaData.name))).toEqual([['foo1', 'foo2', 'foo5']]); | ||
}); | ||
it('should return values by line', async () => { | ||
const result = await selectHttpFiles(defaultHttpFiles, { line: 1 }); | ||
|
||
expect(result.length).toBe(2); | ||
expect(result.map(h => h.httpFile.fileName)).toEqual(['test1', 'test2']); | ||
expect(result.map(h => h.httpRegions?.map(hr => hr.metaData.name))).toEqual([['foo2'], ['test2']]); | ||
}); | ||
it('should return values by manual input', async () => { | ||
const inquirer = await import('inquirer'); | ||
Object.assign(inquirer.default, { | ||
prompt(questions: QuestionCollection) { | ||
const q = questions[0]; | ||
return { | ||
region: q.choices[1], | ||
}; | ||
}, | ||
}); | ||
const result = await selectHttpFiles(defaultHttpFiles, {}); | ||
|
||
expect(result.length).toBe(1); | ||
expect(result.map(h => h.httpFile.fileName)).toEqual(['test1']); | ||
expect(result.map(h => h.httpRegions?.map(hr => hr.metaData.name))).toEqual([['foo1']]); | ||
}); | ||
it('should return empty on invalid manual input', async () => { | ||
const inquirer = await import('inquirer'); | ||
Object.assign(inquirer.default, { | ||
prompt() { | ||
return { | ||
region: 'fii', | ||
}; | ||
}, | ||
}); | ||
const result = await selectHttpFiles(defaultHttpFiles, {}); | ||
|
||
expect(result.length).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import * as models from '../../models'; | ||
import * as utils from '../../utils'; | ||
import { SendOptions } from './options'; | ||
|
||
type SelectActionResult = Array<{ httpRegions?: Array<models.HttpRegion>; httpFile: models.HttpFile }>; | ||
|
||
export async function selectHttpFiles( | ||
httpFiles: Array<models.HttpFile>, | ||
cliOptions: SendOptions | ||
): Promise<SelectActionResult> { | ||
if (cliOptions.all) { | ||
return httpFiles.map(httpFile => ({ | ||
httpFile, | ||
})); | ||
} | ||
const resultWithArgs = selectHttpFilesWithArgs(httpFiles, cliOptions); | ||
if (resultWithArgs.length > 0) { | ||
return resultWithArgs; | ||
} | ||
return await selectManualHttpFiles(httpFiles); | ||
} | ||
|
||
function selectHttpFilesWithArgs(httpFiles: Array<models.HttpFile>, cliOptions: SendOptions) { | ||
const result: SelectActionResult = []; | ||
|
||
for (const httpFile of httpFiles) { | ||
const httpRegions = httpFile.httpRegions.filter(h => { | ||
if (hasName(h, cliOptions.name)) { | ||
return true; | ||
} | ||
if (hasTag(h, cliOptions.tag)) { | ||
return true; | ||
} | ||
if (isLine(h, cliOptions.line)) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (httpRegions.length > 0) { | ||
result.push({ | ||
httpFile, | ||
httpRegions, | ||
}); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
function hasName(httpRegion: models.HttpRegion, name: string | undefined) { | ||
if (name) { | ||
return httpRegion.metaData?.name === name; | ||
} | ||
return false; | ||
} | ||
|
||
function isLine(httpRegion: models.HttpRegion, line: number | undefined) { | ||
if (line !== undefined) { | ||
return line && httpRegion.symbol.startLine <= line && httpRegion.symbol.endLine >= line; | ||
} | ||
return false; | ||
} | ||
|
||
function hasTag(httpRegion: models.HttpRegion, tags: Array<string> | undefined) { | ||
if (tags && utils.isString(httpRegion.metaData?.tag)) { | ||
return tags.includes(httpRegion.metaData.tag); | ||
} | ||
return false; | ||
} | ||
|
||
async function selectManualHttpFiles(httpFiles: Array<models.HttpFile>): Promise<SelectActionResult> { | ||
const httpRegionMap: Record<string, SelectActionResult> = {}; | ||
const hasManyFiles = httpFiles.length > 1; | ||
const cwd = `${process.cwd()}`; | ||
for (const httpFile of httpFiles) { | ||
const fileName = utils.ensureString(httpFile.fileName)?.replace(cwd, '.'); | ||
httpRegionMap[hasManyFiles ? `${fileName}: all` : 'all'] = [{ httpFile }]; | ||
|
||
for (const httpRegion of httpFile.httpRegions) { | ||
if (!httpRegion.isGlobal()) { | ||
const name = httpRegion.symbol.name; | ||
httpRegionMap[hasManyFiles ? `${fileName}: ${name}` : name] = [ | ||
{ | ||
httpRegions: [httpRegion], | ||
httpFile, | ||
}, | ||
]; | ||
} | ||
} | ||
} | ||
const inquirer = await import('inquirer'); | ||
const answer = await inquirer.default.prompt([ | ||
{ | ||
type: 'list', | ||
name: 'region', | ||
message: 'please choose which region to use', | ||
choices: Object.entries(httpRegionMap).map(([key]) => key), | ||
}, | ||
]); | ||
if (answer.region && httpRegionMap[answer.region]) { | ||
return httpRegionMap[answer.region]; | ||
} | ||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export * from './getEnvironmentConfig'; | ||
export * from './httpFile'; | ||
export * from './httpFileStore'; | ||
export * from './httpRegion'; | ||
export * from './parser'; | ||
export * from './pluginStore'; | ||
export * from './userSessionStore'; |