Husky 和 ​​lint-staged 无法运行 gulp 命令

Posted

技术标签:

【中文标题】Husky 和 ​​lint-staged 无法运行 gulp 命令【英文标题】:Husky and lint-staged unable to run gulp command 【发布时间】:2020-08-18 23:30:36 【问题描述】:

我正在尝试从 package.json 运行 gulp 命令。但无法执行。

这是我的 package.json。

  "scripts":        
    "deploy": "gulp deploy",
    "lint": "eslint",
    "lint-fix": "eslint --ext .js"
  ,
  "husky": 
    "hooks": 
      "pre-commit": "lint-staged"
    
  ,
  "lint-staged": 
    "*": [
      "gulp locale-sass", // not working
      "npm run lint", // working fine
      "git add"
    ]
  ,
  "devDependencies": 
    "@open-tech-world/cli-progress-bar": "^1.0.6",
    "colors": "^1.4.0",
    "console-table-printer": "^1.1.58",
    "del": "^5.1.0",
    "eslint": "^6.8.0",
    "eslint-plugin-deprecate": "^0.7.0",
    "glob": "^7.1.6",
    "gulp": "^4.0.2",
    "gulp-changed": "^4.0.2",
    "gulp-clean": "^0.4.0",
    "gulp-clean-css": "^4.2.0",
    "gulp-compass": "^2.1.0",
    "gulp-concat": "^2.6.1",
    "gulp-consolidate": "0.2.0",
    "gulp-css-globbing": "^0.2.2",
    "gulp-debug": "^4.0.0",
    "gulp-eslint": "^6.0.0",
    "gulp-ext-replace": "^0.3.0",
    "gulp-if": "^3.0.0",
    "gulp-jshint": "^2.1.0",
    "gulp-livereload": "^4.0.2",
    "gulp-load-plugins": "^2.0.2",
    "gulp-rename": "^2.0.0",
    "gulp-replace": "^1.0.0",
    "gulp-sass": "^4.0.2",
    "gulp-sass-lint": "^1.4.0",
    "gulp-scan": "^0.1.4",
    "gulp-shell": "^0.7.1",
    "gulp-sourcemaps": "^2.6.5",
    "gulp-svg-symbols": "^3.2.3",
    "gulp-svgmin": "^2.2.0",
    "gulp-terser": "^1.2.0",
    "gulp-util": "^3.0.8",
    "gulp-watch": "^5.0.1",
    "husky": "^4.2.5",
    "jshint": "^2.11.0",
    "jshint-stylish": "2.2.1",
    "lint-staged": "^10.2.2",
    "minimist": "^1.2.0",
    "node-sass-asset-functions": "^0.1.0",
    "npm-audit-resolver": "^2.2.0",
    "path": "^0.12.7",
    "sass-lint": "^1.13.1",
    "through2": "^3.0.1"
  

npm run lint 工作正常。

这里 gulp locale-sass 不起作用。

✖ gulp locale-sass && npm run lint: 任务从未定义:&&

列出可用任务,尝试运行:gulp --tasks

使用 gulpfile /u02/home/user01/drupal/drupal-7.9/sites/medic/gulpfile.js

husky > pre-commit hook 失败(添加 --no-verify 绕过)

【问题讨论】:

你试过./node_modules/bin/gulp locale-sass吗?当 npm 运行 gulp 时,可能在本地和全局解决 gulp 存在问题。 UPD 不,我知道这不是 Gulp。它只是本地 sass。 @vitkarpov 不。 ./node_modules/bin/gulp locale-sass 没有输出(ENOENT)失败。 我有个想法,如果你将“gulp local-sass”移动到 npm 命令中,就像你已经拥有的 npm run lint 一样?会一样吗?首先,您可以将它作为 npm run sass (示例名称)运行吗?如果是,请尝试在 lint-staged 中进行更改。 @vitkarpov 结果相同。任务从未定义。和之前一样。 那么,我说的 npm run sass 也不起作用是对的吗? 【参考方案1】:

我通过添加 "locale-sass"lint-staged npm run gulp locale-sass 而不是 gulp locale-sass 来修改 srcipt 部分来解决问题

"scripts":        
    "deploy": "gulp deploy",
    "lint": "eslint",
    "lint-fix": "eslint --ext .js",
    "locale-sass": "gulp locale-sass"
  ,
  "husky": 
    "hooks": 
      "pre-commit": "lint-staged"
    
  ,
  "lint-staged": 
    "*": [
      "npm run gulp locale-sass", // Working
      "npm run lint", // working
      "git add"
    ]
  ,

【讨论】:

以上是关于Husky 和 ​​lint-staged 无法运行 gulp 命令的主要内容,如果未能解决你的问题,请参考以下文章

Husky/lint-staged 是不是可以排除/忽略文件?

代码提交规范 husky + commitlint + lint-staged

如何将 lint-stage 与 Husky 集成?

husky+lint-staged 在非暂存文件上运行

用 husky 和 lint-staged 构建代码检查工作流

用 husky 和 lint-staged 构建代码检查工作流