Created
March 14, 2019 17:25
-
-
Save tcrowe/cbb6978cd1d22341d7b463608d7e1fac to your computer and use it in GitHub Desktop.
Delete all the snippets in Sublimetext's packages and also the user's installed packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
⚠️ It does not prompt or ask permission. It just deletes all the snippets! | |
usage: node osx-snippet-delete.js | |
*/ | |
const path = require("path"); | |
const {exec} =require("child_process") | |
const appPackagesPath = "/Applications/Sublime Text.app/Contents/MacOS/Packages"; | |
const userPackagesPath = path.join(process.env.HOME, "Library", "Application Support", "Sublime Text 3", "Installed Packages"); | |
const patterns = [ | |
"*Snippets*", | |
"*.sublime-snippet", | |
"*.tmSnippet" | |
]; | |
/** | |
* List files that are sublime packages | |
*/ | |
function listPackages(absolutePath, done) { | |
const cmd = `find "${userPackagesPath}" -type f -iname "*.sublime-package"` | |
exec(cmd, function(err, stdout) { | |
if (err !== undefined && err !== null) { | |
return done(err) | |
} | |
done(null, stdout.trim().split("\n").map(item => item.trim())) | |
}) | |
} | |
/** | |
* Delete inside a zip using a pattern | |
*/ | |
function zipDelete(absolutePath, deletePattern, done) { | |
const cmd = `zip -d "${absolutePath}" "${deletePattern}"` | |
exec(cmd, function(err, stdout, stderr) { | |
if (err !== undefined && err !== null) { | |
if (err.code === 12) { | |
// "nothing to do" message | |
return done(); | |
} | |
console.log("stdout", stdout) | |
console.log("stderr", stderr) | |
return done(err); | |
} | |
done(); | |
}) | |
} | |
/** | |
* Use all the patterns to queue up some deletes | |
*/ | |
function zipPatternDelete(absolutePath, sleep) { | |
patterns.forEach(function(pattern, index) { | |
setTimeout(function() { | |
zipDelete(absolutePath, pattern, function(err) { | |
if (err !== undefined && err !== null) { | |
console.error("error deleting app package snippets", err) | |
console.log("pattern", pattern) | |
console.log("absolutePath", absolutePath) | |
return | |
} | |
console.log("deleted", pattern, absolutePath) | |
}) | |
}, sleep * index) | |
}) | |
} | |
// | |
// GO! | |
// | |
listPackages(appPackagesPath, function(err, appPackages) { | |
if (err !== undefined && err !== null) { | |
return console.error("error getting app packages list", err) | |
} | |
appPackages.forEach(function(item, index) { | |
zipPatternDelete(item, index * 100) | |
}) | |
}) | |
listPackages(userPackagesPath, function(err, userPackages) { | |
if (err !== undefined && err !== null) { | |
return console.error("error getting user packages list", err) | |
} | |
userPackages.forEach(function(item, index) { | |
zipPatternDelete(item, index * 100) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment