-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3727 from alphagov/component-libs
- Loading branch information
Showing
14 changed files
with
147 additions
and
111 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
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,52 @@ | ||
import { readFile } from 'fs/promises' | ||
import { join } from 'path' | ||
|
||
import fm from 'front-matter' | ||
import { paths } from 'govuk-frontend-config' | ||
import { getDirectories } from 'govuk-frontend-lib/files' | ||
|
||
/** | ||
* Get example names | ||
* | ||
* @returns {Promise<string[]>} Component names | ||
*/ | ||
export function getExampleNames () { | ||
return getDirectories(join(paths.app, 'src/views/examples')) | ||
} | ||
|
||
/** | ||
* Load all full page examples' front matter | ||
* | ||
* @returns {Promise<FullPageExample[]>} Full page examples | ||
*/ | ||
export async function getFullPageExamples () { | ||
const directories = await getDirectories(join(paths.app, 'src/views/full-page-examples')) | ||
|
||
// Add metadata (front matter) to each example | ||
const examples = await Promise.all(directories.map(async (exampleName) => { | ||
const templatePath = join(paths.app, 'src/views/full-page-examples', exampleName, 'index.njk') | ||
const { attributes } = fm(await readFile(templatePath, 'utf8')) | ||
|
||
return { | ||
name: exampleName, | ||
path: exampleName, | ||
...attributes | ||
} | ||
})) | ||
|
||
const collator = new Intl.Collator('en', { | ||
sensitivity: 'base' | ||
}) | ||
|
||
return examples.sort(({ name: a }, { name: b }) => | ||
collator.compare(a, b)) | ||
} | ||
|
||
/** | ||
* Full page example from front matter | ||
* | ||
* @typedef {object} FullPageExample | ||
* @property {string} name - Example name | ||
* @property {string} [scenario] - Description explaining the example | ||
* @property {string} [notes] - Additional notes about the example | ||
*/ |
40 changes: 40 additions & 0 deletions
40
packages/govuk-frontend-review/src/common/lib/files.test.mjs
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,40 @@ | ||
import { getFullPageExamples } from './files.mjs' | ||
|
||
describe('getFullPageExamples', () => { | ||
it('contains name of each example', async () => { | ||
const examples = await getFullPageExamples() | ||
|
||
examples.forEach((example) => expect(example).toEqual( | ||
expect.objectContaining({ | ||
name: expect.any(String), | ||
path: expect.any(String) | ||
}) | ||
)) | ||
}) | ||
|
||
it('contains scenario front matter, for some examples', async () => { | ||
const examples = await getFullPageExamples() | ||
|
||
// At least 1x example with { name, path, scenario } | ||
expect(examples).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
name: expect.any(String), | ||
path: expect.any(String), | ||
scenario: expect.any(String) | ||
}) | ||
])) | ||
}) | ||
|
||
it('contains notes front matter, for some examples', async () => { | ||
const examples = await getFullPageExamples() | ||
|
||
// At least 1x example with { name, path, notes } | ||
expect(examples).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
name: expect.any(String), | ||
path: expect.any(String), | ||
notes: expect.any(String) | ||
}) | ||
])) | ||
}) | ||
}) |
3 changes: 1 addition & 2 deletions
3
packages/govuk-frontend-review/src/routes/full-page-examples.mjs
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
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
Oops, something went wrong.