如何从保存的Bash变量运行ESLint?

问题描述 投票:0回答:1

我正在设置一个项目初始化脚本和一个git pre-commit钩子来在项目上工作。我们要运行的脚本不是在根目录中而是在子目录中运行,我想保持这种方式。

我想要做的是将eslint和phpcs的二进制可执行文件的完整相对路径设置为bash变量,然后能够运行它们。我还希望能够从bash变量执行composer.phar二进制文件。

所以这就是我所做的。

# Set tool paths
PHPCS_PATH=`./wp-content/themes/our-theme/vendor/bin/phpcs`
PHPCBF_PATH=`./wp-content/themes/our-theme/vendor/bin/phpcbf`
ESLINT_PATH=`./wp-content/themes/our-theme/node_modules/.bin/eslint`
SASSLINT_PATH=`./wp-content/themes/our-theme/node_modules/.bin/sass-lint`
COMPOSER_PATH=`./wp-content/themes/our-theme/composer.phar`

我正在尝试从项目根目录在本地测试这些路径,并且不断收到错误。

我将这些行之一复制并粘贴到我的终端中,按Enter键,然后执行以下一项操作:

  • [${ESLINT_PATH}command ${ESLINT_PATH}以及"${ESLINT_PATH}"$ESLINT_PATH都产生我...

    zsh: command too long: eslint [options] file.js [file.js] [dir]\n\nBasic configuration...
    
  • [eval "${ESLINT_PATH}"eval $ESLINT_PATHeval "$ESLINT_PATH"都产生我...

    zsh: no matches found: [options]
    zsh: command not found: Basic
    zsh: command not found: --no-eslintrc
    zsh: command not found: -c,
    zsh: no matches found: [String]
    

我失去理智了吗?我该如何使路径可执行?如果我使用路径的内容并运行它,它就可以正常工作。

示例:./wp-content/themes/swmaster/node_modules/.bin/eslint实际上告诉我指定路径。

我在这里做错了什么?

bash variables eslint lint
1个回答
0
投票

看起来您的问题出在这些作业中:

# Set tool paths
PHPCS_PATH=`./wp-content/themes/our-theme/vendor/bin/phpcs`
PHPCBF_PATH=`./wp-content/themes/our-theme/vendor/bin/phpcbf`
ESLINT_PATH=`./wp-content/themes/our-theme/node_modules/.bin/eslint`
SASSLINT_PATH=`./wp-content/themes/our-theme/node_modules/.bin/sass-lint`
COMPOSER_PATH=`./wp-content/themes/our-theme/composer.phar`

那些反引号是命令替换。一种更易于阅读的书写方式是:

PHPCS_PATH=$(./wp-content/themes/our-theme/vendor/bin/phpcs)
...

我认为这不是您真正想要的。命令替换实际上执行内部内容,并由stdout中的任何内容替换。我认为您实际上是在尝试进行字符串分配。因为路径上没有空格,所以只需删除后退标记即可完成此操作。但是,无论如何都要双引号是一个好习惯。试试这个:

# Set tool paths
PHPCS_PATH="./wp-content/themes/our-theme/vendor/bin/phpcs"
PHPCBF_PATH="./wp-content/themes/our-theme/vendor/bin/phpcbf"
ESLINT_PATH="./wp-content/themes/our-theme/node_modules/.bin/eslint"
SASSLINT_PATH="./wp-content/themes/our-theme/node_modules/.bin/sass-lint"
COMPOSER_PATH="./wp-content/themes/our-theme/composer.phar"

解决您问题的另一种方法是将这些目录添加到PATH变量中。例如,如果将PATH设置为包括phpcs和phpcbf的bin目录,则无需指定完整(或相对路径)路径就可以执行那些程序:

export PATH=$PATH:./wp-content/themes/our-theme/vendor/bin
# or export PATH=$PATH:/full/path/to/wp-content/themes/our-theme/vendor/bin
# Now you can just run the line below without a path...
phpcs
© www.soinside.com 2019 - 2024. All rights reserved.