-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
210 lines (196 loc) Β· 5.75 KB
/
cli.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const argv = require('yargs')
.option('theme', {
alias: 't',
describe: 'See available at: https://highlightjs.org/static/demo/'
})
.option('eject', {
alias: 'e',
describe: 'Revert Slack.app to normal'
})
.help()
.argv
const availableThemes = require('./fixtures/hljs-themes')
const path = require('path')
const fsTemp = require('fs-temp/promise')
const asar = require('asar')
const pify = require('pify')
const fsp = require('fs-promise')
const execa = require('execa')
const Listr = require('listr')
const pathExists = require('path-exists')
const readFileContents = (path) => fsp.readFile(path).then(contents => contents.toString())
// namespace is used to check that we don't add the script twice
const namespace = '/* INJECTED */'
const slackAppPath = path.join('/Applications', 'Slack.app')
const slackResourcesPath = path.join(slackAppPath, 'Contents', 'Resources')
const asarFileName = 'app.asar'
const asarPath = path.join(slackResourcesPath, asarFileName)
const unmodifiedAsarFileName = 'app.unmodified.asar'
const unmodifiedAsarPath = path.join(slackResourcesPath, unmodifiedAsarFileName)
const unpackedPath = path.join(slackResourcesPath, '_app')
const entryJsPath = path.join(unpackedPath, 'src', 'static', 'index.js')
// Run this in execa since asar.extractAll is sync
const extractAsar = (from, to) => execa('npm', ['run', 'asar', 'extract', from, to])
// This is needed because paths in the asar is somehow relative to the filename -.-
const repackAsar = (from, to) => new Listr([
{
title: 'Create temp directory',
task: (ctx, task) => fsTemp.mkdir().then(tmpPath => { ctx.tmpPath = tmpPath })
},
{
title: 'Extract asar to temp directory',
task: (ctx, task) => extractAsar(from, ctx.tmpPath)
},
{
title: 'Repack asar to destination',
task: (ctx, task) => pify(asar.createPackage)(ctx.tmpPath, to)
},
{
title: 'Remove temp directory',
task: (ctx) => fsp.remove(ctx.tmpPath)
}
])
const tasks = new Listr([
{
title: 'Locating Slack.app',
task: (ctx, task) => pathExists(slackAppPath)
.then((exists) => {
if (!exists) throw new Error(`Couldn't find Slack.app (${slackAppPath})`)
task.title = `Locating Slack.app (${slackAppPath})`
})
},
{
title: 'Find app.asar in Slack.app',
task: (ctx, task) => pathExists(asarPath)
.then((exists) => {
if (!exists) throw new Error(`Couldn't find app.asar (${asarPath})`)
task.title = `Find app.asar in Slack.app (${asarPath})`
})
},
{
title: `Backup unmodified app.asar`,
task: (ctx, task) => pathExists(unmodifiedAsarPath)
.then(exists => {
if (exists) {
task.skip('Backup already exists, use --eject to restore backup')
return
}
return repackAsar(asarPath, unmodifiedAsarPath)
})
.then(path => path)
},
{
title: `Extract ${unmodifiedAsarFileName}`,
task: () => extractAsar(unmodifiedAsarPath, unpackedPath)
},
{
title: 'Load index.js',
task: (ctx) => readFileContents(entryJsPath)
.then(source => {
ctx.source = source.includes(namespace) ? source.split(namespace)[0] : source
})
},
{
title: 'Load inject.js',
task: (ctx) => readFileContents(path.join(__dirname, 'inject.js'))
.then(injectSource => {
ctx.injectSource = injectSource
})
},
{
title: 'Load config.json',
task: (ctx, task) => readFileContents(path.join(__dirname, 'config.json'))
.then(raw => {
ctx.rawConfig = raw
})
.catch(() => {
ctx.rawConfig = false
task.skip('No config.json found.')
})
},
{
title: 'Load default-config.json',
enabled: ctx => !ctx.rawConfig,
task: (ctx, task) => readFileContents(path.join(__dirname, 'fixtures', 'default-config.json'))
.then(raw => {
ctx.rawConfig = raw
})
},
{
title: 'Verify config',
task: (ctx) => {
try {
ctx.config = JSON.parse(ctx.rawConfig)
} catch (_) {
throw new Error('Error parsing config')
}
return true
}
},
{
title: `Verify theme`,
task: (ctx, task) => {
const theme = argv.theme || ctx.config.theme
if (!availableThemes.includes(theme)) throw new Error(`Theme "${theme}" does not exist`)
task.title = `Verify theme (${theme})`
ctx.overrideConfig = { theme }
}
},
{
title: 'Merge script',
task: (ctx) => {
const newSource = [
ctx.source.replace(/\n+$/g, ''),
'\n',
namespace,
`const config = JSON.parse(\`${JSON.stringify(Object.assign({}, ctx.config, ctx.overrideConfig))}\`)`,
ctx.injectSource
].join('\n')
return fsp.writeFile(entryJsPath, newSource)
}
},
{
title: 'Repack app.asar',
task: () => pify(asar.createPackage)(unpackedPath, asarPath)
},
{
title: 'Cleanup',
task: () => fsp.remove(unpackedPath)
}
])
if (argv.eject) {
const tasks = new Listr([
{
title: 'Check if backup exists',
task: (ctx, task) => pathExists(unmodifiedAsarPath)
.then((exists) => {
if (!exists) throw new Error(`Can't find unmodified asar (${unmodifiedAsarPath})`)
})
},
{
title: 'Repack app.unmodified.asar to app.asar',
task: () => repackAsar(unmodifiedAsarPath, asarPath)
}
])
tasks
.run()
.then(() => {
console.log()
console.log('π₯ Done ejecting, please restart Slack.app')
console.log()
})
.catch(err => {
console.error('π₯ Error running tasks', err)
})
} else {
tasks
.run()
.then(() => {
console.log()
console.log('π΄ Done, restart Slack.app to see changes π
')
console.log()
})
.catch(err => {
console.error('π₯ Error running tasks', err)
})
}