Skip to content

Commit

Permalink
fix: Perform the installation steps using link-bin-executable
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Oct 27, 2023
1 parent 349725e commit a14c31b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 122 deletions.
86 changes: 4 additions & 82 deletions bin/newchanges.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,13 @@
#!/usr/bin/env node

import { spawn } from 'child_process'
import debug from 'debug'
import { access, lstat, symlink, unlink } from 'fs/promises'
import { platform } from 'os'
import { dirname, join } from 'path'
import { runAndReplaceLink, reportError } from 'link-bin-executable'
import { dirname } from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const exists = file => access(file).then(() => true, () => false)
const log = debug('newchanges')

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)
// installed globally
if (!await exists(bin)) {
bin = join(__dirname, '..', '..', '..', '..', 'bin')
log('global bin "%s"', bin)
// installed dependencies of this package
if (!await exists(bin)) {
bin = join(__dirname, '..', 'node_modules', '.bin')
log('package bin "%s"', bin)
}
if (!await exists(bin)) throw new Error('cannot find bin directory')
}
return bin
}

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
}
const __dirname = dirname(fileURLToPath(import.meta.url))

try {
await runAndReplaceLink('newchanges')
await runAndReplaceLink({ name: 'newchanges', scriptDirectory: __dirname })
} catch (err) {
reportError(err)
}
38 changes: 7 additions & 31 deletions install.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import debug from 'debug'
import { access, symlink, unlink } from 'fs/promises'
import { basename, dirname, join } from 'path'
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import grab from 'grab-github-release'
import { installLink } from 'link-bin-executable'

const __dirname = dirname(fileURLToPath(import.meta.url))
const exists = file => access(file).then(() => true, () => false)
const log = debug('newchanges')

const name = 'newchanges'
const repository = 'prantlf/v-newchanges'
const platformSuffixes = {
linux: 'linux',
Expand All @@ -16,34 +14,12 @@ const platformSuffixes = {
}

try {
if (!process.env.INIT_CWD) throw new Error('not running during npm install')

// installed locally
let bin = join(__dirname, '..', 'node_modules', '.bin')
log('local bin "%s"', bin)
// installed globally
if (!await exists(bin)) {
bin = join(__dirname, '..', '..', 'bin')
log('global bin "%s"', bin)
// installed dependencies of this package
if (!await exists(bin)) {
bin = join(__dirname, 'node_modules', '.bin')
log('package bin "%s"', bin)
}
if (!await exists(bin)) throw new Error('cannot find bin directory')
}

const { executable, version } = await grab(
{ repository, platformSuffixes, targetDirectory: __dirname, unpackExecutable: true })
const { executable, version } = await grab({
repository, platformSuffixes, targetDirectory: __dirname, unpackExecutable: true
})
console.log('downloaded and unpacked "%s" version %s', executable, version)

const link = join(bin, basename(executable))
if (await exists(link)) {
log('unlink "%s"', link)
await unlink(link)
}
log('link "%s"', link)
await symlink(executable, link, 'junction')
await installLink({ name, packageDirectory: __dirname })
} catch (err) {
console.error(err)
process.exitCode = 1
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
]
},
"dependencies": {
"debug": "^4.3.4",
"grab-github-release": "^0.1.0"
"grab-github-release": "^0.1.1",
"link-bin-executable": "^0.0.1"
},
"devDependencies": {
"@semantic-release/changelog": "6.0.3",
Expand Down
23 changes: 16 additions & 7 deletions pnpm-lock.yaml

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

0 comments on commit a14c31b

Please sign in to comment.