如何从生成器存储库运行全局生成生成器?

时间:2018-01-24 02:45:38

标签: node.js code-generation generate

说我们在存储库generate-mygenerator中。 因此,我们在根目录中有一个generator.js文件。

如果我想运行gen gh-repo怎么办?

我收到以下错误:

Error: Cannot find module 'lazy-cache'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/remi/d/dev/generate-package/utils.js:3:13)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
/home/remi/d/dev/generate/bin/generate.js:78
if (err) return app.emit('error', err);
                  ^

TypeError: Cannot read property 'emit' of undefined
at /home/remi/d/dev/generate/bin/generate.js:78:23
at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/base-runner/index.js:122:7)
at Liftoff.execute (/home/remi/d/dev/generate/node_modules/liftoff/index.js:203:12)
at module.exports (/home/remi/d/dev/generate/node_modules/flagged-respawn/index.js:51:3)
at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:195:5)
at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:170:7)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)

我是否要将generator.js重命名为generator.js.wait,然后在生成器完成后将其重命名?

重现错误的步骤:

generate-package存储库中的示例:

remi@poremil:~
$ cd d/dev/generate-package/
remi@poremil:~/d/dev/generate-package:master
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
remi@poremil:~/d/dev/generate-package:master
$ node -v
v8.9.4
remi@poremil:~/d/dev/generate-package:master
$ npm -v
5.6.0
remi@poremil:~/d/dev/generate-package:master
$ ls
docs  generator.js  index.js  LICENSE  package.json  README.md  templates  test  utils.js

正在运行generate-gh-repo

remi@poremil:~/d/dev/generate-package:master
$ gen gh-repo
[23:33:06] starting generate
[23:33:06] using generator ~/d/dev/generate-package/generator.js
Error: Cannot find module 'lazy-cache'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/remi/d/dev/generate-package/utils.js:3:13)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
/home/remi/d/dev/generate/bin/generate.js:78
  if (err) return app.emit('error', err);
                      ^

TypeError: Cannot read property 'emit' of undefined
    at /home/remi/d/dev/generate/bin/generate.js:78:23
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/base-runner/index.js:122:7)
    at Liftoff.execute (/home/remi/d/dev/generate/node_modules/liftoff/index.js:203:12)
    at module.exports (/home/remi/d/dev/generate/node_modules/flagged-respawn/index.js:51:3)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:195:5)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:170:7)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:686:11)
    at startup (bootstrap_node.js:187:16)

或者相同的运行generate-git

remi@poremil:~/d/dev/generate-package:master
$ gen git
[23:33:16] starting generate
[23:33:16] using generator ~/d/dev/generate-package/generator.js
Error: Cannot find module 'lazy-cache'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/remi/d/dev/generate-package/utils.js:3:13)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
/home/remi/d/dev/generate/bin/generate.js:78
  if (err) return app.emit('error', err);
                      ^

TypeError: Cannot read property 'emit' of undefined
    at /home/remi/d/dev/generate/bin/generate.js:78:23
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/base-runner/index.js:122:7)
    at Liftoff.execute (/home/remi/d/dev/generate/node_modules/liftoff/index.js:203:12)
    at module.exports (/home/remi/d/dev/generate/node_modules/flagged-respawn/index.js:51:3)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:195:5)
    at Liftoff.<anonymous> (/home/remi/d/dev/generate/node_modules/liftoff/index.js:170:7)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:686:11)
    at startup (bootstrap_node.js:187:16)

1 个答案:

答案 0 :(得分:0)

原理

只要generate-gh-repo全局安装且您的gh-repo文件中没有名为generator.js的任务,就应该运行全局生成器。

如果您的generator.js文件中的任务与您尝试运行的全局安装的生成器同名,则该任务将运行。这是因为generate将以比全局生成器更高的优先级解析任务。

您可以通过运行gen gh-repo:default来运行全局生成器(将gh-repo替换为您尝试运行的实际生成器)。这告诉generate在gh-repo生成器上查找默认任务。如果您未在gh-repo文件中注册generator.js生成器,请执行以下操作:

app.register('gh-repo', require('generate-gh-repo'));

然后生成应该找到全局安装的生成器。

它应该可以工作

当你运行gen gh-repo时,它应该可以工作。

要避免此错误,只需安装本地npm依赖项

我只是简单地尝试在另一台尚未安装本地节点模块的其他生成器的根文件夹中运行全局生成器来重现错误行为!

generate-package存储库中的示例:

安装npm依赖项:

remi@poremil:~/d/dev/generate-package:master
$ npm install
npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning EINTEGRITY: sha1-x6vpzIuHwLqodrGf3oP9RkeX44w= integrity checksum failed when using sha1: wanted sha1-x6vpzIuHwLqodrGf3oP9RkeX44w= but got sha512-D5JUjPyJbaJDkuAazpVnSfVkLlpeO3wDlPROTMLGKG1zMFNFRgrciKo1ltz/AzNTkqE0HzDx655QOL51N06how==. (4087 bytes)
npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation.
npm notice created a lockfile as package-lock.json. You should commit this file.
added 982 packages in 38.844s

再次运行generate-gh-repo

remi@poremil:~/d/dev/generate-package:master
$ gen gh-repo
[02:31:11] starting generate
[02:31:11] using generator ~/d/dev/generate-package/generator.js
[02:31:11] ✔ running tasks: [ 'gh-repo' ]
[02:31:11] starting gh-repo
[02:31:11] starting gh-repo:default task
[02:31:11] starting gh-repo:gh-repo task

? Found saved GitHub authentication. Would you like to use it? (Y/n)

generate-git

remi@poremil:~/d/dev/generate-package:master
$ gen git
[02:31:00] starting generate
[02:31:01] using generator ~/d/dev/generate-package/generator.js
[02:31:01] ✔ running tasks: [ 'git' ]

问题解决了!