-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a script that can generate .gts components from the .svg icons in the public folder. This setup saves us a lot of manual work and it easily allows us to manage the icons we bundle.
- Loading branch information
Showing
7 changed files
with
97 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# build scripts | ||
/lib/generate-icon-components.mjs | ||
|
||
# compiled output | ||
/dist/ | ||
/tmp/ | ||
|
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,59 @@ | ||
import { pascalCase } from "change-case"; | ||
import { existsSync } from "node:fs"; | ||
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises"; | ||
import { basename, dirname, extname, join } from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
||
const ICON_FOLDER = join(__dirname, "..", "public", "icons"); | ||
const COMPONENT_ICON_FOLDER = join( | ||
__dirname, | ||
"..", | ||
"addon", | ||
"components", | ||
"icons", | ||
); | ||
|
||
const files = await readdir(ICON_FOLDER); | ||
const icons = files | ||
.filter((file) => extname(file) === ".svg") | ||
.map((svg) => basename(svg, ".svg")); | ||
|
||
await prepareOutputDir(); | ||
|
||
const promises = icons.map((svg) => { | ||
return generateComponent(svg); | ||
}); | ||
await Promise.all(promises); | ||
|
||
async function generateComponent(iconName) { | ||
const componentName = pascalCase(iconName, { | ||
mergeAmbiguousCharacters: true, | ||
}); | ||
|
||
const iconContent = (await readFile(join(ICON_FOLDER, iconName + ".svg"))) | ||
.toString() | ||
.replace(">", " ...attributes>"); // We assume the first closing bracket belongs to the svg element | ||
|
||
const componentContent = `// THIS FILE IS GENERATED. ANY CHANGES TO THIS FILE WILL BE LOST. | ||
import type { TOC } from '@ember/component/template-only'; | ||
export interface ${componentName}IconSignature { | ||
Element: SVGSVGElement; | ||
} | ||
export const ${componentName}Icon: TOC<${componentName}IconSignature> = <template>${iconContent}</template>;`; | ||
|
||
await writeFile( | ||
join(COMPONENT_ICON_FOLDER, iconName + ".gts"), | ||
componentContent, | ||
); | ||
} | ||
|
||
async function prepareOutputDir() { | ||
if (existsSync(COMPONENT_ICON_FOLDER)) { | ||
await rm(COMPONENT_ICON_FOLDER, { recursive: true }); | ||
} | ||
await mkdir(COMPONENT_ICON_FOLDER); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import { AddIcon } from '@appuniversum/ember-appuniversum/components/icons/add'; | ||
|
||
module('Integration | Icon components', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('the icon components accept attributes', async function (assert) { | ||
await render(<template><AddIcon data-test-icon /></template>); | ||
|
||
assert.dom('[data-test-icon]').exists(); | ||
}); | ||
}); |