Skip to content

Commit ee2b94e

Browse files
Add initial tools utilities (#3)
1 parent bbcc72f commit ee2b94e

15 files changed

Lines changed: 16704 additions & 6 deletions

.github/workflows/tests.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
runs-on: ubuntu-latest
8+
env:
9+
WIREIT_FAILURES: continue
10+
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- uses: actions/setup-node@v3
15+
with:
16+
node-version: 18
17+
cache: npm
18+
cache-dependency-path: package-lock.json
19+
20+
- uses: google/wireit@setup-github-actions-caching/v1
21+
22+
- name: NPM install
23+
run: npm ci
24+
25+
- name: Test
26+
run: npm test

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ node_modules/
22
.wireit/
33
.eslintcache
44

5-
packages/**/lib/
6-
packages/**/tsconfig.tsbuildinfo
5+
packages/*/lib/
6+
packages/*/tsconfig.tsbuildinfo

package-lock.json

Lines changed: 85 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"scripts": {
66
"build": "wireit",
7+
"test": "wireit",
78
"format": "prettier \"./packages/*/src/**/*.ts\" --write",
89
"check": "wireit"
910
},
@@ -24,6 +25,11 @@
2425
"./packages/validator:build"
2526
]
2627
},
28+
"test": {
29+
"dependencies": [
30+
"./packages/tools:test"
31+
]
32+
},
2733
"check": {
2834
"dependencies": [
2935
"check:lint",

packages/tools/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/lib/
2-
/test/
2+
/test/*
3+
!/test/test-data/
4+
35
/index.js
46
/index.js.map
57
/index.d.ts

packages/tools/package.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"type": "module",
99
"main": "index.js",
1010
"scripts": {
11-
"build": "wireit"
11+
"build": "wireit",
12+
"test": "wireit"
1213
},
1314
"wireit": {
1415
"build": {
@@ -25,9 +26,27 @@
2526
"index.d.ts.map",
2627
"lib/",
2728
"test/",
29+
"!test/test-data/*",
2830
"tsconfig.tsbuildinfo"
2931
],
3032
"clean": "if-file-deleted"
33+
},
34+
"test": {
35+
"command": "NODE_OPTIONS='--enable-source-maps' uvu test \"_test\\.js$\"",
36+
"dependencies": [
37+
"build"
38+
],
39+
"files": [
40+
"test/test-data/*"
41+
],
42+
"output": []
3143
}
44+
},
45+
"dependencies": {
46+
"custom-elements-manifest": "^2.0.0"
47+
},
48+
"devDependencies": {
49+
"@types/node": "^18.16.3",
50+
"uvu": "^0.5.6"
3251
}
3352
}

packages/tools/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
export {};
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
export * from './lib/get-custom-elements.js';
8+
export * from './lib/resolve-reference.js';
9+
export * from './lib/get-module.js';
10+
export * from './lib/predicates.js';
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
import type {
8+
CustomElementDeclaration,
9+
CustomElementExport,
10+
Module,
11+
Package,
12+
Reference,
13+
} from 'custom-elements-manifest/schema.js';
14+
import {isCustomElementDeclaration} from './predicates.js';
15+
import {resolveReference} from './resolve-reference.js';
16+
17+
export type CustomElementInfo = {
18+
package: Package;
19+
module: Module;
20+
export: CustomElementExport;
21+
declaration: CustomElementDeclaration;
22+
declarationReference: Reference;
23+
};
24+
25+
/**
26+
* Gets all the custom element exports of a package
27+
*
28+
* @param pkg The package to search
29+
* @param packageName The name of the package
30+
* @param packageVersion The version of the package
31+
*/
32+
export const getCustomElements = (
33+
pkg: Package,
34+
packageName: string,
35+
packageVersion: string
36+
): Array<CustomElementInfo> => {
37+
const customElements: Array<CustomElementInfo> = [];
38+
for (const mod of pkg.modules) {
39+
if (mod.exports) {
40+
for (const e of mod.exports) {
41+
if (e.kind === 'custom-element-definition') {
42+
// TODO (justinfagnani): for large manifests we want to index ahead
43+
// of time to avoid polynomial lookups
44+
const decl = resolveReference(
45+
pkg,
46+
mod,
47+
e.declaration,
48+
packageName,
49+
packageVersion
50+
);
51+
if (decl !== undefined && isCustomElementDeclaration(decl)) {
52+
customElements.push({
53+
package: pkg,
54+
module: mod,
55+
export: e,
56+
declaration: decl,
57+
declarationReference: e.declaration,
58+
});
59+
} else {
60+
// This is some kind of manifest error, should we warn?
61+
// Or assume it was handled in a validation pass?
62+
}
63+
}
64+
}
65+
}
66+
}
67+
return customElements;
68+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
import type {Package} from 'custom-elements-manifest/schema.js';
8+
9+
/**
10+
* Gets a module by path from a package.
11+
*/
12+
export const getModule = (pkg: Package, path: string) => {
13+
path = normalizeModulePath(path);
14+
for (const mod of pkg.modules) {
15+
const modulePath = normalizeModulePath(mod.path);
16+
if (modulePath === path) {
17+
return mod;
18+
}
19+
}
20+
return undefined;
21+
};
22+
23+
/**
24+
* Normalizes a module path by removing leading slashes.
25+
*/
26+
export const normalizeModulePath = (path: string) =>
27+
path.startsWith('/') ? path.substring(1) : path;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
import type {
8+
Declaration,
9+
ClassDeclaration,
10+
CustomElementDeclaration,
11+
FunctionDeclaration,
12+
CustomElement,
13+
CustomElementMixinDeclaration,
14+
} from 'custom-elements-manifest/schema.js';
15+
16+
export const isClassDeclaration = (d: Declaration): d is ClassDeclaration =>
17+
d.kind === 'class';
18+
19+
export const isFunctionDeclaration = (
20+
d: Declaration
21+
): d is FunctionDeclaration => d.kind === 'function';
22+
23+
export const isMixinDeclaration = (d: Declaration): d is FunctionDeclaration =>
24+
d.kind === 'mixin';
25+
26+
export const isVariableDeclaration = (
27+
d: Declaration
28+
): d is FunctionDeclaration => d.kind === 'variable';
29+
30+
export const isCustomElement = (
31+
d: Declaration
32+
): d is CustomElementDeclaration | CustomElementMixinDeclaration =>
33+
(d as CustomElement).customElement === true;
34+
35+
export const isCustomElementDeclaration = (
36+
d: Declaration
37+
): d is CustomElementDeclaration => isClassDeclaration(d) && isCustomElement(d);
38+
39+
export const isCustomElementMixinDeclaration = (
40+
d: Declaration
41+
): d is CustomElementMixinDeclaration =>
42+
isMixinDeclaration(d) && isCustomElement(d);

0 commit comments

Comments
 (0)