1#! /usr/bin/env tsx
2/* eslint-disable node/no-process-env */
3import { v2 as compose } from 'docker-compose' 4import { config as loadEnv } from 'dotenv' 5import { expand as dotenvExpand } from 'dotenv-expand' 6import yargs from 'yargs' 7import { hideBin } from 'yargs/helpers' 8 9import fs from 'fs'10import path from 'path'1112dotenvExpand(loadEnv({ path: path.resolve(__dirname, '../.env') }))13const dockerComposeFile = path.resolve(__dirname, './docker-compose.yml')1415if (!fs.existsSync(dockerComposeFile)) {16 throw new Error(`Could not find a docker compose file at: ${dockerComposeFile}`)17}1819const postgresLocalUrlRegex = /^postgres:\/\/(?:([^:]+):([^@]+)@)?(?:localhost|127\.0\.0\.1)(:\d+)?\/([^?]+)/2021yargs(hideBin(process.argv))22 .demandCommand(1)23 .command(24 'up',25 'Start docker containers',26 () => {},27 async () => {28 if (process.env.DATABASE_URL && postgresLocalUrlRegex.test(process.env.DATABASE_URL)) {29 console.log("Starting docker...")30 await compose.upAll({31 config: dockerComposeFile,32 callback: (chunk) => console.log(chunk.toString()),33 })34 } else {35 console.log("Skipping docker start, DATABASE_URL is not set to a local address")36 }37 }38 )39 .command(40 'down',41 'Stop docker containers',42 () => {},43 async () => {44 try {45 await compose.ps({ config: dockerComposeFile })4647 await compose.downAll({48 config: dockerComposeFile,49 callback: (chunk) => console.log(chunk.toString()),50 })51 } catch (err) {52 if (err instanceof Error) {53 console.error(err.message)54 }55 }56 }57 ).argv
The issue checks for incorrect or missing shebang in files mentioned in the bin
field of the package.json
.
When creating a CLI tool with Node.js, it is necessary to add a shebang to the file that serves as the entry point for the application.
As the NPM docs say, the file(s) referenced in bin
must start with #!/usr/bin/env node
, or the scripts won't be run with the Node.js executable.
The bin
field in package.json is used to specify JS files which run as tools when launched with the Node.js launcher.
Incorrect shebang:
// /user/bin/env node
console.log("App launched");
No shebang in a file referenced in package.json's bin
field:
console.log("App launched")
The following code snippet is assumed to be in a file that is referenced in the bin
field:
#!/usr/bin/env node
console.log("App launched");