-
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.
- Loading branch information
Showing
20 changed files
with
653 additions
and
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
**/*.mock.* | ||
**/code-pushup.config.ts | ||
**/mocks/fixtures/** | ||
**/__snapshots__/** |
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
15 changes: 0 additions & 15 deletions
15
...s/nx-plugin/src/generators/configuration/__snapshots__/generator.integration.test.ts.snap
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
packages/nx-plugin/src/generators/configuration/__snapshots__/root-code-pushup.config.ts
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,15 @@ | ||
import type { CoreConfig } from '../dist/packages/models'; | ||
import * as myPlugin from 'my-plugin'; | ||
import { myPluginCategory } from 'my-plugin'; | ||
|
||
// see: https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md#coreconfig | ||
export default { | ||
persist: { | ||
filename: 'report-123', | ||
}, | ||
update: { | ||
apiKey: '123', | ||
}, | ||
plugins: [myPlugin({ timeout: 42 })], | ||
categories: [myPluginCategory()], | ||
} satisfies CoreConfig; |
45 changes: 45 additions & 0 deletions
45
packages/nx-plugin/src/generators/configuration/code-pushup-config.integration.test.ts
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,45 @@ | ||
import * as devKit from '@nx/devkit'; | ||
import { formatFiles } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import { join } from 'node:path'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { generateCodePushupConfig } from './code-pushup-config'; | ||
|
||
describe('generateCodePushupConfig options', () => { | ||
let tree: devKit.Tree; | ||
const project = 'test-app'; | ||
const projectRoot = join('libs', project); | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
devKit.addProjectConfiguration(tree, project, { | ||
root: 'test-app', | ||
}); | ||
}); | ||
|
||
it('should create code-pushup.config.ts with options in tree', async () => { | ||
generateCodePushupConfig(tree, projectRoot, { | ||
fileImports: 'import type { CoreConfig } from "../dist/packages/models";', | ||
persist: { filename: 'report-123' }, | ||
upload: { apiKey: '123' }, | ||
plugins: [ | ||
{ | ||
fileImports: 'import * as myPlugin from "my-plugin";', | ||
codeStrings: 'myPlugin({ timeout: 42})', | ||
}, | ||
], | ||
categories: [ | ||
{ | ||
fileImports: 'import {myPluginCategory} from "my-plugin";', | ||
codeStrings: 'myPluginCategory()', | ||
}, | ||
], | ||
}); | ||
|
||
await formatFiles(tree); | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
expect( | ||
tree.read(join(projectRoot, 'code-pushup.config.ts'))?.toString(), | ||
).toMatchFileSnapshot('__snapshots__/root-code-pushup.config.ts'); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
packages/nx-plugin/src/generators/configuration/code-pushup-config.ts
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,71 @@ | ||
import { Tree, generateFiles, logger } from '@nx/devkit'; | ||
import { join } from 'node:path'; | ||
import type { PersistConfig, UploadConfig } from '@code-pushup/models'; | ||
import type { ItemOrArray } from '@code-pushup/utils'; | ||
import { ExecutableCode } from './types'; | ||
import { | ||
formatArrayToJSArray, | ||
formatArrayToLinesOfJsString, | ||
formatObjectToFormattedJsString, | ||
normalizeExecutableCode, | ||
normalizeItemOrArray, | ||
} from './utils'; | ||
|
||
export const DEFAULT_IMPORTS = [ | ||
"import type { CoreConfig } from '@code-pushup/models';", | ||
]; | ||
|
||
export type GenerateCodePushupConfigOptions = { | ||
fileImports?: ItemOrArray<string>; | ||
persist?: Partial<PersistConfig>; | ||
upload?: Partial<UploadConfig>; | ||
plugins?: ExecutableCode[]; | ||
categories?: ExecutableCode[]; | ||
}; | ||
|
||
export function generateCodePushupConfig( | ||
tree: Tree, | ||
root: string, | ||
options?: GenerateCodePushupConfigOptions, | ||
) { | ||
const supportedFormats = ['ts', 'mjs', 'js']; | ||
const firstExistingFormat = supportedFormats.find(ext => | ||
tree.exists(join(root, `code-pushup.config.${ext}`)), | ||
); | ||
if (firstExistingFormat) { | ||
logger.warn( | ||
`NOTE: No config file created as code-pushup.config.${firstExistingFormat} file already exists.`, | ||
); | ||
} else { | ||
const { | ||
fileImports: rawImports, | ||
persist, | ||
upload, | ||
plugins: rawPlugins = [], // plugins are required | ||
categories: rawCategories, | ||
} = options ?? {}; | ||
|
||
const plugins = rawPlugins.map(normalizeExecutableCode); | ||
const categories = rawCategories?.map(normalizeExecutableCode); | ||
const configFileImports = [ | ||
...(rawImports ? normalizeItemOrArray(rawImports) : DEFAULT_IMPORTS), | ||
...plugins.flatMap(({ fileImports }) => fileImports), | ||
...(categories ?? []).flatMap(({ fileImports }) => fileImports), | ||
]; | ||
|
||
generateFiles(tree, join(__dirname, 'files'), root, { | ||
...options, | ||
fileImports: formatArrayToLinesOfJsString(configFileImports), | ||
persist: formatObjectToFormattedJsString(persist), | ||
upload: formatObjectToFormattedJsString(upload), | ||
plugins: formatArrayToJSArray( | ||
plugins.flatMap(({ codeStrings }) => codeStrings), | ||
), | ||
categories: | ||
categories && | ||
formatArrayToJSArray( | ||
categories.flatMap(({ codeStrings }) => codeStrings), | ||
), | ||
}); | ||
} | ||
} |
Oops, something went wrong.