From 6fd6473b3035ed2870c4e8e6e4ed8c0509375b8b Mon Sep 17 00:00:00 2001 From: erezrokah Date: Mon, 1 Mar 2021 14:06:25 +0100 Subject: [PATCH 1/2] feat(command-lm): add PATH configuration for fish shell --- src/utils/lm/install.js | 45 ++++++++++++++++++++---------------- src/utils/lm/scripts/fish.sh | 3 +++ src/utils/lm/ui.js | 4 ++-- 3 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 src/utils/lm/scripts/fish.sh diff --git a/src/utils/lm/install.js b/src/utils/lm/install.js index 2614ec8fe42..130935e8615 100644 --- a/src/utils/lm/install.js +++ b/src/utils/lm/install.js @@ -134,33 +134,37 @@ const setupWindowsPath = async function () { ) } -const getInitContent = (incFilePath) => ` +const CONTENT_COMMENT = ` # The next line updates PATH for Netlify's Git Credential Helper. -if [ -f '${incFilePath}' ]; then source '${incFilePath}'; fi ` +const getInitContent = (shell, incFilePath) => { + if (shell === 'fish') { + return `${CONTENT_COMMENT}test -f '${incFilePath}' && source '${incFilePath}' +` + } + + return `${CONTENT_COMMENT}if [ -f '${incFilePath}' ]; then source '${incFilePath}'; fi +` +} + const setupUnixPath = async () => { if (isBinInPath()) { return true } - const { shell, incFilePath, configFile } = shellVariables() - const initContent = getInitContent(incFilePath) + const { shell, incFilePath, configFile } = getShellInfo() - switch (shell) { - case 'bash': - case 'zsh': { - return await Promise.all([ - await copyFileAsync(`${__dirname}/scripts/${shell}.sh`, incFilePath), - await writeConfig(configFile, initContent), - ]) - } - default: { - const error = `Unable to set credential helper in PATH. We don't how to set the path for ${shell} shell. + if (configFile === undefined) { + const error = `Unable to set credential helper in PATH. We don't how to set the path for ${shell} shell. Set the helper path in your environment PATH: ${getBinPath()}` - throw new Error(error) - } + throw new Error(error) } + + return await Promise.all([ + await copyFileAsync(`${__dirname}/scripts/${shell}.sh`, incFilePath), + await writeConfig(configFile, getInitContent(shell, incFilePath)), + ]) } const writeConfig = async function (name, initContent) { @@ -253,9 +257,10 @@ const getLegacyBinPath = function () { const CONFIG_FILES = { bash: '.bashrc', zsh: '.zshrc', + fish: '.config/fish/config.fish', } -const shellVariables = function () { +const getShellInfo = function () { const shellEnv = process.env.SHELL if (!shellEnv) { throw new Error('Unable to detect SHELL type, make sure the variable is defined in your environment') @@ -271,12 +276,12 @@ const shellVariables = function () { const cleanupShell = async function () { try { - const { configFile, incFilePath } = shellVariables() + const { configFile, shell, incFilePath } = getShellInfo() if (configFile === undefined) { return } - await removeConfig(configFile, getInitContent(incFilePath)) + await removeConfig(configFile, getInitContent(shell, incFilePath)) } catch (_) {} } @@ -299,4 +304,4 @@ const removeConfig = async function (name, toRemove) { return await writeFileAsync(configPath, content.replace(toRemove, '')) } -module.exports = { installPlatform, isBinInPath, shellVariables, uninstall } +module.exports = { installPlatform, isBinInPath, getShellInfo, uninstall } diff --git a/src/utils/lm/scripts/fish.sh b/src/utils/lm/scripts/fish.sh new file mode 100644 index 00000000000..123364798e2 --- /dev/null +++ b/src/utils/lm/scripts/fish.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env fish + +set -U fish_user_paths (dirname (status --current-filename)) $fish_user_paths diff --git a/src/utils/lm/ui.js b/src/utils/lm/ui.js index c41675d17b9..3efc53d825a 100644 --- a/src/utils/lm/ui.js +++ b/src/utils/lm/ui.js @@ -3,14 +3,14 @@ const os = require('os') const boxen = require('boxen') const chalk = require('chalk') -const { shellVariables, isBinInPath } = require('./install') +const { getShellInfo, isBinInPath } = require('./install') const printBanner = function (command, force) { const print = force || !isBinInPath() const platform = os.platform() if (print && platform !== 'win32') { - const { incFilePath } = shellVariables() + const { incFilePath } = getShellInfo() const banner = chalk.bold( `Run this command to use Netlify Large Media in your current shell\n\nsource ${incFilePath}`, ) From c5a78ef7a759211fd39606becbfc2365187e0c75 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Mon, 15 Mar 2021 12:04:53 +0100 Subject: [PATCH 2/2] refactor: use same approach for all shels --- src/utils/lm/install.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/utils/lm/install.js b/src/utils/lm/install.js index 130935e8615..70b0f40e14c 100644 --- a/src/utils/lm/install.js +++ b/src/utils/lm/install.js @@ -138,15 +138,7 @@ const CONTENT_COMMENT = ` # The next line updates PATH for Netlify's Git Credential Helper. ` -const getInitContent = (shell, incFilePath) => { - if (shell === 'fish') { - return `${CONTENT_COMMENT}test -f '${incFilePath}' && source '${incFilePath}' -` - } - - return `${CONTENT_COMMENT}if [ -f '${incFilePath}' ]; then source '${incFilePath}'; fi -` -} +const getInitContent = (incFilePath) => `${CONTENT_COMMENT}test -f '${incFilePath}' && source '${incFilePath}'` const setupUnixPath = async () => { if (isBinInPath()) { @@ -163,7 +155,7 @@ Set the helper path in your environment PATH: ${getBinPath()}` return await Promise.all([ await copyFileAsync(`${__dirname}/scripts/${shell}.sh`, incFilePath), - await writeConfig(configFile, getInitContent(shell, incFilePath)), + await writeConfig(configFile, getInitContent(incFilePath)), ]) } @@ -276,12 +268,12 @@ const getShellInfo = function () { const cleanupShell = async function () { try { - const { configFile, shell, incFilePath } = getShellInfo() + const { configFile, incFilePath } = getShellInfo() if (configFile === undefined) { return } - await removeConfig(configFile, getInitContent(shell, incFilePath)) + await removeConfig(configFile, getInitContent(incFilePath)) } catch (_) {} }