nodejs / nodejs/node

Frontmatter for inline scripts

Abierto
#64,809 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature request
Lenguaje dominante
JavaScript
Estrellas
122k
Forks
37.3k
Merge medio
4 d 2 h
PR fusionados (30 d)
283

Descripción

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;

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

No se nombran archivos de implementación ni pruebas. Empieza comparando el formato propuesto con las referencias enlazadas de Node.js, Nix, Cargo y Python, y determina después cómo debe interactuar el frontmatter con las opciones explícitas, package.json, node.config.json y argv; «done» requiere acordar la sintaxis y el comportamiento antes de que pueda comenzar la implementación.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, node.js, typescript
Área
cli, developer-experience
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Tranquilo
Claridad
Necesita aclaración
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.