Skip to content

Commit

Permalink
fix: Retarget symlink only on non-windows, rerun exe otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Oct 27, 2023
1 parent 9541d57 commit 3fd5d90
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

* Create symlink properly when installed globally ([60f7861](https://github.com/prantlf/node-newchanges/commit/60f786189f43428292e33cdb2ad746091c11b00a))

# Changes

## 2023-10-26 (0.0.1)

Initial release
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
[![Latest version](https://img.shields.io/npm/v/newchanges)
![Dependency status](https://img.shields.io/librariesio/release/npm/newchanges)
](https://www.npmjs.com/package/newchanges)
[![Coverage](https://codecov.io/gh/prantlf/node-newchanges/branch/master/graph/badge.svg)](https://codecov.io/gh/prantlf/node-newchanges)

Creates or updates the changelog file from commit messages formatted according to [Conventional Commits]. Installs [newchanges] in [Node.js] environments.

## Installation

This package is usually installed globally, so that you can use the `newchanges` executable:
This package is usually installed globally, so that you can use the `newchanges` executable from any directory. You can install it during the first usage with `npx` too:

```sh
$ npm i -g newchanges
$ npx newchanges ...
```

Make sure, that you use [Node.js] version 18 or newer.
Expand Down
75 changes: 62 additions & 13 deletions bin/newchanges.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node

import { spawn } from 'child_process'
import debug from 'debug'
import { access, symlink, unlink } from 'fs/promises'
import { access, lstat, symlink, unlink } from 'fs/promises'
import { platform } from 'os'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'
Expand All @@ -11,7 +12,26 @@ const __dirname = dirname(__filename)
const exists = file => access(file).then(() => true, () => false)
const log = debug('newchanges')

try {
async function findExe(name) {
const exe = join(__dirname, '..', platform() != 'win32' ? name : `${name}.exe`)
if (!await exists(exe)) {
log('exe "%s"', exe)
throw new Error('missing executable')
}
return exe
}

function runExe(exe) {
const [,, ...args] = process.argv
log('run "%s" with %d args', exe, args.length)
return new Promise((resolve, reject) =>
spawn(exe, args, { stdio: 'inherit' })
.on('error', reject)
.on('exit', code => code ? reject(code) : resolve())
)
}

async function findBin() {
// installed locally
let bin = join(__dirname, '..', '..', '.bin')
log('local bin "%s"', bin)
Expand All @@ -26,17 +46,46 @@ try {
}
if (!await exists(bin)) throw new Error('cannot find bin directory')
}
return bin
}

const exe = join(__dirname, '..', platform() != 'win32' ? 'newchanges' : `newchanges.exe`)
log('exe "%s"', exe)
if (!await exists(exe)) throw new Error('missing executable')
const link = join(bin, 'newchanges')
log('unlink "%s"', link)
await unlink(link)
log('link "%s"', link)
await symlink(exe, link, 'junction')
console.log('installation finished, re-run the same command')
async function replaceLink(bin, name, exe) {
const link = join(bin, name)
log('stat "%s"', link)
const { mode } = await lstat(link)
if (mode & 0o222) {
log('unlink "%s"', link)
await unlink(link)
log('link "%s"', link)
await symlink(exe, link, 'junction')
return true
} else {
log('not writable')
}
}

async function runAndReplaceLink(name) {
const exe = await findExe(name)
if (platform() != 'win32') {
const bin = await findBin()
await replaceLink(bin, name, exe)
}
await runExe(exe)
}

function reportError(err) {
let code
if (typeof err !== 'number') {
console.error(err)
code = 1
} else {
code = err
}
process.exitCode = code
}

try {
await runAndReplaceLink('newchanges')
} catch (err) {
console.error(err)
process.exitCode = 1
reportError(err)
}

0 comments on commit 3fd5d90

Please sign in to comment.