Skip to content

Commit

Permalink
Handle tilde as home dir in darwin and linux
Browse files Browse the repository at this point in the history
  • Loading branch information
krnowak committed Mar 28, 2017
1 parent de9f491 commit 1e9083d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
24 changes: 24 additions & 0 deletions script/lib/handle-tilde.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const os = require('os')
const passwdUser = require('passwd-user')
const path = require('path')

module.exports = function (aPath) {
if (!aPath.startsWith('~')) {
return aPath
}

const sepIndex = aPath.indexOf(path.sep)
const user = (sepIndex < 0) ? aPath.substring(1) : aPath.substring(1, sepIndex)
const rest = (sepIndex < 0) ? '' : aPath.substring(sepIndex)
const home = (user === '') ? os.homedir() : (() => {
const passwd = passwdUser.sync(user);
if (passwd === undefined) {
throw new Error(`Failed to expand the tilde in ${aPath} - user "${user}" does not exist`)
}
return passwd.homedir
})()

return `${home}${rest}`
}
5 changes: 3 additions & 2 deletions script/lib/install-application.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const fs = require('fs-extra')
const handleTilde = require('./handle-tilde')
const path = require('path')
const runas = require('runas')
const template = require('lodash.template')
Expand All @@ -10,7 +11,7 @@ const CONFIG = require('../config')
module.exports = function (packagedAppPath, installDir) {
const packagedAppFileName = path.basename(packagedAppPath)
if (process.platform === 'darwin') {
const installPrefix = installDir !== '' ? installDir : path.join(path.sep, 'Applications')
const installPrefix = installDir !== '' ? handleTilde(installDir) : path.join(path.sep, 'Applications')
const installationDirPath = path.join(installPrefix, packagedAppFileName)
if (fs.existsSync(installationDirPath)) {
console.log(`Removing previously installed "${packagedAppFileName}" at "${installationDirPath}"`)
Expand Down Expand Up @@ -41,7 +42,7 @@ module.exports = function (packagedAppPath, installDir) {
const apmExecutableName = CONFIG.channel === 'beta' ? 'apm-beta' : 'apm'
const appName = CONFIG.channel === 'beta' ? 'Atom Beta' : 'Atom'
const appDescription = CONFIG.appMetadata.description
const prefixDirPath = installDir !== '' ? installDir : path.join('/usr', 'local')
const prefixDirPath = installDir !== '' ? handleTilde(installDir) : path.join('/usr', 'local')
const shareDirPath = path.join(prefixDirPath, 'share')
const installationDirPath = path.join(shareDirPath, atomExecutableName)
const applicationsDirPath = path.join(shareDirPath, 'applications')
Expand Down
1 change: 1 addition & 0 deletions script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"mkdirp": "0.5.1",
"normalize-package-data": "2.3.5",
"npm": "3.10.5",
"passwd-user": "2.1.0",
"pegjs": "0.9.0",
"runas": "3.1.1",
"season": "5.3.0",
Expand Down

0 comments on commit 1e9083d

Please sign in to comment.