|
| 1 | +import { memo } from '@shopify/theme-check-common'; |
| 2 | +import { Dependencies } from '@shopify/theme-language-server-browser'; |
| 3 | +import { Connection } from 'vscode-languageserver/browser'; |
| 4 | + |
| 5 | +/** |
| 6 | + * These are replaced at build time by the contents of |
| 7 | + * @shopify/theme-check-docs-updater's DocsManager |
| 8 | + */ |
| 9 | +declare global { |
| 10 | + export const WEBPACK_TAGS: any[]; |
| 11 | + export const WEBPACK_FILTERS: any[]; |
| 12 | + export const WEBPACK_OBJECTS: any[]; |
| 13 | + export const WEBPACK_SYSTEM_TRANSLATIONS: any; |
| 14 | + export const WEBPACK_SCHEMAS: any; |
| 15 | +} |
| 16 | + |
| 17 | +const tags = WEBPACK_TAGS; |
| 18 | +const filters = WEBPACK_FILTERS; |
| 19 | +const objects = WEBPACK_OBJECTS; |
| 20 | +const systemTranslations = WEBPACK_SYSTEM_TRANSLATIONS; |
| 21 | +const schemas = WEBPACK_SCHEMAS; |
| 22 | + |
| 23 | +type ThemeDocset = Dependencies['themeDocset']; |
| 24 | +type JsonValidationSet = Dependencies['jsonValidationSet']; |
| 25 | + |
| 26 | +const fetchDeps = memo(async () => { |
| 27 | + return fetch('https://vs-code-for-web.shop.dev/deps.json').then((response) => { |
| 28 | + if (!response.ok) { |
| 29 | + console.error(response); |
| 30 | + throw new Error(`Failed to fetch dependencies: ${response.statusText}`); |
| 31 | + } |
| 32 | + return response.json(); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +export class ThemeDocsetManager implements ThemeDocset, JsonValidationSet { |
| 37 | + constructor(private connection: Connection) {} |
| 38 | + |
| 39 | + // Liquid documentation |
| 40 | + filters = memo(async () => this.fetchUpdatedData('filters', filters)); |
| 41 | + tags = memo(async () => this.fetchUpdatedData('tags', tags)); |
| 42 | + objects = memo(async () => this.fetchUpdatedData('objects', objects)); |
| 43 | + liquidDrops = memo(async () => this.fetchUpdatedData('objects', objects)); |
| 44 | + |
| 45 | + // prettier-ignore |
| 46 | + systemTranslations = memo(async () => this.fetchUpdatedData('systemTranslations', systemTranslations)); |
| 47 | + |
| 48 | + // JSON validation data |
| 49 | + schemas = memo(async () => this.fetchUpdatedData('schemas', schemas)); |
| 50 | + |
| 51 | + fetchUpdatedData = async <T>( |
| 52 | + dependency: 'filters' | 'tags' | 'objects' | 'systemTranslations' | 'schemas', |
| 53 | + fallback: T, |
| 54 | + ): Promise<T> => { |
| 55 | + return fetchDeps() |
| 56 | + .then((deps) => deps[dependency] ?? fallback) |
| 57 | + .then((data) => { |
| 58 | + console.error(`data received! ${dependency}`, data); |
| 59 | + return data as T; |
| 60 | + }) |
| 61 | + .catch(() => fallback); |
| 62 | + }; |
| 63 | +} |
0 commit comments