[RRFC] Add lifecycle hooks that can be mapped to user defined scripts (1:M) in package.json

Abierto
#892 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Tranquilo
Stack tecnológico
javascript
Área
cli, tooling

Línea de trabajo

Comienza con la sección hooks propuesta de package.json y con el issue 9384 de npm/cli referenciado en el RFC. Revisa cómo analiza actualmente npm los nombres de scripts y los hooks del ciclo de vida, y define después las reglas del delimitador y de resolución de pre/post. Se considerará terminado cuando la propuesta tenga una sintaxis acordada, un comportamiento de ejecución y un alcance adecuados para la implementación.

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

Descripción

Motivation ("The Why")

Scripts in package.json oftentimes rely on prior state or context in order to execute as expected. For example, injecting the right .env files to have respective apps not upload things such as test data into production.

Current conventions for most applications handle this in a few ways:

  1. Manually add an inline variable to the command such as NODE_ENV={whatever}, or for node 20+ projects use the --env-file switch. This gets cumbersome when you need to make 3+ copies of many regular scripts (eg. dev|start|build) and the commands themselves have enough other switches to worry about.
  2. Use external libraries like dotenv resolve this for them. Any addition of dependencies introduces the classic issues of new surface areas for vulnerabilities, as well as relying on the availability of respective maintainers to update them when contracts change, etc etc. Also, I still haven't found an external dependency that addresses this gap directly.
Example

Allow for an opt-in, zero-dependency solution that gives developers a cleaner, DRYer package.json. Example snippet:

"scripts": {
  "serve":  "node server.js",
  "build": "node scripts/build-client.js"
},
// act as hooks to run before script execution
"hooks": {
  "dev": "NODE_ENV=development && NODE_OPTIONS=--env-file=.env.development",
  "stg": "NODE_ENV=staging && NODE_OPTIONS=--env-file=.env.staging",
  // and so on...
}

and when running in the terminal, the environment keywords will then be parsed for as hooks, delimited by : for example, and then execute those commands "pre" lifecycle hook style.

npm run dev:serve
npm run stg:serve

This kind of functionality can be abstracted to other use cases where pre or post lifecycle hooks can be leveraged for any group of scripts that wants to be decorated by them.

How
Current Behaviour

User generated npm scripts cannot define any pre/before or post/after hooks that can be referenced by their key signature.

Desired Behaviour

User generated npm scripts CAN reference pre/before or post/after hooks that are defined in a new section in package.json, using an agreed upon delimiter such as colon (:). The placement of the hook key, either on the left (pre/before) or on the right (post/after) side of the script executes the hook script that the separated key maps to.

References
  • Originally opened as an issue in npm/cli
Lenguaje dominante
JavaScript
Estrellas
780
Forks
266
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

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.

Más de npm/rfcs

Todos los issues de npm/rfcs

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.