|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { |
| 4 | + existsSync, mkdirSync, rmdirSync, writeFileSync, |
| 5 | +} from 'fs'; |
| 6 | + |
| 7 | +import { log, messages } from '../../../messages'; |
| 8 | +import { Widget } from '../../../types'; |
| 9 | + |
| 10 | +export class CreateDownloadedTemplate { |
| 11 | + // Name of the widget template supplied by the user |
| 12 | + _widgetTemplateName: string; |
| 13 | + |
| 14 | + // Name of the widget template directory created with the widget template name |
| 15 | + _widgetTemplateDir: string; |
| 16 | + |
| 17 | + _widget: Widget; |
| 18 | + |
| 19 | + // Name of the configuration file |
| 20 | + _configFile = 'config.json'; |
| 21 | + |
| 22 | + // Name of the schema file |
| 23 | + _schemaFile = 'schema.json'; |
| 24 | + |
| 25 | + // Name of the html template |
| 26 | + _templateFile = 'widget.html'; |
| 27 | + |
| 28 | + _queryFile = 'query.graphql'; |
| 29 | + |
| 30 | + _queryParamsFile = 'queryParams.json'; |
| 31 | + |
| 32 | + _queryParamsBuilderFile = 'queryParamsBuilder.json'; |
| 33 | + |
| 34 | + _schemaTranslationsFile = 'schema_translations.json'; |
| 35 | + |
| 36 | + _uuidFile = 'widget.yml'; |
| 37 | + |
| 38 | + constructor(widget: Widget, templateName: string) { |
| 39 | + this._widget = widget; |
| 40 | + this._widgetTemplateDir = `${templateName}/${widget.name}`; |
| 41 | + } |
| 42 | + |
| 43 | + createDirectory() { |
| 44 | + try { |
| 45 | + if (!existsSync(this._widgetTemplateDir)) { |
| 46 | + mkdirSync(this._widgetTemplateDir); |
| 47 | + log.success(messages.createWidgetTemplate.createSuccess(this._widget.name)); |
| 48 | + } |
| 49 | + } catch { |
| 50 | + throw new Error(messages.createWidgetTemplate.createError(this._widget.name)); |
| 51 | + } |
| 52 | + |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + createSchemaFile() { |
| 57 | + const configPath = `${this._widgetTemplateDir}/${this._schemaFile}`; |
| 58 | + if (!this._widget.schema) return this; |
| 59 | + const schema = JSON.stringify(this._widget.schema, null, 2); |
| 60 | + try { |
| 61 | + writeFileSync(configPath, schema); |
| 62 | + log.success(messages.createWidgetTemplate.createSuccess(this._schemaFile, configPath)); |
| 63 | + } catch { |
| 64 | + throw new Error(messages.createWidgetTemplate.createError(this._schemaFile, configPath)); |
| 65 | + } |
| 66 | + |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + createTemplateFile() { |
| 71 | + const templatePath = `${this._widgetTemplateDir}/${this._templateFile}`; |
| 72 | + if (!this._widget.template) return this; |
| 73 | + const downloadedHtmlTemplate = this._widget.template; |
| 74 | + try { |
| 75 | + writeFileSync(templatePath, downloadedHtmlTemplate); |
| 76 | + log.success(messages.createWidgetTemplate.createSuccess(this._templateFile, templatePath)); |
| 77 | + } catch { |
| 78 | + throw new Error(messages.createWidgetTemplate.createError(this._templateFile, templatePath)); |
| 79 | + } |
| 80 | + |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + createUuidFile() { |
| 85 | + const uuidPath = `${this._widgetTemplateDir}/${this._uuidFile}`; |
| 86 | + if (!this._widget.uuid) return this; |
| 87 | + const downloadedUuid = this._widget.uuid; |
| 88 | + try { |
| 89 | + writeFileSync(uuidPath, downloadedUuid); |
| 90 | + log.success(messages.createWidgetTemplate.createSuccess(this._uuidFile, uuidPath)); |
| 91 | + } catch { |
| 92 | + throw new Error(messages.createWidgetTemplate.createError(this._uuidFile, uuidPath)); |
| 93 | + } |
| 94 | + |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + createQueryFile() { |
| 99 | + const queryPath = `${this._widgetTemplateDir}/${this._queryFile}`; |
| 100 | + if (!this._widget.storefront_api_query) return this; |
| 101 | + const downloadedQuery = this._widget.storefront_api_query; |
| 102 | + try { |
| 103 | + writeFileSync(queryPath, downloadedQuery); |
| 104 | + log.success(messages.createWidgetTemplate.createSuccess(this._queryFile, queryPath)); |
| 105 | + } catch { |
| 106 | + throw new Error(messages.createWidgetTemplate.createError(this._queryFile, queryPath)); |
| 107 | + } |
| 108 | + |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + createSchemaTranslationsFile() { |
| 113 | + const schemaTranslationsPath = `${this._widgetTemplateDir}/${this._schemaTranslationsFile}`; |
| 114 | + if (!this._widget.schema_translations || Object.keys(this._widget.schema_translations).length === 0) return; |
| 115 | + const downloadedSchemaTranslations = JSON.stringify(this._widget.schema_translations, null, 2); |
| 116 | + try { |
| 117 | + writeFileSync(schemaTranslationsPath, downloadedSchemaTranslations); |
| 118 | + log.success(messages.createWidgetTemplate.createSuccess(this._schemaTranslationsFile, schemaTranslationsPath)); // eslint-disable-line max-len |
| 119 | + } catch { |
| 120 | + throw new Error(messages.createWidgetTemplate.createError(this._schemaTranslationsFile, schemaTranslationsPath)); // eslint-disable-line max-len |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + removeDirectory() { |
| 125 | + try { |
| 126 | + if (existsSync(this._widgetTemplateDir)) { |
| 127 | + rmdirSync(this._widgetTemplateDir, { recursive: true }); |
| 128 | + log.success(messages.createWidgetTemplate.removeSuccess(this._widgetTemplateDir)); |
| 129 | + } |
| 130 | + } catch { |
| 131 | + log.error(messages.createWidgetTemplate.removeError(this._widgetTemplateDir)); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +const generate = (widget: Widget, widgetTemplateName: string) => { |
| 137 | + const blankTemplate = new CreateDownloadedTemplate(widget, widgetTemplateName); |
| 138 | + |
| 139 | + try { |
| 140 | + blankTemplate |
| 141 | + .createDirectory() |
| 142 | + .createSchemaFile() |
| 143 | + .createTemplateFile() |
| 144 | + .createQueryFile() |
| 145 | + .createUuidFile() |
| 146 | + .createSchemaTranslationsFile(); |
| 147 | + } catch (e) { |
| 148 | + blankTemplate.removeDirectory(); |
| 149 | + throw e; |
| 150 | + } |
| 151 | +}; |
| 152 | + |
| 153 | +const createDownloadedWidgetTemplate = { |
| 154 | + generate, |
| 155 | +}; |
| 156 | + |
| 157 | +export default createDownloadedWidgetTemplate; |
0 commit comments