From d72fd72c404900921a8f2bdda8c3a157d4cd389a Mon Sep 17 00:00:00 2001 From: Luis Almeida Date: Wed, 28 Aug 2024 10:29:31 +0200 Subject: [PATCH 1/7] wip --- .../src/commands/themes/migrate.ts | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 packages/zcli-themes/src/commands/themes/migrate.ts diff --git a/packages/zcli-themes/src/commands/themes/migrate.ts b/packages/zcli-themes/src/commands/themes/migrate.ts new file mode 100644 index 00000000..ce4c7d07 --- /dev/null +++ b/packages/zcli-themes/src/commands/themes/migrate.ts @@ -0,0 +1,55 @@ +import { Command, Flags, CliUx } from '@oclif/core' +import * as path from 'path' +import * as chalk from 'chalk' +import { CLIError } from '@oclif/core/lib/errors' +import getManifest from '../../lib/getManifest' + +export default class Migrate extends Command { + static description = 'migrate a theme to the latest api_version' + + static enableJsonFlag = true + + static flags = { + backwardCompatible: Flags.boolean({ default: true, description: 'Use the new api but keep existing functionality and customizations', allowNo: true }) + } + + static args = [ + { name: 'themeDirectory', required: true, default: '.' } + ] + + static examples = [ + '$ zcli themes:migrate --backwardCompatible=false' + ] + + static strict = false + + async run () { + const { flags: { backwardCompatible }, argv: [themeDirectory] } = await this.parse(Migrate) + const themePath = path.resolve(themeDirectory) + const { api_version: apiVersion } = getManifest(themePath) + + try { + CliUx.ux.action.start('Migrating theme') + switch (apiVersion) { + case 1: + case 2: { + this.log(chalk.green('Migration from version 1 and 2 coming soon')) + break + } + case 3: { + // implement + break + } + case 4: { + this.log(chalk.green('Theme is already using the latest available api_version')) + break + } + default: + break + } + return {} + } catch (error) { + console.log(error) + } + } +} From 6abad52dd5321b2fa01c0bfdae58f377f84c04ee Mon Sep 17 00:00:00 2001 From: Luis Almeida Date: Thu, 29 Aug 2024 11:04:28 +0200 Subject: [PATCH 2/7] wip --- .../macros/new_request_page/follow_up.hbs | 3 + .../macros/new_request_page/request_form.hbs | 297 ++++++++++++++++++ .../src/commands/themes/migrate.ts | 3 +- packages/zcli-themes/src/lib/migrate.ts | 36 +++ 4 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 packages/zcli-themes/macros/new_request_page/follow_up.hbs create mode 100644 packages/zcli-themes/macros/new_request_page/request_form.hbs create mode 100644 packages/zcli-themes/src/lib/migrate.ts diff --git a/packages/zcli-themes/macros/new_request_page/follow_up.hbs b/packages/zcli-themes/macros/new_request_page/follow_up.hbs new file mode 100644 index 00000000..100cb96f --- /dev/null +++ b/packages/zcli-themes/macros/new_request_page/follow_up.hbs @@ -0,0 +1,3 @@ +{{#with parent}} + Follow-up to request #{{id}} +{{/with}} diff --git a/packages/zcli-themes/macros/new_request_page/request_form.hbs b/packages/zcli-themes/macros/new_request_page/request_form.hbs new file mode 100644 index 00000000..cdcf91df --- /dev/null +++ b/packages/zcli-themes/macros/new_request_page/request_form.hbs @@ -0,0 +1,297 @@ +{{#with new_request_form}} +
+ + + {{#if errors}} +
+ {{errors}} +
+ {{/if}} + + {{#with ticket_form_field}} + {{#if (compare options.length "==" 1)}} + + {{/if}} + {{#if (compare options.length ">" 1)}} +
+ + +
+ {{/if}} + {{/with}} + + {{#with email_field}} +
+ + + {{#if error}} +
+ {{error}} +
+ {{/if}} +
+ {{/with}} + + {{#with cc_field}} +
+ + + {{#if error}} +
{{error}}
+ {{else}} + + {{/if}} +
+ {{/with}} + + {{#with organization_field}} +
+ + +
+ {{/with}} + + {{#each ticket_fields}} + + {{#is type "subject"}} +
+ + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+
+ {{/is}} + + {{#is type "description"}} +
+ + + + + + + {{#if error}} +
{{error}}
+ {{/if}} +

{{description}}

+
+ {{/is}} + + {{#is type "tickettype"}} +
+ + + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{!-- "due date" is a special request field that should appear right after the "tickettype" field. --}} + {{#with ../due_date_field}} + + {{/with}} + {{/is}} + + {{#is type "priority"}} +
+ + + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{!-- TODO: fix --}} + {{#is type "tagger"}} +
+ + + + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{!-- TODO: fix --}} + {{#is type "multiselect"}} +
+ + + + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{#is type "text"}} +
+ + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{#is type "textarea"}} +
+ + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{#is type "integer"}} +
+ + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{#is type "decimal"}} +
+ + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{#is type "checkbox"}} +
+ + + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{#is type "date"}} +
+ + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{#is type "regexp"}} +
+ + + {{#if error}} +
{{error}}
+ {{/if}} + {{#if description}} +

{{description}}

+ {{/if}} +
+ {{/is}} + + {{/each}} + + {{!-- + {{conditional_fields}} + + {{attachments_field}} + + {{captcha_field}} + {{#if captcha_error_message}} +
+ {{captcha_error_message}} +
+ {{/if}} + + {{#if upload_error_message}} +
+ {{upload_error_message}} +
+ {{/if}} + --}} + +
+ +
+
+{{/with}} diff --git a/packages/zcli-themes/src/commands/themes/migrate.ts b/packages/zcli-themes/src/commands/themes/migrate.ts index ce4c7d07..10a0f9df 100644 --- a/packages/zcli-themes/src/commands/themes/migrate.ts +++ b/packages/zcli-themes/src/commands/themes/migrate.ts @@ -3,6 +3,7 @@ import * as path from 'path' import * as chalk from 'chalk' import { CLIError } from '@oclif/core/lib/errors' import getManifest from '../../lib/getManifest' +import migrate from '../../lib/migrate' export default class Migrate extends Command { static description = 'migrate a theme to the latest api_version' @@ -37,7 +38,7 @@ export default class Migrate extends Command { break } case 3: { - // implement + migrate(themePath) break } case 4: { diff --git a/packages/zcli-themes/src/lib/migrate.ts b/packages/zcli-themes/src/lib/migrate.ts new file mode 100644 index 00000000..320dc957 --- /dev/null +++ b/packages/zcli-themes/src/lib/migrate.ts @@ -0,0 +1,36 @@ +import * as path from 'path' +import * as fs from 'fs' +import { globSync } from 'glob' +import getManifest from './getManifest' +import getTemplates from './getTemplates' + +export default async function migrate (themePath: string): Promise { + migrateManifest(themePath) + migrateTemplates(themePath) +} + +function migrateManifest (themePath: string) { + const manifest = getManifest(themePath) + + manifest.api_version = 4 + + fs.writeFileSync(`${themePath}/manifest.json`, JSON.stringify(manifest, null, 2) + '\n') +} + +function migrateTemplates (themePath: string) { + const templates = getTemplates(themePath) + const macrosPath = path.join(__dirname, '..', '..', 'macros') + + for (const dentifier of Object.keys(templates)) { + let template = templates[dentifier] + const macros = globSync(`${macrosPath}/${dentifier}/*.hbs`.replace(/\\/g, '/'), { posix: true }) + + for (const macro of macros) { + const source = fs.readFileSync(macro, 'utf8') + const { name: helper } = path.parse(macro) + template = template.replace(`{{${helper}}}`, source) + } + + fs.writeFileSync(`${themePath}/templates/${dentifier}.hbs`, template) + } +} From abde0fcd5de9169b0cfb7cdbac0b03a966e53bf3 Mon Sep 17 00:00:00 2001 From: Luis Almeida Date: Fri, 30 Aug 2024 11:04:17 +0200 Subject: [PATCH 3/7] wip --- packages/zcli-themes/src/lib/migrate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/zcli-themes/src/lib/migrate.ts b/packages/zcli-themes/src/lib/migrate.ts index 320dc957..5dd52501 100644 --- a/packages/zcli-themes/src/lib/migrate.ts +++ b/packages/zcli-themes/src/lib/migrate.ts @@ -28,7 +28,7 @@ function migrateTemplates (themePath: string) { for (const macro of macros) { const source = fs.readFileSync(macro, 'utf8') const { name: helper } = path.parse(macro) - template = template.replace(`{{${helper}}}`, source) + template = template.replace(new RegExp(`{{${helper}.*}}`), source) } fs.writeFileSync(`${themePath}/templates/${dentifier}.hbs`, template) From fceb67c10b4418f7d1cbc00c85318ee7cf3e4b50 Mon Sep 17 00:00:00 2001 From: BrunoBFerreira Date: Fri, 30 Aug 2024 10:26:23 +0100 Subject: [PATCH 4/7] Add migrate theme without backwards compatibility --- .../src/commands/themes/migrate.ts | 22 +++++--- .../zcli-themes/src/lib/migrateNoBackwards.ts | 52 +++++++++++++++++++ .../src/templates/request_form_module.hbs | 31 +++++++++++ 3 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 packages/zcli-themes/src/lib/migrateNoBackwards.ts create mode 100644 packages/zcli-themes/src/templates/request_form_module.hbs diff --git a/packages/zcli-themes/src/commands/themes/migrate.ts b/packages/zcli-themes/src/commands/themes/migrate.ts index 10a0f9df..a12b1cc3 100644 --- a/packages/zcli-themes/src/commands/themes/migrate.ts +++ b/packages/zcli-themes/src/commands/themes/migrate.ts @@ -1,9 +1,9 @@ import { Command, Flags, CliUx } from '@oclif/core' import * as path from 'path' import * as chalk from 'chalk' -import { CLIError } from '@oclif/core/lib/errors' import getManifest from '../../lib/getManifest' import migrate from '../../lib/migrate' +import migrateNoBackwards from '../../lib/migrateNoBackwards' export default class Migrate extends Command { static description = 'migrate a theme to the latest api_version' @@ -11,7 +11,7 @@ export default class Migrate extends Command { static enableJsonFlag = true static flags = { - backwardCompatible: Flags.boolean({ default: true, description: 'Use the new api but keep existing functionality and customizations', allowNo: true }) + backwardCompatible: Flags.boolean({ default: false, description: 'Use the new api but keep existing functionality and customizations', allowNo: true }) } static args = [ @@ -19,15 +19,18 @@ export default class Migrate extends Command { ] static examples = [ - '$ zcli themes:migrate --backwardCompatible=false' + '$ zcli themes:migrate ./copenhagen_theme --backwardCompatible' ] static strict = false async run () { - const { flags: { backwardCompatible }, argv: [themeDirectory] } = await this.parse(Migrate) + const { flags, argv: [themeDirectory] } = await this.parse(Migrate) + const { backwardCompatible } = flags const themePath = path.resolve(themeDirectory) - const { api_version: apiVersion } = getManifest(themePath) + const manifest = getManifest(themePath) + + const { api_version: apiVersion } = manifest try { CliUx.ux.action.start('Migrating theme') @@ -38,7 +41,14 @@ export default class Migrate extends Command { break } case 3: { - migrate(themePath) + if (backwardCompatible) { + migrate(themePath) + } else { + this.log(chalk.green('Migrating version 3 without backward compatibility')) + + migrateNoBackwards(themePath, manifest) + } + break } case 4: { diff --git a/packages/zcli-themes/src/lib/migrateNoBackwards.ts b/packages/zcli-themes/src/lib/migrateNoBackwards.ts new file mode 100644 index 00000000..579f38c2 --- /dev/null +++ b/packages/zcli-themes/src/lib/migrateNoBackwards.ts @@ -0,0 +1,52 @@ +import * as path from 'path' +import * as fs from 'fs' +import type { Manifest } from '../types' +import * as chalk from 'chalk' +import { CLIError } from '@oclif/core/lib/errors' + +export default function migrateNoBackwards (themePath: string, manifest: Manifest): void { + const newDir = themePath + '/updated-theme' + + if (!fs.existsSync(newDir)) { + fs.mkdirSync(newDir) + } + + updateOrCreateManifest(newDir, manifest) + updateNewRequestTemplate(themePath, newDir) +} + +function updateOrCreateManifest (themePath: string, manifestContent: Manifest): void { + const updated_manifest = manifestContent + updated_manifest.api_version = 4 + + const manifestFilePath = `${themePath}/manifest.json` + + try { + fs.writeFileSync(manifestFilePath, JSON.stringify(updated_manifest, null, 2), { flag: 'w' }) + } catch (error) { + throw new CLIError(chalk.red(`manifest.json file was malformed at path: "${manifestFilePath}"`)) + } +} + +function updateNewRequestTemplate (themePath: string, updatedThemePath: string): void { + const templateFilePath = `${themePath}/templates/new_request_page.hbs` + const updatedTemplatesPath = `${updatedThemePath}/templates` + const updatedTemplateFilePath = `${updatedTemplatesPath}/new_request_page.hbs` + const additionalModuleFilePath = path.join(__dirname, '..', 'templates', 'request_form_module.hbs') + + try { + if (!fs.existsSync(updatedTemplatesPath)) { + fs.mkdirSync(updatedTemplatesPath) + } + + const templateFile = fs.readFileSync(templateFilePath, 'utf-8') + let updatedFile = templateFile.replace(/\n\s*