diff --git a/package.json b/package.json index b644e22..8ddc04e 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "migrate" ], "dependencies": { + "cheerio": "^1.0.0", "commander": "^11.0.0", "ts-morph": "^19.0.0", "winston": "^3.10.0" diff --git a/src/migrator/config.ts b/src/migrator/config.ts index 054a561..89ad5fe 100644 --- a/src/migrator/config.ts +++ b/src/migrator/config.ts @@ -19,7 +19,13 @@ export const vueSpecialMethods = [ 'destroyed', ]; // Vue methods that won't be included under methods: {...}, they go to the root. +export const customsDecorators = [ + 'Apollo', + 'State2Way', +]; + export const supportedDecorators = [ ...vuexDecorators, ...vueClassPropertyDecorators, + ...customsDecorators, ]; // Class Property decorators diff --git a/src/migrator/migrator-to-sfc.ts b/src/migrator/migrator-to-sfc.ts index e9d81c2..7d69563 100644 --- a/src/migrator/migrator-to-sfc.ts +++ b/src/migrator/migrator-to-sfc.ts @@ -1,6 +1,7 @@ import path from 'path'; import { Project, SourceFile } from 'ts-morph'; import logger from './logger'; +import migrateVueTemplate from './vue-template'; export const getScriptContent = (vueSourceFile: SourceFile): string | undefined => { const scriptTagRegex = /]*>([\s\S]*?)<\/script>/; @@ -8,7 +9,7 @@ export const getScriptContent = (vueSourceFile: SourceFile): string | undefined return match ? match[1] : undefined; }; -export const injectScript = (tsSourceFile: SourceFile, vueTemplate: string): string => { +const injectScript = (tsSourceFile: SourceFile, vueTemplate: string): string => { const scriptTag = vueTemplate.match(/|([\s\S]*)<\/script>/); if (!scriptTag) { @@ -44,6 +45,31 @@ const injectScss = ( ); }; +const injectTemplate = (tsSourceFile: SourceFile, vueTemplate: string): string => { + const templateTag = vueTemplate.match(/|([\s\S]*)<\/template>/); + + if (!templateTag) { + throw new Error('Template tag not found on vue file.'); + } + + const outTemplate = migrateVueTemplate(templateTag[0]); + + return vueTemplate.replace( + templateTag[0], + ``, + ); +}; + +export const injectComponent = ( + tsSourceFile: SourceFile, + vueTemplate: string, +) => { + let componentTextFile = injectScript(tsSourceFile, vueTemplate); + componentTextFile = injectTemplate(tsSourceFile, componentTextFile); + + return componentTextFile; +}; + export const getScriptSrc = (vueSourceFile: SourceFile): string | undefined => { // Regex that extracts the src from the