-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
import { promises as fs } from 'fs'; | ||
import { join } from 'path'; | ||
/** @format */ | ||
|
||
import { promises as fs } from "fs"; | ||
import { join } from "path"; | ||
|
||
// Path to the compiled JavaScript file in your dist directory | ||
const scriptPath = join(process.cwd(), 'dist/bin/script.js'); | ||
const scriptPath = join(process.cwd(), "dist/bin/script.js"); | ||
|
||
// The shebang line to add | ||
const shebang = '#!/usr/bin/env node\n'; | ||
const shebang = "#!/usr/bin/env node\n"; | ||
|
||
async function addShebang() { | ||
try { | ||
// Read the contents of the file | ||
let data = await fs.readFile(scriptPath, 'utf8'); | ||
|
||
// Check if the shebang is already there and replace any existing shebang | ||
const shebangRegex = /^#!.*\n/; | ||
if (shebangRegex.test(data)) { | ||
console.log('Found existing shebang, replacing it.'); | ||
data = data.replace(shebangRegex, ''); | ||
} | ||
|
||
// Prepend the new shebang to the file content | ||
const updatedContent = shebang + data; | ||
|
||
// Write the updated content back to the file | ||
await fs.writeFile(scriptPath, updatedContent, 'utf8'); | ||
console.log('Shebang added/replaced successfully!'); | ||
|
||
// Set the correct permissions to make the file executable | ||
await fs.chmod(scriptPath, 0o755); | ||
console.log('Execution permissions added successfully!'); | ||
} catch (err) { | ||
console.error('Error:', err); | ||
try { | ||
// Read the contents of the file | ||
let data = await fs.readFile(scriptPath, "utf8"); | ||
|
||
// Check if the shebang is already there and replace any existing shebang | ||
const shebangRegex = /^#!.*\n/; | ||
if (shebangRegex.test(data)) { | ||
console.log("Found existing shebang, replacing it."); | ||
data = data.replace(shebangRegex, ""); | ||
} | ||
|
||
const updatedContent = shebang + data; | ||
|
||
// Write the updated content back to the file | ||
await fs.writeFile(scriptPath, updatedContent, "utf8"); | ||
console.log("Shebang added/replaced successfully!"); | ||
|
||
// Set the correct permissions to make the file executable | ||
await fs.chmod(scriptPath, 0o755); | ||
console.log("Execution permissions added successfully!"); | ||
} catch (err) { | ||
console.error("Error:", err); | ||
} | ||
} | ||
|
||
addShebang(); |