-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup to Working Copy.js
68 lines (62 loc) · 1.93 KB
/
Backup to Working Copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: code-branch;
let fm = FileManager.iCloud()
let dir = fm.documentsDirectory()
// Fetch all current scripts
let filePaths = allScripts(dir)
// Pick the folder that contains your repository
let dirs = await DocumentPicker.open(["public.folder"])
let dstDir = dirs[0]
let repoName = getFilename(dstDir)
// Remove all scripts in the repository. We'll replace the mwith the current ones.
removeScripts(dstDir)
// Copy all current scripts into the repository.
for (filePath of filePaths) {
copyFile(filePath, dstDir)
}
// Find your key in the Working Copy settings
let key = "JFNLRTTYE"
await pushChanges(repoName, key)
async function pushChanges(repo, key) {
let baseURL = "working-copy://x-callback-url/chain/"
let msg = "Backup from Scriptable"
let cbu = new CallbackURL(baseURL)
cbu.addParameter("key", key)
cbu.addParameter("repo", repo)
cbu.addParameter("command", "commit")
cbu.addParameter("message", msg)
cbu.addParameter("limit", "999")
cbu.addParameter("command", "push")
await cbu.open()
}
function copyFile(srcFilePath, dstDir) {
let fm = FileManager.iCloud()
let filename = getFilename(filePath)
let dstFilePath = fm.joinPath(dstDir, filename)
fm.copy(srcFilePath, dstFilePath)
}
function removeScripts(dstDir) {
let fm = FileManager.iCloud()
let filePaths = allScripts(dstDir)
for (filePath of filePaths) {
fm.remove(filePath)
}
}
function allScripts(dir) {
let fm = FileManager.iCloud()
let files = fm.listContents(dir)
let filePaths = files.map(file => {
return fm.joinPath(dir, file)
})
return filePaths.filter(isScript)
}
function isScript(filePath) {
let fm = FileManager.iCloud()
let uti = fm.getUTI(filePath)
return uti == "com.netscape.javascript-source"
}
function getFilename(filePath) {
let idx = filePath.lastIndexOf("/")
return filePath.substring(idx + 1)
}