From f0107283f856441e8fae0111d975c3fdd671e6e1 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 21 May 2020 19:31:14 +0200 Subject: [PATCH] packages/cli: move reading of plugin template data to plugin diff --- packages/cli/src/commands/plugin/diff.ts | 39 +++++++++++++++++++++++- packages/cli/src/lib/diff/read.ts | 38 ++--------------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/packages/cli/src/commands/plugin/diff.ts b/packages/cli/src/commands/plugin/diff.ts index ba3e99527f..66db692ab4 100644 --- a/packages/cli/src/commands/plugin/diff.ts +++ b/packages/cli/src/commands/plugin/diff.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import fs from 'fs-extra'; import { Command } from 'commander'; import { diffTemplateFiles, @@ -23,6 +24,13 @@ import { makeCheckPromptFunc, yesPromptFunc, } from '../../lib/diff'; +import { paths } from '../../lib/paths'; +import { version } from '../../lib/version'; + +export type PluginData = { + id: string; + name: string; +}; const fileHandlers = [ { @@ -54,7 +62,36 @@ export default async (cmd: Command) => { promptFunc = yesPromptFunc; } - const templateFiles = await diffTemplateFiles('default-plugin'); + const data = await readPluginData(); + const templateFiles = await diffTemplateFiles('default-plugin', { + version, + ...data, + }); await handleAllFiles(fileHandlers, templateFiles, promptFunc); await finalize(); }; + +// Reads templating data from the existing plugin +async function readPluginData(): Promise { + let name: string; + try { + const pkg = require(paths.resolveTarget('package.json')); + name = pkg.name; + } catch (error) { + throw new Error(`Failed to read target package, ${error}`); + } + + const pluginTsContents = await fs.readFile( + paths.resolveTarget('src/plugin.ts'), + 'utf8', + ); + // TODO: replace with some proper parsing logic or plugin metadata file + const pluginIdMatch = pluginTsContents.match(/id: ['"`](.+?)['"`]/); + if (!pluginIdMatch) { + throw new Error(`Failed to parse plugin.ts, no plugin ID found`); + } + + const id = pluginIdMatch[1]; + + return { id, name }; +} diff --git a/packages/cli/src/lib/diff/read.ts b/packages/cli/src/lib/diff/read.ts index a0bb6364ca..75e1b86d24 100644 --- a/packages/cli/src/lib/diff/read.ts +++ b/packages/cli/src/lib/diff/read.ts @@ -23,44 +23,13 @@ import { import handlebars from 'handlebars'; import recursiveReadDir from 'recursive-readdir'; import { paths } from '../paths'; -import { version } from '../version'; import { FileDiff } from './types'; -export type PluginInfo = { - id: string; - name: string; -}; - export type TemplatedFile = { path: string; contents: string; }; -// Reads info from the existing plugin -async function readPluginInfo(): Promise { - let name: string; - try { - const pkg = require(paths.resolveTarget('package.json')); - name = pkg.name; - } catch (error) { - throw new Error(`Failed to read target package, ${error}`); - } - - const pluginTsContents = await fs.readFile( - paths.resolveTarget('src/plugin.ts'), - 'utf8', - ); - // TODO: replace with some proper parsing logic or plugin metadata file - const pluginIdMatch = pluginTsContents.match(/id: ['"`](.+?)['"`]/); - if (!pluginIdMatch) { - throw new Error(`Failed to parse plugin.ts, no plugin ID found`); - } - - const id = pluginIdMatch[1]; - - return { id, name }; -} - async function readTemplateFile( templateFile: string, templateVars: any, @@ -131,13 +100,10 @@ async function diffTemplatedFiles( } // Read all template files for a given template, along with all matching files in the target dir -export async function diffTemplateFiles(template: string) { - const pluginInfo = await readPluginInfo(); - const templateVars = { version, ...pluginInfo }; - +export async function diffTemplateFiles(template: string, templateData: any) { const templateDir = paths.resolveOwn('templates', template); - const templatedFiles = await readTemplate(templateDir, templateVars); + const templatedFiles = await readTemplate(templateDir, templateData); const fileDiffs = await diffTemplatedFiles(paths.targetDir, templatedFiles); return fileDiffs; }