Skip to content

Commit

Permalink
Merge pull request #30 from victorsoares96/feat/fetch-binaries-in-pos…
Browse files Browse the repository at this point in the history
…tinstall

✨ feat: fetch binaries automatically in postinstall
  • Loading branch information
victorsoares96 committed May 24, 2023
2 parents 71a8c9f + 290a376 commit d863d0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
21 changes: 21 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const os = require('os');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const util = require('util');

const exec = util.promisify(childProcess.exec);

(async () => {
if (fs.existsSync(path.resolve(__dirname, '../dist'))) {
const fetchBinaries = require('./fetch-binaries');
const gsPath = path.resolve(__dirname, '../bin/gs');

await fetchBinaries.default(os.platform(), gsPath);

if (os.platform() === 'linux') {
console.log('setting permissions for binaries path');
await exec(`chmod +x ${path.resolve(gsPath, '10.01.1_linux/usr/local/bin/gs')}`);
}
}
})();
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "compress-pdf",
"version": "0.3.1",
"version": "0.3.2",
"templateVersion": "1.3.0",
"description": "An compress pdf library using ghostscript",
"main": "dist/index.js",
Expand All @@ -11,17 +11,19 @@
"types": "dist/index.d.ts",
"scripts": {
"prepare": "husky install",
"copy-json-files-to-dist": "copyfiles -u 1 src/**/*.json dist/",
"postinstall": "node install",
"copy-json-files-to-dist": "copyfiles src/**/*.json install.js dist",
"style:format": "prettier \"*.{js,json,yml,yaml,md}\" \"src/**/*\" --write",
"lint": "eslint --color --ext .ts \"src/**/*.+(ts)\"",
"lint:fix": "eslint --color --ext .ts \"src/**/*.+(ts)\" --fix",
"test": "vitest",
"test:coverage": "vitest run --coverage",
"type-check": "tsc --pretty --noEmit --skipLibCheck --esModuleInterop --resolveJsonModule",
"package": "npm run build && npm pack",
"package": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm pack && npm pkg set scripts.postinstall='node install'",
"build": "tsup",
"release": "npm run build && npm publish",
"release-local": "npm run build && npm link && echo [FINISH]: Run \"npm link compress-pdf\" to link the package to your project"
"release": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm publish && npm pkg set scripts.postinstall='node install'",
"release-beta": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm publish --tag beta && npm pkg set scripts.postinstall='node install'",
"release-local": "npm run build && npm pkg set scripts.postinstall='node dist/install' && npm link && npm pkg set scripts.postinstall='node install' && echo [FINISH]: Run \"npm link compress-pdf\" to link the package to your project"
},
"publishConfig": {
"access": "public"
Expand All @@ -45,7 +47,6 @@
"url": "https://github.com/victorsoares96/compress-pdf/issues"
},
"homepage": "https://github.com/victorsoares96/compress-pdf#readme",
"peerDependencies": {},
"dependencies": {
"lodash": "4.17.21",
"request": "2.88.2",
Expand Down
6 changes: 3 additions & 3 deletions src/fetch-binaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function downloadFile(
const output = path.resolve(destination, filename);

if (fs.existsSync(output)) {
throw new Error(`${filename} already exists in destination`);
return output;
}

const tmpPath = path.resolve(os.tmpdir(), filename);
Expand All @@ -51,11 +51,11 @@ async function downloadFile(

progress += Buffer.byteLength(data);

process.stdout.write(
/* process.stdout.write(
`\rDownloading ${filename} in ${output}, ${Math.floor(
(progress / size) * 100
)}%`
);
); */
})
.on('close', async () => {
fs.renameSync(tmpPath, output);
Expand Down

0 comments on commit d863d0b

Please sign in to comment.