Use Gulp to index demos (#6203)

This commit is contained in:
Paulus Schoutsen 2020-06-22 10:24:01 -07:00 committed by GitHub
parent 7bbecfde2b
commit c53fd0d1e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 12 deletions

View File

@ -34,10 +34,8 @@ jobs:
run: yarn install run: yarn install
env: env:
CI: true CI: true
- name: Build icons - name: Build resources
run: ./node_modules/.bin/gulp gen-icons-json run: ./node_modules/.bin/gulp gen-icons-json build-translations gather-gallery-demos
- name: Build translations
run: ./node_modules/.bin/gulp build-translations
- name: Run eslint - name: Run eslint
run: ./node_modules/.bin/eslint '{**/src,src}/**/*.{js,ts,html}' --ignore-path .gitignore run: ./node_modules/.bin/eslint '{**/src,src}/**/*.{js,ts,html}' --ignore-path .gitignore
- name: Run tsc - name: Run tsc

2
.gitignore vendored
View File

@ -24,7 +24,7 @@ dist
.vscode/* .vscode/*
!.vscode/extensions.json !.vscode/extensions.json
# Cast dev settings # Cast dev settings
src/cast/dev_const.ts src/cast/dev_const.ts
# Secrets # Secrets

View File

@ -1,7 +1,10 @@
// Run demo develop mode // Run demo develop mode
const gulp = require("gulp"); const gulp = require("gulp");
const fs = require("fs");
const path = require("path");
const env = require("../env"); const env = require("../env");
const paths = require("../paths");
require("./clean.js"); require("./clean.js");
require("./translations.js"); require("./translations.js");
@ -12,6 +15,31 @@ require("./service-worker.js");
require("./entry-html.js"); require("./entry-html.js");
require("./rollup.js"); require("./rollup.js");
gulp.task("gather-gallery-demos", async function gatherDemos() {
const files = await fs.promises.readdir(
path.resolve(paths.gallery_dir, "src/demos")
);
let content = "export const DEMOS = {\n";
for (const file of files) {
const demoId = path.basename(file, ".ts");
const demoPath = "../src/demos/" + demoId;
content += ` "${demoId}": () => import("${demoPath}"),\n`;
}
content += "};";
const galleryBuild = path.resolve(paths.gallery_dir, "build");
fs.mkdirSync(galleryBuild, { recursive: true });
fs.writeFileSync(
path.resolve(galleryBuild, "import-demos.ts"),
content,
"utf-8"
);
});
gulp.task( gulp.task(
"develop-gallery", "develop-gallery",
gulp.series( gulp.series(
@ -20,7 +48,11 @@ gulp.task(
}, },
"clean-gallery", "clean-gallery",
"translations-enable-merge-backend", "translations-enable-merge-backend",
gulp.parallel("gen-icons-json", "build-translations"), gulp.parallel(
"gen-icons-json",
"build-translations",
"gather-gallery-demos"
),
"copy-static-gallery", "copy-static-gallery",
"gen-index-gallery-dev", "gen-index-gallery-dev",
env.useRollup() ? "rollup-dev-server-gallery" : "webpack-dev-server-gallery" env.useRollup() ? "rollup-dev-server-gallery" : "webpack-dev-server-gallery"
@ -35,7 +67,11 @@ gulp.task(
}, },
"clean-gallery", "clean-gallery",
"translations-enable-merge-backend", "translations-enable-merge-backend",
gulp.parallel("gen-icons-json", "build-translations"), gulp.parallel(
"gen-icons-json",
"build-translations",
"gather-gallery-demos"
),
"copy-static-gallery", "copy-static-gallery",
env.useRollup() ? "rollup-prod-gallery" : "webpack-prod-gallery", env.useRollup() ? "rollup-prod-gallery" : "webpack-prod-gallery",
"gen-index-gallery-prod" "gen-index-gallery-prod"

View File

@ -11,9 +11,7 @@ import { PolymerElement } from "@polymer/polymer/polymer-element";
import "../../src/components/ha-card"; import "../../src/components/ha-card";
import "../../src/managers/notification-manager"; import "../../src/managers/notification-manager";
import "../../src/styles/polymer-ha-style"; import "../../src/styles/polymer-ha-style";
import { DEMOS } from "../build/import-demos";
// eslint-disable-next-line no-undef
const DEMOS = require.context("./demos", true, /^(.*\.(ts$))[^.]*$/im);
const fixPath = (path) => path.substr(2, path.length - 5); const fixPath = (path) => path.substr(2, path.length - 5);
@ -163,7 +161,7 @@ class HaGallery extends PolymerElement {
}, },
_demos: { _demos: {
type: Array, type: Array,
value: DEMOS.keys().map(fixPath), value: Object.keys(DEMOS),
}, },
_lovelaceDemos: { _lovelaceDemos: {
type: Array, type: Array,
@ -210,7 +208,7 @@ class HaGallery extends PolymerElement {
while (root.lastChild) root.removeChild(root.lastChild); while (root.lastChild) root.removeChild(root.lastChild);
if (demo) { if (demo) {
DEMOS(`./${demo}.ts`); DEMOS[demo]();
const el = document.createElement(demo); const el = document.createElement(demo);
root.appendChild(el); root.appendChild(el);
} }