Skip to content

[FIX] WIndows binary #671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
37 changes: 24 additions & 13 deletions scripts/install-binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const path = require('path')
const tempfile = require('tempfile')
const fs = require('fs')
const { DownloaderHelper } = require('node-downloader-helper')
const { execSync } = require('child_process')

async function download (url, saveDirectory) {
const downloader = new DownloaderHelper(url, saveDirectory)
Expand All @@ -16,9 +17,9 @@ async function download (url, saveDirectory) {
downloader.on('error', (err) => reject(err))
downloader.on('progress.throttled', (downloadEvents) => {
const percentageComplete =
downloadEvents.progress < 100
? downloadEvents.progress.toPrecision(2)
: 100
downloadEvents.progress < 100
? downloadEvents.progress.toPrecision(2)
: 100
console.info(`Downloaded: ${percentageComplete}%`)
})

Expand All @@ -40,9 +41,18 @@ const JQ_NAME_MAP = {
win32: 'jq.exe'
}
const JQ_NAME =
platform in JQ_NAME_MAP ? JQ_NAME_MAP[platform] : JQ_NAME_MAP.def
platform in JQ_NAME_MAP ? JQ_NAME_MAP[platform] : JQ_NAME_MAP.def

const PACKAGE_FOLDER = path.join(__dirname, '..')
const OUTPUT_DIR = path.join(__dirname, '..', 'bin')
const OUTPUT_FILE = path.join(OUTPUT_DIR, JQ_NAME)

const makeBinaryWorkInWindows = () => {
// Run "npm link" to make sure the binary files are symlinked
execSync('npm link . --ignore-scripts --audit false --fund false', {
cwd: PACKAGE_FOLDER
})
}

const fileExist = (path) => {
try {
Expand All @@ -57,7 +67,7 @@ if (!fs.existsSync(OUTPUT_DIR)) {
console.info(`${OUTPUT_DIR} directory was created`)
}

if (fileExist(path.join(OUTPUT_DIR, JQ_NAME))) {
if (fileExist(OUTPUT_FILE)) {
console.log('jq is already installed')
process.exit(0)
}
Expand Down Expand Up @@ -93,14 +103,14 @@ if (platform in DOWNLOAD_MAP && arch in DOWNLOAD_MAP[platform]) {
console.log(`Downloading jq from ${url}`)
download(url, OUTPUT_DIR)
.then(() => {
const distPath = path.join(OUTPUT_DIR, JQ_NAME)
fs.renameSync(path.join(OUTPUT_DIR, filename), distPath)
if (fileExist(distPath)) {
// fs.chmodSync(distPath, fs.constants.S_IXUSR || 0o100)
fs.renameSync(path.join(OUTPUT_DIR, filename), OUTPUT_FILE)
if (fileExist(OUTPUT_FILE)) {
// fs.chmodSync(OUTPUT_FILE, fs.constants.S_IXUSR || 0o100)
// Huan(202111): we need the read permission so that the build system can pack the node_modules/ folder,
// i.e. build with Heroku CI/CD, docker build, etc.
fs.chmodSync(distPath, 0o755)
fs.chmodSync(OUTPUT_FILE, 0o755)
}
makeBinaryWorkInWindows()
console.log(`Downloaded in ${OUTPUT_DIR}`)
})
.catch(err => {
Expand All @@ -115,11 +125,12 @@ if (platform in DOWNLOAD_MAP && arch in DOWNLOAD_MAP[platform]) {
console.log(`Building jq from ${url}`)
binBuild
.url(url, [
`./configure --with-oniguruma=builtin --prefix=${tempfile()} --bindir=${OUTPUT_DIR}`,
'make -j8',
'make install'
`./configure --with-oniguruma=builtin --prefix=${tempfile()} --bindir=${OUTPUT_DIR}`,
'make -j8',
'make install'
])
.then(() => {
makeBinaryWorkInWindows()
console.log(`jq installed successfully on ${OUTPUT_DIR}`)
})
.catch(err => {
Expand Down
4 changes: 2 additions & 2 deletions src/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('options', () => {
run('.', null, { input: 'file' })
.catch(error => {
expect(error).to.be.an.instanceof(Error)
expect(error.message).to.equal(`${INVALID_PATH_ERROR}: "null"`)
expect(error.message).to.equal(`${INVALID_PATH_ERROR}: "undefined"`)
done()
})
.catch(error => {
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('options', () => {
run('.', null, { input: 'string' })
.catch(error => {
expect(error).to.be.an.instanceof(Error)
expect(error.message).to.equal(`${INPUT_STRING_ERROR}: "null"`)
expect(error.message).to.equal(`${INPUT_STRING_ERROR}: "undefined"`)
done()
})
.catch(error => {
Expand Down