/
atom-xterm-profiles.js
385 lines (354 loc) · 16 KB
/
atom-xterm-profiles.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/** @babel */
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2017-2018 Andres Mejia <amejia004@gmail.com>. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { Emitter } from 'atom'
import * as config from './atom-xterm-config'
import fs from 'fs-extra'
import path from 'path'
import uuidv4 from 'uuid/v4'
import { URL } from 'whatwg-url'
import { detailedDiff } from 'deep-object-diff'
const ATOM_XTERM_BASE_URI = 'atom-xterm://'
const CONFIG_KEY_TO_PROFILE_KEY_MAPPING = {
'atom-xterm.spawnPtySettings.command': 'command',
'atom-xterm.spawnPtySettings.args': 'args',
'atom-xterm.spawnPtySettings.name': 'name',
'atom-xterm.spawnPtySettings.cwd': 'cwd',
'atom-xterm.spawnPtySettings.env': 'env',
'atom-xterm.spawnPtySettings.setEnv': 'setEnv',
'atom-xterm.spawnPtySettings.deleteEnv': 'deleteEnv',
'atom-xterm.spawnPtySettings.encoding': 'encoding',
'atom-xterm.terminalSettings.fontSize': 'fontSize',
'atom-xterm.terminalSettings.leaveOpenAfterExit': 'leaveOpenAfterExit',
'atom-xterm.terminalSettings.allowRelaunchingTerminalsOnStartup': 'allowRelaunchingTerminalsOnStartup',
'atom-xterm.terminalSettings.relaunchTerminalOnStartup': 'relaunchTerminalOnStartup',
'atom-xterm.terminalSettings.title': 'title',
'atom-xterm.terminalSettings.xtermOptions': 'xtermOptions'
}
let symbol = Symbol('AtomXtermProfilesSingleton sentinel')
class AtomXtermProfilesSingleton {
constructor (symbolCheck) {
if (symbol !== symbolCheck) {
throw new Error('AtomXtermProfilesSingleton cannot be instantiated directly.')
}
this.emitter = new Emitter()
this.profilesConfigPath = path.join(config.getUserDataPath(), 'profiles.json')
this.profiles = {}
this.previousBaseProfile = null
this.baseProfile = this.getDefaultProfile()
this.resetBaseProfile()
this.profilesLoadPromise = null
this.reloadProfiles()
}
static get instance () {
if (!this[symbol]) {
this[symbol] = new AtomXtermProfilesSingleton(symbol)
}
return this[symbol]
}
sortProfiles (profiles) {
let orderedProfiles = {}
Object.keys(profiles).sort().forEach((key) => {
orderedProfiles[key] = profiles[key]
})
return orderedProfiles
}
reloadProfiles () {
this.profilesLoadPromise = new Promise((resolve, reject) => {
fs.readJson(this.profilesConfigPath, (err, data) => {
if (err) {
// Create the profiles file.
this.updateProfiles({}).then(() => {
this.emitter.emit('did-reload-profiles', this.getSanitizedProfilesData())
resolve()
})
} else {
this.profiles = this.sortProfiles(data)
this.emitter.emit('did-reload-profiles', this.getSanitizedProfilesData())
resolve()
}
})
})
}
onDidReloadProfiles (callback) {
return this.emitter.on('did-reload-profiles', callback)
}
onDidResetBaseProfile (callback) {
return this.emitter.on('did-reset-base-profile', callback)
}
updateProfiles (newProfilesConfigData) {
return new Promise((resolve, reject) => {
fs.ensureDir(path.dirname(this.profilesConfigPath), (err) => {
if (err) reject(err)
newProfilesConfigData = this.sortProfiles(newProfilesConfigData)
fs.writeJson(this.profilesConfigPath, newProfilesConfigData, (err) => {
if (err) reject(err)
this.profiles = newProfilesConfigData
resolve()
})
})
})
}
deepClone (data) {
return JSON.parse(JSON.stringify(data))
}
diffProfiles (oldProfile, newProfile) {
// This method will return added or modified entries.
let diff = detailedDiff(oldProfile, newProfile)
return Object.assign(diff.added, diff.updated)
}
getDefaultProfile () {
return {
command: config.getDefaultShellCommand(),
args: JSON.parse(config.getDefaultArgs()),
name: config.getDefaultTermType(),
cwd: config.getDefaultCwd(),
env: null,
setEnv: JSON.parse(config.getDefaultSetEnv()),
deleteEnv: JSON.parse(config.getDefaultDeleteEnv()),
encoding: null,
fontSize: config.getDefaultFontSize(),
leaveOpenAfterExit: config.getDefaultLeaveOpenAfterExit(),
relaunchTerminalOnStartup: config.getDefaultRelaunchTerminalOnStartup(),
title: null,
xtermOptions: JSON.parse(config.getDefaultXtermOptions())
}
}
getBaseProfile () {
return this.deepClone(this.baseProfile)
}
validateJsonConfigSetting (name, defaultJsonValue) {
let profileKey = CONFIG_KEY_TO_PROFILE_KEY_MAPPING[name]
let previousValue = this.previousBaseProfile[profileKey]
let value = atom.config.get(name)
try {
value = JSON.parse(value || defaultJsonValue) || previousValue
} catch (e) {
// This normally happens when the user is in the middle of updating some
// setting that is a JSON string. Ignore syntax errors and use the last
// known good config setting.
if (!(e instanceof SyntaxError)) {
throw e
}
value = previousValue
}
return value
}
resetBaseProfile () {
this.previousBaseProfile = this.deepClone(this.baseProfile)
let env = this.validateJsonConfigSetting('atom-xterm.spawnPtySettings.env', 'null')
if (!env || env.constructor !== Object) {
env = null
}
let encoding = atom.config.get('atom-xterm.spawnPtySettings.encoding') || null
let leaveOpenAfterExit = atom.config.get('atom-xterm.terminalSettings.leaveOpenAfterExit')
if (leaveOpenAfterExit !== true && leaveOpenAfterExit !== false) leaveOpenAfterExit = config.getDefaultLeaveOpenAfterExit()
let relaunchTerminalOnStartup = atom.config.get('atom-xterm.terminalSettings.relaunchTerminalOnStartup')
if (relaunchTerminalOnStartup !== true && relaunchTerminalOnStartup !== false) relaunchTerminalOnStartup = config.getDefaultRelaunchTerminalOnStartup()
let title = atom.config.get('atom-xterm.terminalSettings.title') || config.getDefaultTitle()
this.baseProfile = {
command: atom.config.get('atom-xterm.spawnPtySettings.command') || config.getDefaultShellCommand(),
args: this.validateJsonConfigSetting('atom-xterm.spawnPtySettings.args', config.getDefaultArgs()),
name: atom.config.get('atom-xterm.spawnPtySettings.name') || config.getDefaultTermType(),
cwd: atom.config.get('atom-xterm.spawnPtySettings.cwd') || config.getDefaultCwd(),
env: env,
setEnv: this.validateJsonConfigSetting('atom-xterm.spawnPtySettings.setEnv', config.getDefaultSetEnv()),
deleteEnv: this.validateJsonConfigSetting('atom-xterm.spawnPtySettings.deleteEnv', config.getDefaultDeleteEnv()),
encoding: encoding,
fontSize: atom.config.get('atom-xterm.terminalSettings.fontSize') || config.getDefaultFontSize(),
leaveOpenAfterExit: leaveOpenAfterExit,
relaunchTerminalOnStartup: relaunchTerminalOnStartup,
title: title || null,
xtermOptions: this.validateJsonConfigSetting('atom-xterm.terminalSettings.xtermOptions', config.getDefaultXtermOptions())
}
this.emitter.emit('did-reset-base-profile', this.getBaseProfile())
}
sanitizeData (data) {
let sanitizedData = {}
if ('command' in data) sanitizedData.command = data.command
if ('args' in data) sanitizedData.args = data.args
if ('name' in data) sanitizedData.name = data.name
if ('cwd' in data) sanitizedData.cwd = data.cwd
if ('env' in data) sanitizedData.env = data.env
if ('setEnv' in data) sanitizedData.setEnv = data.setEnv
if ('deleteEnv' in data) sanitizedData.deleteEnv = data.deleteEnv
if ('encoding' in data) sanitizedData.encoding = data.encoding
if ('fontSize' in data) sanitizedData.fontSize = data.fontSize
if ('leaveOpenAfterExit' in data) sanitizedData.leaveOpenAfterExit = data.leaveOpenAfterExit
if ('relaunchTerminalOnStartup' in data) sanitizedData.relaunchTerminalOnStartup = data.relaunchTerminalOnStartup
if ('title' in data) sanitizedData.title = data.title
if ('xtermOptions' in data) sanitizedData.xtermOptions = data.xtermOptions
return this.deepClone(sanitizedData)
}
getSanitizedProfilesData () {
let retval = {}
for (let key in this.profiles) {
retval[key] = this.sanitizeData(this.profiles[key])
}
return retval
}
getProfiles () {
return new Promise((resolve, reject) => {
this.profilesLoadPromise.then(() => {
resolve(this.getSanitizedProfilesData())
})
})
}
getProfile (profileName) {
return new Promise((resolve, reject) => {
this.profilesLoadPromise.then(() => {
resolve(Object.assign(
{},
this.deepClone(this.baseProfile),
this.sanitizeData(this.profiles[profileName] || {})
))
})
})
}
isProfileExists (profileName) {
return new Promise((resolve, reject) => {
this.profilesLoadPromise.then(() => {
resolve(profileName in this.profiles)
})
})
}
setProfile (profileName, data) {
return new Promise((resolve, reject) => {
this.profilesLoadPromise.then(() => {
let profileData = Object.assign(
{},
this.deepClone(this.baseProfile),
this.sanitizeData(data)
)
let newProfilesConfigData = Object.assign(
{},
this.deepClone(this.profiles)
)
newProfilesConfigData[profileName] = profileData
this.updateProfiles(newProfilesConfigData).then(() => {
resolve()
})
})
})
}
deleteProfile (profileName) {
return new Promise((resolve, reject) => {
this.profilesLoadPromise.then(() => {
let newProfilesConfigData = Object.assign(
{},
this.deepClone(this.profiles)
)
delete newProfilesConfigData[profileName]
this.updateProfiles(newProfilesConfigData).then(() => {
resolve()
})
})
})
}
generateNewUri () {
return ATOM_XTERM_BASE_URI + uuidv4() + '/'
}
generateNewUrlFromProfileData (data) {
data = this.sanitizeData(data)
let url = new URL(this.generateNewUri())
// Command to run, can be basename of command or full path to command.
if ('command' in data) url.searchParams.set('command', data.command)
// Arguments to pass to command. This should be in a JSON array.
if ('args' in data) url.searchParams.set('args', JSON.stringify(data.args))
// This defines the term type to use ('xterm', 'xterm-color', etc.). This
// option does nothing on Windows.
if ('name' in data) url.searchParams.set('name', data.name)
// Current working directory to start command in.
if ('cwd' in data) url.searchParams.set('cwd', data.cwd)
// Environment to use for command. This can be null or left out in which
// case the current environment is used.
if ('env' in data) url.searchParams.set('env', JSON.stringify(data.env))
// Environment variables to set or override from the defined environment
// above or the current environment when starting command.
if ('setEnv' in data) url.searchParams.set('setEnv', JSON.stringify(data.setEnv))
// Environment variables to delete when starting command.
if ('deleteEnv' in data) url.searchParams.set('deleteEnv', JSON.stringify(data.deleteEnv))
// Encoding to use when running command.
if ('encoding' in data) url.searchParams.set('encoding', data.encoding)
// Font size to use.
if ('fontSize' in data) url.searchParams.set('fontSize', JSON.stringify(data.fontSize))
// This determines whether to leave the terminal tab open when the command
// has finished running.
if ('leaveOpenAfterExit' in data) url.searchParams.set('leaveOpenAfterExit', JSON.stringify(data.leaveOpenAfterExit))
// This determines whether the terminal tab should be restarted when Atom
// is restarted.
if ('relaunchTerminalOnStartup' in data) url.searchParams.set('relaunchTerminalOnStartup', JSON.stringify(data.relaunchTerminalOnStartup))
// This is used to set a custom title for the new terminal tab.
if ('title' in data) url.searchParams.set('title', data.title)
// The options supported by the Terminal object in xterm.js. See also
// https://github.com/xtermjs/xterm.js/blob/3.0.2/typings/xterm.d.ts#L14 .
// NOTE: The 'fontSize' setting defined in the options here is ignored.
if ('xtermOptions' in data) url.searchParams.set('xtermOptions', JSON.stringify(data.xtermOptions))
return url
}
createProfileDataFromUri (uri) {
let param
let url = new URL(uri)
let baseProfile = this.getBaseProfile()
let newProfile = {}
param = url.searchParams.get('command')
if (param) newProfile.command = param
if (!('command' in newProfile)) newProfile.command = baseProfile.command
param = url.searchParams.get('args')
if (param) newProfile.args = JSON.parse(param)
if (!('args' in newProfile && newProfile.args)) newProfile.args = baseProfile.args
param = url.searchParams.get('name')
if (param) newProfile.name = param
if (!('name' in newProfile)) newProfile.name = baseProfile.name
param = url.searchParams.get('cwd')
if (param) newProfile.cwd = param
if (!('cwd' in newProfile)) newProfile.cwd = baseProfile.cwd
param = url.searchParams.get('env')
if (param) newProfile.env = JSON.parse(param)
if (!('env' in newProfile && newProfile.env)) newProfile.env = baseProfile.env
param = url.searchParams.get('setEnv')
if (param) newProfile.setEnv = JSON.parse(param)
if (!('setEnv' in newProfile && newProfile.setEnv)) newProfile.setEnv = baseProfile.setEnv
param = url.searchParams.get('deleteEnv')
if (param) newProfile.deleteEnv = JSON.parse(param)
if (!('deleteEnv' in newProfile && newProfile.deleteEnv)) newProfile.deleteEnv = baseProfile.deleteEnv
param = url.searchParams.get('encoding')
if (param && param !== 'null') newProfile.encoding = param
if (!('encoding' in newProfile)) newProfile.encoding = baseProfile.encoding
param = url.searchParams.get('fontSize')
if (param) newProfile.fontSize = JSON.parse(param)
if (!('fontSize' in newProfile && newProfile.fontSize)) newProfile.fontSize = baseProfile.fontSize
param = url.searchParams.get('leaveOpenAfterExit')
if (param) newProfile.leaveOpenAfterExit = JSON.parse(param)
if (!('leaveOpenAfterExit' in newProfile && newProfile.leaveOpenAfterExit !== null && newProfile.leaveOpenAfterExit !== '')) newProfile.leaveOpenAfterExit = baseProfile.leaveOpenAfterExit
param = url.searchParams.get('relaunchTerminalOnStartup')
if (param) newProfile.relaunchTerminalOnStartup = JSON.parse(param)
if (!('relaunchTerminalOnStartup' in newProfile && newProfile.relaunchTerminalOnStartup !== null && newProfile.relaunchTerminalOnStartup !== '')) newProfile.relaunchTerminalOnStartup = baseProfile.relaunchTerminalOnStartup
param = url.searchParams.get('title')
if (param && param !== 'null') newProfile.title = param
if (!('title' in newProfile)) newProfile.title = baseProfile.title
param = url.searchParams.get('xtermOptions')
if (param) newProfile.xtermOptions = JSON.parse(param)
if (!('xtermOptions' in newProfile && newProfile.xtermOptions)) newProfile.xtermOptions = baseProfile.xtermOptions
return newProfile
}
}
export {
ATOM_XTERM_BASE_URI,
AtomXtermProfilesSingleton
}