From f33c594fae46b4563271ce50cc2e8ea55259fd3b Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Wed, 22 Mar 2017 11:51:53 -0500 Subject: [PATCH 1/2] make patching smart again --- .../ignite/addPluginScreenExamples.js | 3 ++- .../src/extensions/ignite/patchInFile.js | 23 ++++++++++--------- .../ignite-cli/src/extensions/patching.js | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js b/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js index ca2716d02..85f4145b5 100644 --- a/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js +++ b/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js @@ -65,10 +65,11 @@ module.exports = (plugin, command, context) => { const componentName = replace(/.js|\s|-/g, '', exampleFileName) if (filesystem.exists(destinationPath)) { + // insert screen import patching.insertInFile( destinationPath, - 'import RoundedButton', + 'import ExamplesRegistry', `import ${componentName} from '../Examples/Containers/${pluginName}/${file.screen}'` ) diff --git a/packages/ignite-cli/src/extensions/ignite/patchInFile.js b/packages/ignite-cli/src/extensions/ignite/patchInFile.js index 2ba392c83..255098ada 100644 --- a/packages/ignite-cli/src/extensions/ignite/patchInFile.js +++ b/packages/ignite-cli/src/extensions/ignite/patchInFile.js @@ -2,17 +2,17 @@ const jetpack = require('fs-jetpack') module.exports = (plugin, command, context) => { /** - * Conditionally inserts a string into a file before or after another string. + * Conditionally places a string into a file before or after another string. * TODO: Move to infinitered/gluegun eventually? Plugin or core? * - * @param {string} file File to be patched - * @param {Object} opts Options - * @param {string} opts.before Insert before this string - * @param {string} opts.after Insert after this string - * @param {string} opts.replace Replace this string - * @param {string} opts.insert String to be inserted - * @param {string} opts.delete Delete this string - * @param {string} opts.match Skip if this string exists already + * @param {string} file File to be patched + * @param {Object} opts Options + * @param {string} opts.before Insert before this string + * @param {string} opts.after Insert after this string + * @param {string} opts.replace Replace this string + * @param {string} opts.insert String to be inserted + * @param {string} opts.delete Delete this string + * @param {boolean} opts.force Write even if it already exists * * @example * patchInFile('thing.js', { before: 'bar', insert: 'foo' }) @@ -23,8 +23,9 @@ module.exports = (plugin, command, context) => { const data = jetpack.read(file, 'utf8') - // If the file already includes opts.match, no-op - if (data.includes(opts.match)) return + // If the file already has insert, no-op unless forced + // stops accidental double inserts unless you're sure you want that + if (data.includes(opts.insert) && !opts.force) return // delete is the same as replace + insert '' const replaceString = opts.delete || opts.replace diff --git a/packages/ignite-cli/src/extensions/patching.js b/packages/ignite-cli/src/extensions/patching.js index 5a3397e12..d39ffeebf 100644 --- a/packages/ignite-cli/src/extensions/patching.js +++ b/packages/ignite-cli/src/extensions/patching.js @@ -28,7 +28,7 @@ function attach (plugin, command, context) { const matches = data.match(finder) // Quick error check - if (matches === null) throw new Error(`'${findPattern}' was not found in file.`) + if (matches === null) throw new Error(`'${findPattern}' was not found in file ${filePath}.`) if (insertAfter) { newContents = data.replace(finder, `${matches[0]}\n${content}`) From 451397308385d9835e28390a4a95d34ffe107b66 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Wed, 22 Mar 2017 12:11:51 -0500 Subject: [PATCH 2/2] fix lint --- .../ignite-cli/src/extensions/ignite/addPluginScreenExamples.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js b/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js index 85f4145b5..73be7afee 100644 --- a/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js +++ b/packages/ignite-cli/src/extensions/ignite/addPluginScreenExamples.js @@ -65,7 +65,6 @@ module.exports = (plugin, command, context) => { const componentName = replace(/.js|\s|-/g, '', exampleFileName) if (filesystem.exists(destinationPath)) { - // insert screen import patching.insertInFile( destinationPath,