我为generate-swap-project
生成器工具创建了一个名为generate
的生成器插件。
我们先来看看:
- https://github.com/generate:一个用于搭建GitHub项目的新命令行工具和开发人员框架。 Yeoman的一个更强大,可组合,更实用的替代方案。
- https://github.com/generate/generate:一个用于搭建GitHub项目的新命令行工具和开发人员框架。 Generate提供了Yeoman的稳健性和可配置性,Slush的表现力和简洁性,以及比任何一种都更强大的流控制和可组合性。
- https://github.com/rbecheras/generate-swap-project SWAP项目的生成器(我正在处理的生成器)
- https://github.com/rbecheras/generate-swap-project/tree/1-convert-to-standard-code-style:SWAP项目的生成器(主分支上的自述文件)
- https://github.com/rbecheras/generate-swap-project/tree/1-convert-to-standard-code-style :(有问题的分支自述文件)
主分支工作正常,测试传递给travis和本地开发主机。 测试由以下纱线脚本(在主机上)运行:
$ yarn test
但是在PR #2 Resolve #1 "Convert to ESNext/StandardJS code style"
中,我做了以下更改:
ES5
代码库转换为ESNext
src/lib
和src/tests
generator.js
文件中添加了一个只需要./dist/lib/generator.js
测试由以下纱线脚本(在分支1-convert-to-standard-code-style
上)运行:
$ yarn travis
或(别名)
$ yarn pipeline
这是yarn clear && yarn lint && yarn build && yarn test
但是一次测试失败(1次测试失败,14次测试通过),出现一个奇怪的错误:Error: expected file.path to export a function or instance
:
$ yarn pipeline
yarn run v1.3.2
$ yarn clear && yarn lint && yarn build && yarn test
$ yarn rimraf build dist
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/rimraf build dist
$ yarn lint:esnext
$ yarn standard src/*.js src/**/*.js
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/standard 'src/*.js' src/lib/generator.js src/tests/plugin.js src/tests/test.js
$ yarn build:lib && yarn build:tests && yarn build:docs
$ yarn babel src/lib/ --out-dir dist/lib --source-maps
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/babel src/lib/ --out-dir dist/lib --source-maps
src/lib/generator.js -> dist/lib/generator.js
$ yarn babel src/tests/ --out-dir dist/tests --source-maps
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/babel src/tests/ --out-dir dist/tests --source-maps
src/tests/plugin.js -> dist/tests/plugin.js
src/tests/test.js -> dist/tests/test.js
$ echo WARNING: No documentation build available yet
WARNING: No documentation build available yet
$ yarn mocha dist/tests/**/*.js
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/mocha 'dist/tests/**/*.js'
generate-swap-project
plugin
✓ should add tasks to the instance (80ms)
✓ should only register the plugin once
generate-swap-project
tasks
✓ should extend tasks onto the instance
✓ should run the `default` task with .build (186ms)
✓ should run the `default` task with .generate
swap-project (CLI)
X => 1) should run the default task using the `generate-swap-project` name (global install)
✓ should run the default task using the `swap-project` generator alias (local generator.js)
swap-project (API)
✓ should run the default task on the generator
✓ should run the `swap-project` task
✓ should run the `default` task when defined explicitly
sub-generator
✓ should work as a sub-generator
✓ should run the `default` task by default
✓ should run the `generator:default` task when defined explicitly
✓ should run the `generator:swap-project` task
✓ should work with nested sub-generators
✓ should run tasks as a sub-generator
15 passing (1s)
1 failing
1) generate-swap-project
swap-project (CLI)
should run the default task using the `generate-swap-project` name (global install):
Error: expected file.path to export a function or instance
at Env.get (node_modules/base-env/lib/file.js:256:11)
at Env.invoke (node_modules/base-env/lib/env.js:117:19)
at Generate.<anonymous> (node_modules/base-generators/lib/generator.js:62:30)
at Generate.getGenerator (node_modules/base-generators/index.js:157:22)
at Object.exports.process (node_modules/base-generators/lib/tasks.js:127:24)
at node_modules/base-generators/lib/tasks.js:86:23
at Array.reduce (<anonymous>)
at exports.parse (node_modules/base-generators/lib/tasks.js:85:15)
at Generate.generate (node_modules/base-generators/index.js:490:21)
at Generate.generate (node_modules/base-generators/index.js:463:23)
at Context.<anonymous> (dist/tests/test.js:87:11)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
这是travis配置
{
"sudo": false,
"language": "node_js",
"node_js": "lts/carbon",
"script": [
"npm link",
"npm install --global generate .",
"yarn travis"
],
"group": "stable",
"dist": "trusty",
"os": "linux"
}
这是:
我无法理解为什么那个测试(在CLI模式下运行生成器的测试)在ESNext转换和babel转换后失败。
我的公关出了什么问题?
答案 0 :(得分:1)
这个错误确实与巴贝尔翻译的后果有关。
根级目录中的文件generator.js
使用CommonJS
模块加载,这是全局generate
二进制文件直接调用所需的。
以下是./generator.js
的代码:
module.exports = require('./dist/lib/generator')
可是:
./dist/lib/generator.js
是./src/lib/generator.js
的babel转换的结果,它将生成器导出为默认的匿名函数,./generator.js
是CJS
模块并使用require()
然后所需的模块不是导出的生成器函数,而是包含属性default
的对象;并且该属性的值是生成器函数。
因此,实现./generator.js
的正确方法如下:
module.exports = require('./dist/lib/generator').default
这就是全部!