-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
backup.js
182 lines (147 loc) · 5.84 KB
/
backup.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// The directory to backup your workflows
const BACKUP_PATH="/Users/robb/Developer/personal/alfred-workflows"
//The directory where your Alfred workflows live
const WORKFLOW_PATH="/Users/robb/Documents/AlfredPrefs/Alfred.alfredpreferences/workflows"
//The bundle prefix you use for your workflows
const BUNDLE_PREFIX="com.rknightuk."
// The Github repository
const GITHUB_REPO="rknightuk/alfred-workflows"
const GITHUB_DOWNLOAD="https://github.com/${GITHUB_REPO}/raw/main"
// Copy the example readme into the backup readme file
const README_FILE=`${BACKUP_PATH}/readme.md`
function run(argv) {
// todo run for one workflow
const singleWorkflow = argv
const app = Application.currentApplication();
app.includeStandardAdditions = true;
const se = Application('System Events');
const workflows = se.folders.byName(WORKFLOW_PATH).diskItems.name()
app.doShellScript(`cp ./readme.example ${README_FILE}`);
let mine = []
let others = []
let apiData = []
workflows.forEach(wf => {
let data = null
try {
data = se.propertyListFiles.byName(`${WORKFLOW_PATH}/${wf}/info.plist`).contents.value()
} catch (e) {
console.log(`Cannot run for ${wf}`)
return
}
if (!data) return
let { disabled, webaddress, createdby, version, bundleid, description, name, variables, variablesdontexport, objects } = data
if (disabled) return
if (!version) version = '1.0.0'
const isMine = bundleid.includes(BUNDLE_PREFIX)
bundleid = bundleid.replace(BUNDLE_PREFIX, '')
if (!bundleid) return
if (isMine) {
let currentWorkflow=`${WORKFLOW_PATH}/${wf}`
formattedDescription = description ? `_${description.trim()}_ ` : null
if (variables && variablesdontexport && variablesdontexport.length > 0)
{
currentWorkflow = app.doShellScript(`./overridevariables.sh ${currentWorkflow}`)
}
let hasReadme = app.doShellScript(`[ -f ${currentWorkflow}/readme.md ] && echo "true" || echo "false"`) === 'true'
const hasScreenshot = app.doShellScript(`[ -f ${currentWorkflow}/screenshot.png ] && echo "true" || echo "false"`) === 'true'
const hasChangelog = app.doShellScript(`[ -f ${currentWorkflow}/changelog.md ] && echo "true" || echo "false"`) === 'true'
if (!hasReadme) {
let keyword = null
objects.every(o => {
if (o.config.keyword)
{
keyword = o.config.keyword
return false
}
return true
})
let contents = app.read(Path(`${BACKUP_PATH}/workflow-readme.example`))
contents = contents.replaceAll('{{ NAME }}', name)
.replaceAll('{{ DESC }}', description)
.replaceAll('{{ KEYWORD }}', keyword)
if (name === 'Workflow Development')
{
console.log(contents)
console.log(keyword)
}
app.doShellScript(`touch ${currentWorkflow}/readme.md`)
let newReadmeFile = app.openForAccess(Path(`${currentWorkflow}/readme.md`), { writePermission: true })
app.setEof(newReadmeFile, { to: 0 })
app.write(contents, { to: newReadmeFile, startingAt: app.getEof(newReadmeFile) })
app.closeAccess(newReadmeFile)
console.log(`Added readme for ${name}`)
hasReadme = true
}
const copyPath=`${BACKUP_PATH}/workflows/${bundleid}/${bundleid}.alfredworkflow`
const link = `workflows/${bundleid}`
app.doShellScript(`mkdir -p ${BACKUP_PATH}/workflows/${bundleid}/src/`)
app.doShellScript(`cp -r ${currentWorkflow}/ ${BACKUP_PATH}/workflows/${bundleid}/src/`)
app.doShellScript(`ditto -ck "${currentWorkflow}" "${copyPath}"`)
if (!hasScreenshot) console.log(`⚠️ No screenshot found for ${name}`)
if (hasReadme)
{
app.doShellScript(`cp "${currentWorkflow}/readme.md" "${BACKUP_PATH}/workflows/${bundleid}/readme.md"`)
}
let lines = [
`### ${name}\n`,
`${formattedDescription || ''}[Download v${version}](${link})\n`,
]
const wfData = {
uid: wf.replace('user.workflow.', ''),
bundleid: bundleid,
name: name,
description: description,
version: version,
link: `https://github.com/rknightuk/alfred-workflows/blob/main/${link}`,
screenshot: null,
icon: `https://raw.githubusercontent.com/rknightuk/alfred-workflows/main/workflows/${bundleid}/src/icon.png`
}
if (hasScreenshot)
{
const screenshotPath = `https://raw.githubusercontent.com/rknightuk/alfred-workflows/main/workflows/${bundleid}/src/screenshot.png`
lines.push(`![${bundleid} screenshot](${screenshotPath})\n`)
wfData.screenshot = screenshotPath
}
if (hasChangelog)
{
const changeLogContents = app.read(Path(`${BACKUP_PATH}/workflows/${bundleid}/src/changelog.md`), { usingDelimiter: '\n' })
lines = [...lines, ...changeLogContents, '\n']
}
mine.push({
name: name,
lines: lines,
})
apiData.push(wfData)
} else {
if (createdby) {
others.push({
name,
author: createdby,
link: webaddress,
})
}
}
})
apiData = apiData.sort((a,b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : ((b.name.toLowerCase() > a.name.toLowerCase()) ? -1 : 0))
mine = mine.sort((a,b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : ((b.name.toLowerCase() > a.name.toLowerCase()) ? -1 : 0))
others = others.sort((a,b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : ((b.name.toLowerCase() > a.name.toLowerCase()) ? -1 : 0))
app.doShellScript('touch ./api.json')
apiData = JSON.stringify(apiData, '', 2)
var apiFile = app.openForAccess(Path('./api.json'), { writePermission: true })
app.setEof(apiFile, { to: 0 })
app.write(apiData, { to: apiFile, startingAt: app.getEof(apiFile) })
app.closeAccess(apiFile)
mine.forEach(m => {
m.lines.forEach(l => {
app.doShellScript(`echo "${l}" >> ${README_FILE}`);
})
})
app.doShellScript(`echo "## Third Party Workflows\n" >> ${README_FILE}`);
others.forEach(o => {
let text = `- ${o.name} by ${o.author}`
if (o.link) {
text = `- [${o.name} by ${o.author}](${o.link})`
}
app.doShellScript(`echo "${text}" >> ${README_FILE}`);
})
}