Skip to content

Commit

Permalink
Merge pull request #927 from infinitered/update_patching
Browse files Browse the repository at this point in the history
smart patching
  • Loading branch information
GantMan authored Mar 22, 2017
2 parents 8d0daf1 + 4513973 commit 53001c5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = (plugin, command, context) => {
// insert screen import
patching.insertInFile(
destinationPath,
'import RoundedButton',
'import ExamplesRegistry',
`import ${componentName} from '../Examples/Containers/${pluginName}/${file.screen}'`
)

Expand Down
23 changes: 12 additions & 11 deletions packages/ignite-cli/src/extensions/ignite/patchInFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand All @@ -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 <string> is the same as replace <string> + insert ''
const replaceString = opts.delete || opts.replace
Expand Down
2 changes: 1 addition & 1 deletion packages/ignite-cli/src/extensions/patching.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down

0 comments on commit 53001c5

Please sign in to comment.