(filepath)
| 120 | |
| 121 | // Helper function to backup a file by copying it to the backup folder |
| 122 | function backupFile(filepath) { |
| 123 | var fs = require("fs"), |
| 124 | path = require("path"); |
| 125 | // Backup the file if it exists |
| 126 | if(fs.existsSync(filepath)) { |
| 127 | // Get the timestamp |
| 128 | var timestamp = $tw.utils.stringifyDate(fs.statSync(filepath).mtime || (new Date())), |
| 129 | backupSubPath = backupPathByPath(filepath); |
| 130 | // Compose and uniquify the backup pathname |
| 131 | var count = 0, |
| 132 | backupPath, |
| 133 | uniquifier, |
| 134 | ext = path.extname(filepath); |
| 135 | do { |
| 136 | uniquifier = count ? " " + count : ""; |
| 137 | backupPath = path.resolve( |
| 138 | backupSubPath, |
| 139 | path.basename(filepath,ext) + "." + timestamp + uniquifier + ext |
| 140 | ); |
| 141 | count = count + 1; |
| 142 | } while(fs.existsSync(backupPath)); |
| 143 | // Copy the existing file to the backup |
| 144 | $tw.utils.createDirectory(path.dirname(backupPath)); |
| 145 | fs.writeFileSync(backupPath,fs.readFileSync(filepath)); // For some reason $tw.utils.copyFile() doesn't work here |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Helper to get the backup folder for a given filepath |
| 150 | function backupPathByPath(pathname) { |
no test coverage detected