* Get the command that triggered this postinstall script being run. If there is * an error while attempting to get this value then the string constant * 'ERROR_WHILE_FINDING_POSTINSTALL_TRIGGER' is returned. * This information is just necessary for telemetry. * This get's passed in to Ge
()
| 240 | * This get's passed in to Generate, which then automatically get's propagated to telemetry. |
| 241 | */ |
| 242 | function getPostInstallTrigger() { |
| 243 | /* |
| 244 | npm_config_argv` is not officially documented so here are our (Prisma's) research notes |
| 245 | `npm_config_argv` is available to the postinstall script when the containing package has been installed by npm into some project. |
| 246 | An example of its value: |
| 247 | ``` |
| 248 | npm_config_argv: '{"remain":["../test"],"cooked":["add","../test"],"original":["add","../test"]}', |
| 249 | ``` |
| 250 | We are interesting in the data contained in the "original" field. |
| 251 | Trivia/Note: `npm_config_argv` is not available when running e.g. `npm install` on the containing package itself (e.g. when working on it) |
| 252 | Yarn mimics this data and environment variable. Here is an example following `yarn add` for the same package: |
| 253 | ``` |
| 254 | npm_config_argv: '{"remain":[],"cooked":["add"],"original":["add","../test"]}' |
| 255 | ``` |
| 256 | Other package managers like `pnpm` have not been tested. |
| 257 | */ |
| 258 | |
| 259 | const maybe_npm_config_argv_string = process.env.npm_config_argv |
| 260 | |
| 261 | if (maybe_npm_config_argv_string === undefined) { |
| 262 | return UNABLE_TO_FIND_POSTINSTALL_TRIGGER__ENVAR_MISSING |
| 263 | } |
| 264 | |
| 265 | let npm_config_argv |
| 266 | try { |
| 267 | npm_config_argv = JSON.parse(maybe_npm_config_argv_string) |
| 268 | } catch (e) { |
| 269 | return `${UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_PARSE_ERROR}: ${maybe_npm_config_argv_string}` |
| 270 | } |
| 271 | |
| 272 | if (typeof npm_config_argv !== "object" || npm_config_argv === null) { |
| 273 | return `${UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR}: ${maybe_npm_config_argv_string}` |
| 274 | } |
| 275 | |
| 276 | const npm_config_arv_original_arr = npm_config_argv.original |
| 277 | |
| 278 | if (!Array.isArray(npm_config_arv_original_arr)) { |
| 279 | return `${UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR}: ${maybe_npm_config_argv_string}` |
| 280 | } |
| 281 | |
| 282 | const npm_config_arv_original = npm_config_arv_original_arr |
| 283 | .filter((arg) => arg !== "") |
| 284 | .join(" ") |
| 285 | |
| 286 | const command = |
| 287 | npm_config_arv_original === "" |
| 288 | ? getPackageManagerName() |
| 289 | : [getPackageManagerName(), npm_config_arv_original].join(" ") |
| 290 | |
| 291 | return command |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Wrap double quotes around the given string. |
no test coverage detected