mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
feat: improve cross-uni app workflow
This commit is contained in:
36
packages/angular-builder/src/icons/check-icon-correctness.ts
Normal file
36
packages/angular-builder/src/icons/check-icon-correctness.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Font, open} from 'fontkit';
|
||||
import {existsSync} from 'fs';
|
||||
import {getUsedIcons} from './gather-used-icons.js';
|
||||
import {fetchCodePointMap} from './get-code-points.js';
|
||||
import type {IconConfig} from '../index.js';
|
||||
|
||||
export async function checkIconCorrectness(config: IconConfig) {
|
||||
if (!existsSync(config.outputPath)) {
|
||||
throw new Error('Icons have not been generated');
|
||||
}
|
||||
|
||||
const modifiedFont = (await open(config.outputPath)) as Font;
|
||||
const codePoints = await fetchCodePointMap();
|
||||
|
||||
for (const icon of await getUsedIcons(config)) {
|
||||
const codePoint = codePoints.get(icon);
|
||||
if (!codePoint) throw new Error(`"${icon}" is not a valid icon`);
|
||||
if (!modifiedFont.getGlyph(Number.parseInt(codePoint, 16))) {
|
||||
throw new Error(`"${icon}" (code point ${codePoint}) is missing`);
|
||||
}
|
||||
}
|
||||
}
|
||||
55
packages/angular-builder/src/icons/gather-used-icons.ts
Normal file
55
packages/angular-builder/src/icons/gather-used-icons.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {glob} from 'glob';
|
||||
import {readFileSync} from 'fs';
|
||||
import {matchPropertyAccess, matchPropertyContent, matchTagProperties} from './icon-match.js';
|
||||
import {IconConfig} from '../index.js';
|
||||
|
||||
export async function getUsedIconsHtml(pattern = 'src/**/*.html'): Promise<Record<string, string[]>> {
|
||||
return Object.fromEntries(
|
||||
(await glob(pattern))
|
||||
.map(file => [
|
||||
file,
|
||||
readFileSync(file, 'utf8')
|
||||
.match(matchTagProperties('ion-icon'))
|
||||
?.flatMap(match => {
|
||||
return match.match(matchPropertyContent(['name', 'md', 'ios']));
|
||||
})
|
||||
.filter(it => !!it) || [],
|
||||
])
|
||||
.filter(([, values]) => values && values.length > 0),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getUsedIconsTS(pattern = 'src/**/*.ts'): Promise<Record<string, string[]>> {
|
||||
const regex = matchPropertyAccess('SCIcon');
|
||||
return Object.fromEntries(
|
||||
(await glob(pattern))
|
||||
.map(file => [file, readFileSync(file, 'utf8').match(regex) || []])
|
||||
.filter(([, values]) => values && values.length > 0),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getUsedIcons(config: IconConfig) {
|
||||
return new Set(
|
||||
[
|
||||
config.additionalIcons ?? {},
|
||||
await getUsedIconsTS(config.scriptGlob),
|
||||
await getUsedIconsHtml(config.htmlGlob),
|
||||
]
|
||||
.map(Object.values)
|
||||
.flat(2),
|
||||
);
|
||||
}
|
||||
20
packages/angular-builder/src/icons/get-code-points.ts
Normal file
20
packages/angular-builder/src/icons/get-code-points.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
const url =
|
||||
'https://raw.githubusercontent.com/google/material-design-icons/master/' +
|
||||
'variablefont/MaterialSymbolsRounded%5BFILL%2CGRAD%2Copsz%2Cwght%5D.codepoints';
|
||||
|
||||
export async function fetchCodePointMap() {
|
||||
const icons = await fetch(url)
|
||||
.then(it => it.text())
|
||||
.then(it => new Map(it.split('\n').map(it => it.split(' ') as [string, string])));
|
||||
if (icons.size < 100) throw new Error(`Code point map is very small, is the URL incorrect? ${url}`);
|
||||
return icons;
|
||||
}
|
||||
|
||||
export async function getCodePoints(icons: string[]) {
|
||||
const codePoints = await fetchCodePointMap();
|
||||
return icons.map(icon => {
|
||||
const code = codePoints.get(icon);
|
||||
if (!code) throw new Error(`Code point for icon ${icon} not found`);
|
||||
return code;
|
||||
});
|
||||
}
|
||||
11
packages/angular-builder/src/icons/icon-config.ts
Normal file
11
packages/angular-builder/src/icons/icon-config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import {cosmiconfig} from 'cosmiconfig';
|
||||
import type {IconConfig} from '../index.js';
|
||||
|
||||
export async function getIconConfig(): Promise<IconConfig> {
|
||||
const explorer = cosmiconfig('icons');
|
||||
const result = await explorer.search();
|
||||
if (!result) {
|
||||
throw new Error('No icon configuration found');
|
||||
}
|
||||
return result.config;
|
||||
}
|
||||
92
packages/angular-builder/src/icons/icon-match.spec.ts
Normal file
92
packages/angular-builder/src/icons/icon-match.spec.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* eslint-disable unicorn/no-null */
|
||||
import {expect} from 'chai';
|
||||
import {matchPropertyAccess, matchPropertyContent, matchTagProperties} from './icon-match.js';
|
||||
import {describe} from 'mocha';
|
||||
|
||||
describe('matchTagProperties', function () {
|
||||
const regex = matchTagProperties('test');
|
||||
|
||||
it('should match html tag content', function () {
|
||||
expect('<test content></test>'.match(regex)).to.deep.equal([' content']);
|
||||
});
|
||||
|
||||
it('should match all tags', function () {
|
||||
expect('<test content1></test> <test content2></test>'.match(regex)).to.deep.equal([
|
||||
' content1',
|
||||
' content2',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should not match wrong tags', function () {
|
||||
expect('<no content></no>'.match(regex)).to.deep.equal(null);
|
||||
});
|
||||
|
||||
it('should accept valid html whitespaces', function () {
|
||||
expect(
|
||||
`
|
||||
<test
|
||||
content
|
||||
>
|
||||
</test
|
||||
>
|
||||
`.match(regex),
|
||||
).to.deep.equal(['\n content\n ']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('matchPropertyContent', function () {
|
||||
const regex = matchPropertyContent(['test1', 'test2']);
|
||||
|
||||
it('should match bare literals', function () {
|
||||
expect(`test1="content" test2="content1"`.match(regex)).to.deep.equal(['content', 'content1']);
|
||||
});
|
||||
|
||||
it('should match angular literals', function () {
|
||||
expect(`[test1]="'content'" [test2]="'content1'"`.match(regex)).to.deep.equal(['content', 'content1']);
|
||||
});
|
||||
|
||||
it('should not match wrong literals', function () {
|
||||
expect(`no="content" [no]="'content'"`.match(regex)).to.deep.equal(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('matchPropertyAccess', function () {
|
||||
const property = '0_20a_boninAo0_';
|
||||
const object = 'test';
|
||||
const regex = matchPropertyAccess(object);
|
||||
|
||||
it('should match property access', function () {
|
||||
expect(`${object}.${property}`.match(regex)).to.deep.equal([property]);
|
||||
});
|
||||
|
||||
it('should respect whitespace', function () {
|
||||
expect(`${object}. ${property}`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object} .${property}`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object} . ${property}`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object} \n . \n ${property}`.match(regex)).to.deep.equal([property]);
|
||||
});
|
||||
|
||||
it('should not include invalid trailing stuff', function () {
|
||||
expect(`${object}.${property}!`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object}.${property}.`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object}.${property}-`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object}.${property}]`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object}.${property}}`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object}.${property};`.match(regex)).to.deep.equal([property]);
|
||||
expect(`${object}.${property}:`.match(regex)).to.deep.equal([property]);
|
||||
});
|
||||
});
|
||||
28
packages/angular-builder/src/icons/icon-match.ts
Normal file
28
packages/angular-builder/src/icons/icon-match.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export function matchTagProperties(tag: string) {
|
||||
return new RegExp(`(?<=<${tag})[\\s\\S]*?(?=>\\s*<\\/${tag}\\s*>)`, 'g');
|
||||
}
|
||||
|
||||
export function matchPropertyContent(properties: string[]) {
|
||||
const names = properties.join('|');
|
||||
|
||||
return new RegExp(`((?<=(${names})=")[\\w-]+(?="))|((?<=\\[(${names})]="')[\\w-]+(?='"))`, 'g');
|
||||
}
|
||||
|
||||
export function matchPropertyAccess(objectName: string) {
|
||||
return new RegExp(`(?<=${objectName}\\s*\\.\\s*)\\w+`, 'g');
|
||||
}
|
||||
Reference in New Issue
Block a user