|
| 1 | +var Promise = require('ember-cli/lib/ext/promise'); |
| 2 | +var fs = require('ember-cli/node_modules/fs-extra'); |
| 3 | +var path = require('path'); |
| 4 | +var writeFile = Promise.denodeify(fs.outputFile); |
| 5 | + |
| 6 | +var replacePhrases = function(content, phrases){ |
| 7 | + var contentsToWrite = content; |
| 8 | + |
| 9 | + return Object.keys(phrases).reduce(function(prev, next){ |
| 10 | + var contentMarkerIndex = contentsToWrite.indexOf(next); |
| 11 | + if (contentMarkerIndex !== -1) { |
| 12 | + var replaceIndex = contentMarkerIndex; |
| 13 | + |
| 14 | + contentsToWrite = contentsToWrite.slice(0, replaceIndex) + |
| 15 | + phrases[next] + |
| 16 | + contentsToWrite.slice(replaceIndex + next.length); |
| 17 | + return contentsToWrite; |
| 18 | + } |
| 19 | + }, ""); |
| 20 | +}; |
| 21 | + |
| 22 | +var insertPhrases = function(contents, phrases){ |
| 23 | + var contentsToWrite = contents; |
| 24 | + |
| 25 | + return Object.keys(phrases).reduce(function(prev, next){ |
| 26 | + var contentMarkerIndex = contentsToWrite.indexOf(next); |
| 27 | + if (contentMarkerIndex !== -1) { |
| 28 | + var insertIndex; |
| 29 | + if(phrases[next].after){ |
| 30 | + insertIndex = contentMarkerIndex + next.length; |
| 31 | + } |
| 32 | + |
| 33 | + if(phrases[next].before){ |
| 34 | + insertIndex = contentMarkerIndex; |
| 35 | + } |
| 36 | + |
| 37 | + contentsToWrite = contentsToWrite.slice(0, insertIndex) + |
| 38 | + phrases[next].content + |
| 39 | + contentsToWrite.slice(insertIndex); |
| 40 | + return contentsToWrite; |
| 41 | + } |
| 42 | + }, ""); |
| 43 | +}; |
| 44 | +module.exports = { |
| 45 | + processTextContent: function(pathRelativeToProjectRoot, commands) { |
| 46 | + var fullPath = path.join(this.project.root, pathRelativeToProjectRoot); |
| 47 | + var originalContents = ''; |
| 48 | + |
| 49 | + if (fs.existsSync(fullPath)) { |
| 50 | + originalContents = fs.readFileSync(fullPath, { |
| 51 | + encoding: 'utf8' |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + var contentsToWrite = originalContents; |
| 56 | + if (commands.replace) { |
| 57 | + contentsToWrite = replacePhrases(contentsToWrite, commands.replace); |
| 58 | + } |
| 59 | + if (commands.insert) { |
| 60 | + contentsToWrite = insertPhrases(contentsToWrite, commands.insert); |
| 61 | + } |
| 62 | + |
| 63 | + var returnValue = { |
| 64 | + path: fullPath, |
| 65 | + originalContents: originalContents, |
| 66 | + contents: contentsToWrite, |
| 67 | + inserted: false |
| 68 | + }; |
| 69 | + |
| 70 | + if (contentsToWrite !== originalContents) { |
| 71 | + returnValue.inserted = true; |
| 72 | + |
| 73 | + return writeFile(fullPath, contentsToWrite) |
| 74 | + .then(function() { |
| 75 | + return returnValue; |
| 76 | + |
| 77 | + }); |
| 78 | + } else { |
| 79 | + return Promise.resolve(returnValue); |
| 80 | + } |
| 81 | + |
| 82 | + }, |
| 83 | + renameFile: function(pathOld, pathNew){ |
| 84 | + var fullPathOld = path.join(this.project.root, pathOld); |
| 85 | + var fullPathNew = path.join(this.project.root, pathNew); |
| 86 | + fs.rename(fullPathOld, fullPathNew, function(error){ |
| 87 | + if(error) { |
| 88 | + return; |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | +}; |
0 commit comments