MCPcopy
hub / github.com/TiddlyWiki/TiddlyDesktop / backupFile

Function backupFile

source/js/utils/saving.js:122–147  ·  view source on GitHub ↗
(filepath)

Source from the content-addressed store, hash-verified

120
121// Helper function to backup a file by copying it to the backup folder
122function 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
150function backupPathByPath(pathname) {

Callers 1

saving.jsFile · 0.85

Calls 1

backupPathByPathFunction · 0.85

Tested by

no test coverage detected