-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): initial collect command (#45)
Co-authored-by: Michael <[email protected]> Co-authored-by: Michael Hladky <[email protected]>
- Loading branch information
1 parent
37ea0a5
commit ba048be
Showing
28 changed files
with
531 additions
and
332 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
41 changes: 41 additions & 0 deletions
41
packages/cli/src/lib/collect/_command-object-config.mock.js
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,41 @@ | ||
const { runnerOutputSchema } = require('@quality-metrics/models'); | ||
module.exports = { | ||
persist: { outputPath: 'command-object-config-out.json' }, | ||
plugins: [ | ||
{ | ||
audits: [ | ||
{ | ||
slug: 'command-object-audit-slug', | ||
title: 'audit title', | ||
description: 'audit description', | ||
label: 'mock audit label', | ||
docsUrl: 'http://www.my-docs.dev', | ||
}, | ||
], | ||
runner: { | ||
command: 'bash', | ||
args: [ | ||
'-c', | ||
`echo '${JSON.stringify( | ||
runnerOutputSchema.parse({ | ||
audits: [ | ||
{ | ||
slug: 'command-object-audit-slug', | ||
value: 0, | ||
score: 0, | ||
}, | ||
], | ||
}), | ||
)}' > command-object-config-out.json`, | ||
], | ||
outputPath: 'command-object-config-out.json', | ||
}, | ||
groups: [], | ||
meta: { | ||
slug: 'command-object-plugin', | ||
name: 'command-object plugin', | ||
}, | ||
}, | ||
], | ||
categories: [], | ||
}; |
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,90 @@ | ||
import { CoreConfig, PluginConfig, Report } from '@quality-metrics/models'; | ||
import { CollectOptions } from '@quality-metrics/utils'; | ||
import { readFileSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import { yargsCli } from '../cli'; | ||
import { getDirname } from '../implementation/utils'; | ||
import { middlewares } from '../middlewares'; | ||
import { yargsGlobalOptionsDefinition } from '../options'; | ||
import { yargsCollectCommandObject } from './command-object'; | ||
|
||
const outputPath = 'collect-command-object.json'; | ||
const dummyConfig: CoreConfig = { | ||
persist: { outputPath }, | ||
plugins: [mockPlugin()], | ||
categories: [], | ||
}; | ||
|
||
describe('collect-command-object', () => { | ||
it('should parse arguments correctly', async () => { | ||
const args = ['collect', '--verbose', '--configPath', '']; | ||
const cli = yargsCli([], { options: yargsGlobalOptionsDefinition() }) | ||
.config(dummyConfig) | ||
.command(yargsCollectCommandObject()); | ||
const parsedArgv = (await cli.parseAsync( | ||
args, | ||
)) as unknown as CollectOptions; | ||
const { persist } = parsedArgv; | ||
const { outputPath: outPath } = persist; | ||
expect(outPath).toBe(outputPath); | ||
return Promise.resolve(void 0); | ||
}); | ||
|
||
it('should execute middleware correctly', async () => { | ||
const args = [ | ||
'collect', | ||
'--configPath', | ||
join( | ||
getDirname(import.meta.url), | ||
'..', | ||
'implementation', | ||
'mock', | ||
'config-middleware-config.mock.mjs', | ||
), | ||
]; | ||
await yargsCli([], { middlewares }) | ||
.config(dummyConfig) | ||
.command(yargsCollectCommandObject()) | ||
.parseAsync(args); | ||
const report = JSON.parse(readFileSync(outputPath).toString()) as Report; | ||
expect(report.plugins[0]?.meta.slug).toBe('collect-command-object'); | ||
expect(report.plugins[0]?.audits[0]?.slug).toBe( | ||
'command-object-audit-slug', | ||
); | ||
}); | ||
}); | ||
|
||
function mockPlugin(): PluginConfig { | ||
return { | ||
audits: [ | ||
{ | ||
slug: 'command-object-audit-slug', | ||
title: 'audit title', | ||
description: 'audit description', | ||
label: 'mock audit label', | ||
docsUrl: 'http://www.my-docs.dev', | ||
}, | ||
], | ||
runner: { | ||
command: 'bash', | ||
args: [ | ||
'-c', | ||
`echo '${JSON.stringify({ | ||
audits: [ | ||
{ | ||
slug: 'command-object-audit-slug', | ||
value: 0, | ||
score: 0, | ||
}, | ||
], | ||
})}' > ${outputPath}`, | ||
], | ||
outputPath, | ||
}, | ||
groups: [], | ||
meta: { | ||
slug: 'collect-command-object', | ||
name: 'collect command object', | ||
}, | ||
} satisfies PluginConfig; | ||
} |
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,18 @@ | ||
import { collect, CollectOptions } from '@quality-metrics/utils'; | ||
import { writeFile } from 'fs/promises'; | ||
import { CommandModule } from 'yargs'; | ||
|
||
export function yargsCollectCommandObject() { | ||
const handler = async (args: CollectOptions): Promise<void> => { | ||
const collectOutput = await collect(args); | ||
|
||
const { persist } = args; | ||
await writeFile(persist.outputPath, JSON.stringify(collectOutput, null, 2)); | ||
}; | ||
|
||
return { | ||
command: 'collect', | ||
describe: 'Run Plugins and collect results', | ||
handler: handler as unknown as CommandModule['handler'], | ||
} satisfies CommandModule; | ||
} |
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,3 +1,4 @@ | ||
import { CommandModule } from 'yargs'; | ||
import { yargsCollectCommandObject } from './collect/command-object'; | ||
|
||
export const commands: CommandModule[] = []; | ||
export const commands: CommandModule[] = [yargsCollectCommandObject()]; |
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 |
---|---|---|
@@ -1,21 +1,28 @@ | ||
export { CategoryConfig, categoryConfigSchema } from './lib/category-config'; | ||
export { | ||
unrefinedCoreConfigSchema, | ||
refineCoreConfig, | ||
coreConfigSchema, | ||
CoreConfig, | ||
coreConfigSchema, | ||
refineCoreConfig, | ||
unrefinedCoreConfigSchema, | ||
} from './lib/core-config'; | ||
export { uploadConfigSchema, UploadConfig } from './lib/upload-config'; | ||
export { GlobalCliArgs, globalCliArgsSchema } from './lib/global-cli-options'; | ||
export { PersistConfig, persistConfigSchema } from './lib/persist-config'; | ||
export { | ||
pluginConfigSchema, | ||
AuditGroup, | ||
AuditMetadata, | ||
Issue, | ||
PluginConfig, | ||
RunnerOutput, | ||
auditGroupSchema, | ||
auditMetadataSchema, | ||
issueSchema, | ||
pluginConfigSchema, | ||
runnerOutputSchema, | ||
} from './lib/plugin-config'; | ||
export { | ||
runnerOutputAuditRefsPresentInPluginConfigs, | ||
reportSchema, | ||
PluginOutput, | ||
PluginReport, | ||
Report, | ||
runnerOutputAuditRefsPresentInPluginConfigs, | ||
} from './lib/report'; | ||
export { persistConfigSchema, PersistConfig } from './lib/persist-config'; | ||
export { categoryConfigSchema, CategoryConfig } from './lib/category-config'; | ||
export { globalCliArgsSchema, GlobalCliArgs } from './lib/global-cli-options'; | ||
export { UploadConfig, uploadConfigSchema } from './lib/upload-config'; |
Oops, something went wrong.