Skip to content

Commit

Permalink
fix(bin): updates binary to install as-needed
Browse files Browse the repository at this point in the history
Updates the installation script to download the Zint repo only if
needed. Changes download from git to fetch for universal compliance.
Adds script for fresh binary rebuild.
  • Loading branch information
jshor committed Nov 27, 2022
1 parent 75fa8ee commit 86ba08c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 31 deletions.
8 changes: 8 additions & 0 deletions docs/docs/installation.md
Expand Up @@ -20,6 +20,14 @@ npm_config_build_from_source=true yarn add symbology
npm i symbology --build-from-source=true
```

### Building a fresh binary

The following command can be run to create a fresh build:

```sh
yarn install:rebuild
```

## Binary download failures

If you run into issues downloading the binary, the library will attempt to compile the module from source.
Expand Down
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -3,6 +3,7 @@
"version": "3.0.9",
"description": "🔖 Generates 1D, 2D, and composite barcodes in png, svg, or eps formats. Supports 50+ symbologies.",
"scripts": {
"ye": "node ./test.js",
"commit": "git-cz",
"release": "standard-version",
"lint": "eslint ./src",
Expand All @@ -13,6 +14,7 @@
"binary:install": "node-pre-gyp install --fallback-to-build",
"binary:rebuild": "node-pre-gyp rebuild package",
"install": "node ./scripts/install.js && npm run binary:install",
"install:rebuild": "rimraf bin && node ./scripts/install.js && npm run binary:rebuild",
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs"
},
Expand Down Expand Up @@ -52,9 +54,11 @@
"@mapbox/node-pre-gyp": "^1.0.10",
"git-clone": "^0.1.0",
"nan": "^2.14.2",
"node-fetch": "2",
"pngjs": "^6.0.0",
"replace-in-file": "^4.2.0",
"rimraf": "^3.0.0"
"rimraf": "^3.0.0",
"tar": "^6.1.12"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
Expand Down
75 changes: 52 additions & 23 deletions scripts/install.js
@@ -1,21 +1,23 @@
const clone = require('git-clone')
const replace = require('replace-in-file')
const rimraf = require('rimraf')
const fetch = require('node-fetch')
const tar = require('tar')
const path = require('path')
const fs = require('fs')
const rimraf = require('rimraf')
const patches = require('./patches')
const { zintVersion } = require('../package.json')

/** Current zint git ref (specified in package.json) */
const checkout = zintVersion || 'master'
const checkoutHash = zintVersion || 'master'
const file = 'zint.tgz'

/**
* Returns the absolute path to the given file in zint source.
*
* @param {string} p path names
* @returns {string}
*/
const getPath = p => path.join(__dirname, '../.zint', p)
const getPath = (p = '.') => path.join(__dirname, '../.zint', p)

/**
* Creates zintconfig.h, which contains version definitions from CMake.
Expand All @@ -31,31 +33,58 @@ const createConfigFile = () => {
fs.writeFileSync(getPath('backend/zintconfig.h'), fileData)
}

/**
* Clones zint, then applies C source patches.
*/
const cloneAndPatch = () => {
console.log('Removing any existing .zint directory...')
const downloadFile = (async (url, path) => {
const res = await fetch(url)
const fileStream = fs.createWriteStream(path)

await new Promise((resolve, reject) => {
res.body.pipe(fileStream)
res.body.on('error', reject)
fileStream.on('finish', resolve)
})
})

rimraf(path.join(__dirname, '../.zint'), () => {
console.log(`Cloning zint at '${checkout}' in`, path.join(__dirname, '../.zint'))
function applyPatches () {
console.log('Successfully cloned. Applying code patches...')

clone(`https://github.com/woo-j/zint.git`, './.zint', { checkout }, (err) => {
if (err) {
console.error(err)
} else {
console.log('Successfully cloned. Applying code patches...')
patches.forEach(patch => replace.sync(patch))

patches.forEach(patch => replace.sync(patch))
console.log('Creating zint config header file...')

console.log('Creating zint config header file...')
createConfigFile()
}

createConfigFile()
async function downloadAndExtract () {
console.log(`Downloading zint at '${checkoutHash}' to`, getPath())

console.log('Done.')
}
})
})
try {
await downloadFile(`https://github.com/zint/zint/tarball/${checkoutHash}`, getPath('../zint.tgz'))
await tar.extract({ file })

fs.renameSync(getPath(`../zint-zint-${checkoutHash}`), getPath())
fs.rmSync(file)

applyPatches()

console.log('Done.')
} catch (error) {
console.error(error)
}
}

/**
* Clones zint, then applies C source patches.
*/
const cloneAndPatch = () => {
if (fs.existsSync(path.join(__dirname, '../bin/binding/bin/symbology.node'))) {
// if the binary already exists, do not recompile
console.log('\x1b[32m ✓ Binary already exists. Nothing to install. \x1b[37m')
return
}

console.log('Removing any existing .zint directory...')

rimraf(path.join(__dirname, '../.zint'), downloadAndExtract)
}

cloneAndPatch()
Expand Down
26 changes: 19 additions & 7 deletions yarn.lock
Expand Up @@ -6145,20 +6145,20 @@ no-case@^3.0.4:
lower-case "^2.0.2"
tslib "^2.0.3"

node-fetch@2, node-fetch@^2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.6.1:
version "2.6.6"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.6.tgz#1751a7c01834e8e1697758732e9efb6eeadfaf89"
integrity sha512-Z8/6vRlTUChSdIgMa51jxQ4lrw/Jy5SOW10ObaA47/RElsAN2c5Pn8bTgFGWn/ibwzXTE8qwr1Yzx28vsecXEA==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

node-forge@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"
Expand Down Expand Up @@ -7879,6 +7879,18 @@ tar@^6.1.11:
mkdirp "^1.0.3"
yallist "^4.0.0"

tar@^6.1.12:
version "6.1.12"
resolved "https://registry.yarnpkg.com/tar/-/tar-6.1.12.tgz#3b742fb05669b55671fb769ab67a7791ea1a62e6"
integrity sha512-jU4TdemS31uABHd+Lt5WEYJuzn+TJTCBLljvIAHZOz6M9Os5pJ4dD+vRFLxPa/n3T0iEFzpi+0x1UfuDZYbRMw==
dependencies:
chownr "^2.0.0"
fs-minipass "^2.0.0"
minipass "^3.0.0"
minizlib "^2.1.1"
mkdirp "^1.0.3"
yallist "^4.0.0"

teeny-request@7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/teeny-request/-/teeny-request-7.1.1.tgz#2b0d156f4a8ad81de44303302ba8d7f1f05e20e6"
Expand Down

0 comments on commit 86ba08c

Please sign in to comment.