@@ -29,6 +29,7 @@ const pathsToCheck = [process.env.HOME+'/.local/share/applications/', '/usr/loca
29
29
addPathsToCheck ( process . env [ 'XDG_DATA_DIRS' ] ?. split ( ":" ) || [ ] ) ;
30
30
addPathsToCheck ( process . env [ 'XDG_DATA_HOME' ] ?. split ( ':' ) || [ ] ) ;
31
31
32
+ const isRoot = process . getuid ( ) === 0 || process . getgid ( ) === 0 ;
32
33
33
34
// noinspection SpellCheckingInspection
34
35
console . log ( "\r\ndesktopmenuitem" . blue , version . blue , "\r\n---------------------" ) ;
@@ -40,6 +41,8 @@ program.description('An application for creating or editing .desktop files');
40
41
program . option ( "--view" , "View .desktop file" ) ;
41
42
program . option ( "--edit" , "Call your editor with the .desktop file" ) ;
42
43
program . option ( "--list" , "List all .desktop files" ) ;
44
+ program . option ( "--changelog" , "Display the changelog" ) ;
45
+ program . option ( "--overwrite" , "Over write the original file location, if root." )
43
46
program . option ( "-d, --desktop <file>" , "Desktop file to use" ) ;
44
47
program . option ( "-k, --keywords <keywords>" , "Set keywords" ) ;
45
48
program . option ( "-m, --mime <type>" , "Set mime type" ) ;
@@ -56,10 +59,30 @@ const options = program.opts();
56
59
57
60
// List Directories
58
61
if ( options . list ) {
59
- pathsToCheck . forEach ( ( val ) => {
60
- listDirectory ( val ) ;
61
- console . log ( "" ) ;
62
- } ) ;
62
+ if ( program . args . length ) {
63
+ pathsToCheck . forEach ( ( val ) => {
64
+ if ( val . indexOf ( program . args [ 0 ] ) >= 0 ) {
65
+ listDirectory ( val ) ;
66
+ console . log ( "" ) ;
67
+ }
68
+ } ) ;
69
+ } else {
70
+ pathsToCheck . forEach ( ( val ) => {
71
+ listDirectory ( val ) ;
72
+ console . log ( "" ) ;
73
+ } ) ;
74
+ }
75
+ process . exit ( 0 ) ;
76
+ }
77
+
78
+ // Show the Changelog
79
+ if ( options . changelog ) {
80
+ let startPath = path . normalize ( process . argv [ 1 ] . replace ( path . basename ( process . argv [ 1 ] ) , '' ) + "../lib/node_modules/@master.technology/desktopmenuitem/changelog" ) ;
81
+ if ( fs . existsSync ( startPath ) ) {
82
+ console . log ( fs . readFileSync ( startPath ) . toString ( ) ) ;
83
+ } else {
84
+ console . log ( "Missing changelog" . red ) ;
85
+ }
63
86
process . exit ( 0 ) ;
64
87
}
65
88
@@ -100,7 +123,7 @@ if (program.args.length && program.args[0].endsWith(".desktop")) {
100
123
// Figure out the Application Name to show as the Menu
101
124
if ( options . name == null || options . name . length === 0 ) {
102
125
if ( program . args . length ) {
103
- options . dynamicName = cleanName ( path . basename ( program . args [ 0 ] , ".desktop" ) ) ;
126
+ options . dynamicName = properCase ( cleanName ( path . basename ( program . args [ 0 ] , ".desktop" ) ) ) ;
104
127
} else if ( options . desktop != null && options . desktop . length ) {
105
128
options . dynamicName = path . basename ( options . desktop , ".desktop" ) ;
106
129
}
@@ -116,30 +139,30 @@ if (options.exec == null || options.exec.length === 0) {
116
139
}
117
140
118
141
const info = loadFile ( desktopFile ) ;
119
- if ( options . edit && ! info . _created ) {
120
- spawnEditor ( info . _pathToDesktopFile ) ;
121
- spawnUpdateDB ( info . _pathToDesktopFile . replace ( path . basename ( info . _pathToDesktopFile ) , '' ) ) ;
142
+ if ( options . edit && ! info . __internal . created ) {
143
+ spawnEditor ( info . __internal . pathWithDesktopFile ) ;
144
+ spawnUpdateDB ( info . __internal . pathToDesktopFile ) ;
122
145
process . exit ( 0 ) ;
123
146
}
124
147
125
148
// If they just want to view the file...
126
149
if ( options . view ) {
150
+ delete info . __internal ;
127
151
console . log ( info ) ;
128
152
process . exit ( 0 ) ;
129
153
}
130
154
131
- if ( configureDesktopEntry ( info ) ) {
132
- // console.log(info);
133
- const fileName = info [ '_pathToDesktopFile' ] ;
134
- delete info [ '_pathToDesktopFile' ] ;
135
- delete info [ '_created' ] ;
155
+ if ( configureDesktopEntry ( info ) || options . edit ) {
156
+ const fileName = info . __internal . pathWithDesktopFile ;
157
+ const pathToDesktopFile = info . __internal . pathToDesktopFile ;
158
+ delete info . __internal ;
136
159
fs . writeFileSync ( fileName , ini . stringify ( info ) ) ;
137
160
if ( options . edit ) {
138
161
spawnEditor ( fileName ) ;
139
162
} else {
140
163
console . log ( "Saved:" , fileName ) ;
141
164
}
142
- spawnUpdateDB ( fileName . replace ( path . basename ( fileName ) , '' ) ) ;
165
+ spawnUpdateDB ( pathToDesktopFile ) ;
143
166
} else {
144
167
console . log ( "No Changes" ) ;
145
168
}
@@ -153,7 +176,6 @@ if (configureDesktopEntry(info)) {
153
176
* @param path
154
177
*/
155
178
function spawnUpdateDB ( path ) {
156
- console . log ( path ) ;
157
179
const updater = [ '/usr/bin/update-desktop-database' ] ;
158
180
for ( let i = 0 ; i < updater . length ; i ++ ) {
159
181
if ( fs . existsSync ( updater [ i ] ) ) {
@@ -259,7 +281,6 @@ function cleanName(name) {
259
281
// Remove known app extensions
260
282
let newName = name . replace ( / \. a p p i m a g e / gi, "" ) ;
261
283
262
-
263
284
let offset = 0 ;
264
285
do {
265
286
offset = findFirstOffset ( newName , offset ) ;
@@ -284,8 +305,7 @@ function cleanName(name) {
284
305
}
285
306
} while ( offset !== - 1 ) ;
286
307
287
- // Upper case first letter
288
- return newName . substr ( 0 , 1 ) . toUpperCase ( ) + newName . substr ( 1 ) ;
308
+ return newName ;
289
309
}
290
310
291
311
/**
@@ -336,18 +356,20 @@ function listDirectory(path) {
336
356
/**
337
357
* This finds and loads a .desktop file, or creates a new .desktop file
338
358
* @param fileName
339
- * @returns {{"Desktop Entry": {Exec: string, Type: string, Keywords: string, Categories: string, Icon: string, Terminal: boolean, MimeType: string, Name: string}, _created: boolean, _pathToDesktopFile: string}|* }
359
+ * @returns {object }
340
360
*/
361
+
341
362
function loadFile ( fileName ) {
342
363
//console.log("Searching for:", fileName.blue);
343
364
for ( let i = 0 ; i < pathsToCheck . length ; i ++ ) {
365
+ //console.log("Looking at", pathsToCheck[i] + fileName)
344
366
if ( fs . existsSync ( pathsToCheck [ i ] + fileName ) ) {
345
- console . log ( "Loading:" , ( pathsToCheck [ i ] + fileName ) . blue ) ;
367
+ console . log ( "Loading:" , ( pathsToCheck [ i ] + fileName ) . blue ) ;
346
368
return parseFile ( pathsToCheck [ i ] + fileName ) ;
347
369
}
348
370
}
349
371
350
- console . log ( "Creating:" , ( pathsToCheck [ 0 ] + fileName ) . blue ) ;
372
+ console . log ( "Creating:" , ( pathsToCheck [ 0 ] + properCase ( fileName ) ) . blue ) ;
351
373
return {
352
374
"Desktop Entry" :
353
375
{
@@ -360,8 +382,12 @@ function loadFile(fileName) {
360
382
Icon : '' ,
361
383
Exec : ''
362
384
} ,
363
- _pathToDesktopFile : pathsToCheck [ 0 ] + fileName ,
364
- _created : true
385
+ __internal : {
386
+ pathToDesktopFile : pathsToCheck [ 0 ] ,
387
+ desktopFile : fileName ,
388
+ pathWithDesktopFile : pathsToCheck [ 0 ] + properCase ( fileName ) ,
389
+ created : true
390
+ }
365
391
} ;
366
392
}
367
393
@@ -371,8 +397,21 @@ function loadFile(fileName) {
371
397
* @returns {object }
372
398
*/
373
399
function parseFile ( file ) {
400
+ let created = false ;
374
401
let data = ini . parse ( fs . readFileSync ( file , 'utf-8' ) ) ;
375
- data . _pathToDesktopFile = file ;
402
+ if ( file . indexOf ( pathsToCheck [ 0 ] ) !== 0 ) {
403
+ if ( ! options . overwrite || ! isRoot ) {
404
+ file = pathsToCheck [ 0 ] + path . basename ( file ) ;
405
+ console . log ( "Saving as:" , file . blue ) ;
406
+ created = true ;
407
+ }
408
+ }
409
+ data . __internal = {
410
+ pathWithDesktopFile : file ,
411
+ desktopFile : path . basename ( file ) ,
412
+ pathToDesktopFile : file . replace ( path . basename ( file ) , '' ) ,
413
+ created : created
414
+ } ;
376
415
return data ;
377
416
}
378
417
@@ -402,3 +441,13 @@ function pathNormalize(inPath) {
402
441
return path . normalize ( process . cwd ( ) + "/" + tempFile ) ;
403
442
}
404
443
}
444
+
445
+ /**
446
+ * Proper-cases a word
447
+ * @param str
448
+ * @returns {string }
449
+ */
450
+ function properCase ( str )
451
+ {
452
+ return str . toLowerCase ( ) . replace ( / ^ ( .) | \s ( .) / g, function ( $1 ) { return $1 . toUpperCase ( ) ; } ) ;
453
+ }
0 commit comments