-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleanSublimeTextProjectHistory.js
107 lines (81 loc) · 3.01 KB
/
cleanSublimeTextProjectHistory.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
#! /usr/local/bin/node
var fs = require("fs");
var strip = require('strip-comments');
var session_file_path;
switch ( process.platform ){
case "win32" :
session_file_path = process.env.APPDATA + "/Sublime Text 3/Local/Session.sublime_session";
break;
case "darwin" :
session_file_path = process.env.HOME + "/Library/Application Support/Sublime Text 3/Local/Session.sublime_session";
break;
case "linux" :
session_file_path = process.env.HOME + "/.config/sublime-text/Local/Session.sublime_session";
}
if ( session_file_path ) {
fs.readFile(session_file_path, "binary", function(err, session_file) {
if(err) {
console.log( err + "\n" );
}
else{
session_file = session_file.replace( /\t/g, '', "g"); // escape tabs
var
sublime_session = JSON.parse(session_file),
cleaned = 0;
console.log('Checking workspaces.recent_workspaces...');
sublime_session.workspaces.recent_workspaces = sublime_session.workspaces.recent_workspaces.filter( checkFilePath);
console.log('Checking projects in workspaces.recent_workspaces...');
sublime_session.workspaces.recent_workspaces = sublime_session.workspaces.recent_workspaces.filter( checkProjectInWorkspace );
console.log('Checking new_window_settings.file_history...');
sublime_session.settings.new_window_settings.file_history = sublime_session.settings.new_window_settings.file_history.filter( checkFilePath );
console.log('Checking select_project.selected_items...');
for (var i = 0, wind; i < sublime_session.windows.length; i++) {
wind = sublime_session.windows[i];
wind.select_project.selected_items = wind.select_project.selected_items.filter( function(e,s) {
return checkFilePath(e.pop());
} );
}
function checkProjectInWorkspace (file_path) {
var
workspace,
project_file_path,
workspace_file = fs.readFileSync(file_path, {encoding: "utf8"});
if ( workspace_file ){
workspace_file = strip(workspace_file.replace( /\t/g, '', "g")); // escape tabs, strip comments
workspace = JSON.parse(workspace_file);
project_file_path = file_path.substr(0, file_path.lastIndexOf("/")) + "/" + workspace.project;
if ( workspace.project && !fs.existsSync(project_file_path) ){
console.log(" Removing '" + project_file_path + "'");
}else{
// console.log('Project DOES exist:', project_file_path);
return true;
}
}
cleaned++;
return false;
}
function checkFilePath(file_path) {
if ( !fs.existsSync(file_path) ){
console.log(" Removing '" + file_path + "'");
cleaned++;
return false;
}else {
return true;
}
}
if ( cleaned > 0 ){
session_file = JSON.stringify(sublime_session);
fs.writeFile(session_file_path, session_file, function (err) {
if (err)
throw err;
else
console.log("Removed " + cleaned + " entries.");
});
}else{
console.log("Nothing to remove.");
}
}
});
} else {
console.log('please enter the session file path manually for your OS: '+process.platform);
}