Skip to content

Instantly share code, notes, and snippets.

@th507
Created April 7, 2017 16:58
Show Gist options
  • Select an option

  • Save th507/eb44aaea1f79b99694fb9a291aa2d87d to your computer and use it in GitHub Desktop.

Select an option

Save th507/eb44aaea1f79b99694fb9a291aa2d87d to your computer and use it in GitHub Desktop.
Reduce disk usage for Microsoft Office 365 (Mac) by linking identical files
const fs = require('fs')
const child = require('child_process')
if (process.env.SUDO_UID === undefined) {
console.log(`Permission denied
sudo required`)
process.exit(1)
}
const getTarget = undef => {
let target = process.argv.pop()
if (target.endsWith('js')) {
console.log("Target not defined")
process.exit(2)
}
const lib = 'Excel PowerPoint Outlook OneNote'
target = new RegExp(target, 'i').exec(lib)
if (target === undef) {
console.log("Target not found")
process.exit(3)
}
if (!fs.existsSync(`/Applications/Microsoft ${target[0]}.app`)) {
console.log(`${target[0]} not installed.`)
process.exit(4)
}
return target[0]
}
let target = getTarget()
const appDir = (_, name, subDir) => {
var out = `/Applications/Microsoft ${name}.app/Contents/Frameworks`
if (subDir) {
out += `/${subDir}`
}
return out
}
const appProofingDir = (_, name) => `/Applications/Microsoft ${name}.app/Contents/SharedSupport/Proofing Tools`
const dir = new Proxy({}, {
get: (_, name) => new Promise((yes, no) => {
fs.readdir(appDir`${name}`, (err, files, undef) => {
(err === undef) ? no(err) : yes(files)
})
})
})
Promise.all([dir.Word, dir[target]])
.then(([word, excel]) => {
let _in = []
let _out = []
excel.forEach(ex => (word.includes(ex) ? _in : _out).push(ex))
return [_in, _out]
})
.then(([_in, _out]) => {
let Word = "Word"
_in.forEach(d => {
console.log('Removing', appDir`${target}${d}`)
child.execSync('rm -fr "' + appDir`${target}${d}` + '"')
console.log('Linking', appDir`${target}${d}`)
child.execSync('ln -fs "' + appDir`${Word}${d}` + '" "' + appDir`${target}${d}` + '"')
console.log()
})
console.log('Removing', appProofingDir`${target}`)
child.execSync('rm -fr "' + appProofingDir`${target}` + '"')
console.log('Linking', appProofingDir`${target}`)
child.execSync('ln -fs "' + appProofingDir`${Word}` + '" "' + appProofingDir`${target}` + '"')
console.log()
console.log('----')
})
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment