nodejs / nodejs/node

Frontmatter for inline scripts

未关闭
#64,809 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

feature request
主要语言
JavaScript
星标
122k
派生
37.3k
平均合并
4 天 2 小时
30 天内合并 PR
283

描述

What is the problem this feature will solve?

Taking inspiration from nix, cargo, and python I would like a way for stand-alone scripts to embed configuration normally passed via package.json, node.config.json, argv, or wrapper scripts.

https://github.com/nodejs/node/issues/59565 asks to support typescript-module in extensionless files but proposals for #!/usr/bin/env node --ext mts were rejected because many systems don't support multi-word shebangs.

I don't think this limitation is exclusive to that --ext use cases, there are other options including --permission that cannot be set in a self contained script, yes you can use .env, node.config.json, or package.json but that's no-longer self contained.

This would also be an interesting integration point for package managers that could read dependencies from the frontmatter and support npm x ./script to install the script's inline dependencies.

https://nixos.wiki/wiki/Nix-shell_shebang
https://rust-lang.github.io/rfcs/3424-cargo-script.html
https://packaging.python.org/en/latest/specifications/inline-script-metadata/

What is the feature you are proposing to solve the problem?

The rough outline is for node to parse a leading comment in the main entry point and read command line arguments,
node.config, and package.json fields. The comment would only be read in the main entry point and would sit after explicit options in priority.

#!/usr/bin/env node
// ```node.config.json
// {
//    "nodeOptions": { "ext": "mts" },
//    "permission": {},
//  }
// ```
await main(process.argv.slice(2))

async function main(args: string[]) {
  console.log(args)
}
What alternatives have you considered?

Today I'll create directories with package.jsonuse .mts file extensions, or use nasty tricks to combine shell and ts into a single script

#!/bin/sh
':' // ; exec node --ext mts
// script goes here
let n: number = 1;

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

没有列出实现文件或测试。首先将提议的格式与链接的 Node.js、Nix、Cargo 和 Python 参考资料进行比较,然后确定 frontmatter 应如何与显式选项、package.json、node.config.json 和 argv 交互;在开始实现之前,必须就语法和行为达成一致,才能标记为「done」。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js, typescript
领域
cli, developer-experience
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
需要澄清
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。