Rome 命令
init
init
命令帮你针对 rome
初始化并设置你的项目。在你的项目根目录运行如下命令:
$ rome init
接下来,rome
将询问你是要使用默认设置还是进一步自定义。你可以用箭头键在 yes
和 no
之间切换。按 enter
键确认选择。
Welcome to Rome!
❯ Use recommended settings?
ℹ Use arrow keys and then enter to select an option
◉ Yes
◯ No
默认配置
如果你选择 yes
,rome
将创建一份默认配置文件 rome.json
,文件内容如下:
{
"version": "^0.0.52",
"lint": {
"enabled": true
}
}
自定义设置
如果你选择 no
并进一步自定义项目,Rome 将引导你完成一组问题。首先你可以自定义项目名称。输入项目名称并按 enter
键确认。
Welcome to Rome!
ℹ Press space to select an option and enter to confirm
❯ Use recommended settings?: No
? Project name:
在这之后,你可以选择是否启用代码检测、格式化和/或测试。你可以使用箭头键在选项间移动。利用 space
键可以选中或取消选中。
Welcome to Rome!
❯ Use recommended settings?: No
? Project name: hello-world
❯ Features enabled
ℹ Use arrow keys and space to select or deselect options and then enter to confirm
☑ Lint
☐ Format
所有选项都选完之后将生成如下配置文件:
{
"name": "hello-world",
"version": "^0.0.52",
"lint": {
"enabled": true
},
"format": {
"enabled": true
}
}
我已经有一个配置文件了
如果你的项目中已经包含了一个配置文件,则 init
命令将推出并报错。
✖ rome.json file already exists
ℹ Use `rome config` to update an existing config
这时可以使用 config
命令来更新配置,而不是 init
命令。
lint
The lint
命令用于检查项目中的文件是否存在编码问题,例如未使用的变量。如果没有指定参数,则检查整个项目。
rome lint [files]
只检查某些文件
如果你只想检查某些文件,你可以指定要检查的文件:
$ rome lint index.js hello.js
检查结果说明
如果 rome
没有发现任何问题,你将得到如下结果:
ℹ 1 file linted
✔ No known problems!
但是,如果出现不正常的情况,rome
将以以下格式向你展示错误消息:
foobar.js:1 parse/js ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ import and export can only appear in a module
> 1 │ import { join } from "path";
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ℹ Change the extension to .mjs to turn this file into a module
ℹ Add "type": "module" to your package.json
这些错误消息包含以下部分:
<the affected file>:<line>:<column> <the linter rule that was violated>
-----------------------------------
x description of what is not ok
> 1 | the line of code that is problematic
| ^^^ <the part of the line that is not ok>
ℹ helpful message(s) of what you can do to fix the problem
每个问题都会展示一条消息。
示例
以下代码有两个未使用的变量:第一行中的 join
和第三行中的 unused
。
const {join} = require('path');
const unused = 'I am not used :)';
console.log('hello world!');
上述代码的检查结果如下:
index.js:1:8 lint/unusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unused variable join
> 1 │ const { join } = require("path");
│ ^^^^
2 │
3 │ const unused = "I am not used :)";
index.js:3:6 lint/unusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unused variable unused
1 │ const { join } = require("path");
2 │
> 3 │ const unused = "I am not used :)";
│ ^^^^^^
4 │
5 │ console.log("hello world!");
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ 1 file linted
✖ Found 2 problems