UNPKG

@augment-vir/node

Version:

A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.

79 lines (78 loc) 2.81 kB
/* node:coverage disable */ /** * This script is used to replace `node_modules/.bin` files so that they run with typescript. This * is used in dev for packages that are dependencies of the listed packages (in `packagesToFix`), * such as `augment-vir` itself. * * To use this script properly, call it directly in your postinstall script. Like this: * * ```json * { * "scripts": { * "postinstall": "tsx node_modules/@augment-vir/node/src/scripts/fix-ts-bin.script.ts" * } * } * ``` */ import { log } from '@augment-vir/common'; import { interpolationSafeWindowsPath, runShellCommand } from '@augment-vir/node'; import { readFile, rm, writeFile } from 'node:fs/promises'; import { join, sep } from 'node:path/posix'; const packagesToFix = [ { packageName: 'runstorm', binName: 'runstorm', scriptPath: join('runstorm', 'src', 'cli', 'cli.script.ts'), fixImport: true, }, { packageName: 'mono-vir', binName: 'mono-vir', scriptPath: join('mono-vir', 'src', 'cli', 'cli.script.ts'), fixImport: true, }, { packageName: 'virmator', binName: 'virmator', scriptPath: join('virmator', 'src', 'cli.script.ts'), fixImport: true, }, { packageName: 'prettier', binName: 'prettier', scriptPath: join('prettier', 'bin', 'prettier.cjs'), fixImport: false, }, { packageName: '@web/test-runner', binName: 'web-test-runner', scriptPath: join('@web/test-runner', 'dist', 'bin.js'), fixImport: false, }, ]; function createBinFileContents({ scriptPath }) { return [ '#!/bin/bash', '', `npx tsx "$(dirname "$(dirname "$(readlink -f "$0")")")${sep}${scriptPath}" "$@"`, ].join('\n'); } async function fixTsBin(packageToFix) { const binFilePath = join(process.cwd(), 'node_modules', '.bin', packageToFix.binName); await rm(binFilePath, { force: true }); await writeFile(binFilePath, createBinFileContents(packageToFix)); await runShellCommand(`chmod +x ${interpolationSafeWindowsPath(binFilePath)}`); await fixPackageJson(packageToFix); log.success(`Fixed ${packageToFix.packageName} bin.`); } async function fixPackageJson(packageToFix) { if (!packageToFix.fixImport) { return; } const packageJsonPath = join(process.cwd(), 'node_modules', packageToFix.packageName, 'package.json'); const original = String(await readFile(packageJsonPath)); await writeFile(packageJsonPath, original .replace('"main": "dist/index.js"', '"main": "src/index.ts"') .replace('"module": "dist/index.js"', '"module": "src/index.ts"')); } await Promise.all(packagesToFix.map(async (packageToFix) => await fixTsBin(packageToFix)));