Created
October 16, 2018 01:45
-
-
Save tomprogers/1389dd781b41e4331b22fc2f9ea73c18 to your computer and use it in GitHub Desktop.
Script that reads all *.sublime-project files in a directory and aggregates their values
This file contains hidden or 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
console.clear() | |
const FS = require('fs') | |
async function readdir(path) { | |
return new Promise((resolve, reject) => { | |
FS.readdir(path, (err, data) => { | |
if(err) reject(err) | |
else resolve(data) | |
}) | |
}) | |
} | |
async function readFile(path, options) { | |
return new Promise((resolve, reject) => { | |
FS.readFile(path, options, (err, data) => { | |
if(err) reject(err) | |
else resolve(data) | |
}) | |
}) | |
} | |
async function getProjects() { | |
let files = await readdir('.'); | |
let projects = files.filter(filename => filename.includes('sublime')) | |
return await Promise.all( | |
projects.map(async filename => { | |
let content = await readFile(filename, { encoding: 'utf8' }) | |
content = content.replace(/(\/\/.*?)$/gmi, '') | |
try { | |
return JSON.parse(content) | |
} catch (error) { | |
console.error(error) | |
console.log(`----- ${filename} -------`) | |
console.log(content) | |
process.exit() | |
} | |
}) | |
) | |
} | |
async function main() { | |
let projects = await getProjects() | |
// collect folder_exclude_patterns | |
let folderSettings = projects | |
.map(project => project.folders) | |
.reduce((all, prj) => { | |
if(prj === undefined) return all | |
else return all.concat(prj) | |
}, []) | |
.reduce((settings, folder) => { | |
settings.folder_exclude_patterns = settings.folder_exclude_patterns.concat( folder.folder_exclude_patterns ) | |
settings.file_exclude_patterns = settings.file_exclude_patterns.concat( folder.file_exclude_patterns ) | |
return settings | |
}, { | |
folder_exclude_patterns: [], | |
file_exclude_patterns: [] | |
}) | |
console.log(JSON.stringify(folderSettings)) | |
// collect all settings | |
let settingsStats = projects | |
.map(project => project.settings) | |
.reduce((stats, settings, i) => { | |
if(settings === undefined) { | |
return stats | |
} | |
Object.keys(settings) | |
.forEach(settingName => { | |
let settingValue = settings[settingName] | |
if(!stats.hasOwnProperty(settingName)) | |
stats[settingName] = [] | |
let valueIndex = stats[settingName] | |
.findIndex(row => row[0] === settingValue) | |
if(valueIndex === -1) | |
stats[settingName].push([ settings[settingName], 1 ]) | |
else { | |
stats[settingName][valueIndex][1] += 1 | |
} | |
}) | |
return stats | |
}, {}) | |
console.log(JSON.stringify(settingsStats)) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment