Handle tilde as home dir in darwin and linux

This commit is contained in:
Krzesimir Nowak
2017-03-28 11:41:56 +02:00
parent de9f491474
commit 1e9083d3b6
3 changed files with 28 additions and 2 deletions

View File

@@ -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}`
}

View File

@@ -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')
@@ -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}"`)
@@ -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')

View File

@@ -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",