cli API
包 Luna-Flow/mooncake-impact-factor/cli 是面向 JavaScript 目标的可执行程序。它从 JSON 文件读取分数快照的八个信号,调用 compute_score_snapshot,并以 JSON 输出结果。它的存在是为了让 MoonBit 之外的程序(首先是 Python 索引构建器)直接使用 MoonBit 的评分规则,而不是复制一份。
源码:src/cli/main.mbt。它的接口文件为空:该包不导出任何 MoonBit 名称,其接口就是本页描述的命令行。
构建
该包声明了 supported_targets = "js" 和 pkgtype(kind: "executable")。在仓库根目录下构建:
moon build src/cli --target js
索引构建器期望构建结果位于 _build/js/debug/build/cli/cli.js,文件不存在时会自动构建。该脚本需要支持 process.getBuiltinModule 的 Node.js 版本(20.16、22.3 或更高)。
score-snapshot
score-snapshot 根据一个 JSON 输入文件计算一个分数快照。
node cli.js score-snapshot --input <path>
脚本之后的第一个参数必须是 score-snapshot。--input 可以出现在其后的任意位置;以第一个后面跟有值的 --input 为准,其他参数会被忽略。
输入
文件必须包含一个 JSON 对象。每个键都是可选的:
| 键 | 信号 |
|---|---|
dependents | 当前的依赖方数量。 |
recent_dependents | 当前的近期依赖方数量。 |
downloads | 当前的下载量。 |
days_since_release | 当前距最新发布的天数。 |
historical_dependents | 30 天前的依赖方数量。 |
historical_recent_dependents | 30 天前的近期依赖方数量。 |
historical_downloads | 30 天前的下载量。 |
historical_days_since_release | 30 天前距最新发布的天数。 |
值为 JSON 数字时才会被采用:它通过向零截断转换为 Int(12.9 变为 12),超出 Int 范围的值会饱和到其边界。缺失的键、其他任何 JSON 类型的值(包括 "20" 这样的数字字符串)以及顶层不是对象的值都按 0 处理。随后评分函数会把负值截为 0。未知的键会被忽略。
{
"dependents": 20,
"recent_dependents": 4,
"downloads": 300,
"days_since_release": 40,
"historical_dependents": 10,
"historical_recent_dependents": 2,
"historical_downloads": 0,
"historical_days_since_release": 10
}
输出
成功时,命令输出一行内容,即 ScoreSnapshot 的 JSON 形式,并以状态 0 退出。字段按声明顺序排列。值为 NaN 的字段(只会由 2147483647 或更大的计数产生)会写成字符串 "NaN"。对于上面的输入,输出为:
{"score":301.7852882193072,"score_30d_ago":135.2764584196223,"score_growth_30d":166.5088297996849,"score_growth_ratio_30d":1.2308780976744749,"rank_label":"S","momentum_label":"Rising","activity_multiplier":1.06}
错误
用法错误和解析错误会向标准输出打印一个带 error 键的 JSON 对象,并以状态 1 退出:
| 情形 | 输出 |
|---|---|
没有参数,或第一个参数不是 score-snapshot | {"error":"Usage: score-snapshot --input <path>"} |
没有后面跟有值的 --input | {"error":"Missing --input <path>"} |
| 文件不是合法的 JSON | {"error":"Failed to parse JSON input"} |
无法读取的文件不会以这种方式报告:Node.js 会抛出未捕获的 ENOENT(或类似)错误,向标准错误打印堆栈跟踪,并以状态 1 退出。调用方应把任何非零状态视为失败,并且只在状态为 0 时解析标准输出。