Skip to content

Commit

Permalink
fix: add hashbang if not exists in build (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito authored Apr 26, 2023
1 parent c88d558 commit 59d929a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@types/stylis": "^4.0.2",
"c8": "^7.12.0",
"eslint": "^8.26.0",
"magic-string": "^0.30.0",
"msw": "^0.47.4",
"pkgroll": "^1.4.0",
"prettier": "^2.7.1",
Expand All @@ -55,7 +56,7 @@
"vitest": "^0.24.3"
},
"scripts": {
"build": "pkgroll --target=node14",
"build": "pkgroll --target=node14 && tsx ./scripts/add-hashbang.ts",
"dev": "pkgroll --target=node14 --watch",
"cli": "node ./dist/cli.mjs",
"test": "vitest",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions scripts/add-hashbang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import MagicString from 'magic-string';
import * as fs from 'node:fs';
import { join } from 'pathe';

// Until pkgroll fixes the shebang bug, this needs to be run postbuild

const addHashbang = (filepath: string) => {
const path = join(process.cwd(), filepath);
const s = new MagicString(fs.readFileSync(path, 'utf8'));

// Only add the hashbang if it's not already there
if (!s.toString().startsWith('#!')) {
s.prepend('#!/usr/bin/env node\n');
fs.writeFileSync(path, s.toString());
}
};

addHashbang('dist/cli.mjs');
addHashbang('dist/cli.js');
69 changes: 35 additions & 34 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
{
/* Visit https://aka.ms/tsconfig.json to read more about this file */
"include": [
"src/**/*.ts",
"src/**/*.json",
"tests/**/*.ts",
"tests/**/*.json",
"vitest.config.ts"
],
"exclude": [".eslintrc.cjs"],
"compilerOptions": {
/* Projects */
//"incremental": true,
/* Visit https://aka.ms/tsconfig.json to read more about this file */
"include": [
"src/**/*.ts",
"src/**/*.json",
"scripts/**/*.ts",
"tests/**/*.ts",
"tests/**/*.json",
"vitest.config.ts"
],
"exclude": [".eslintrc.cjs"],
"compilerOptions": {
/* Projects */
//"incremental": true,

/* Language and Environment */
"target": "esnext",
"useDefineForClassFields": true,
/* Language and Environment */
"target": "esnext",
"useDefineForClassFields": true,

/* Modules */
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
/* Modules */
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,

/* Interop Constraints */
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
/* Interop Constraints */
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,

/* Type Checking */
"strict": true,
"isolatedModules": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
/* Type Checking */
"strict": true,
"isolatedModules": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,

/* Completeness */
"skipLibCheck": true // Skip type checking all .d.ts files.
},
"$schema": "https://json.schemastore.org/tsconfig"
/* Completeness */
"skipLibCheck": true // Skip type checking all .d.ts files.
},
"$schema": "https://json.schemastore.org/tsconfig"
}

0 comments on commit 59d929a

Please sign in to comment.