Skip to content

Commit

Permalink
Adjust install script
Browse files Browse the repository at this point in the history
  • Loading branch information
raub committed Nov 1, 2024
1 parent b00510e commit 3a2a516
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 68 deletions.
7 changes: 5 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ declare module "addon-tools-raub" {
export const getLoggers: () => Readonly<Record<string, TGlobalLogger>>

/**
* Get a logger by name, returns null if not found
* Get a logger by name.
*
* If there is no logger by that name,
* it will create a new one with default parameters.
*/
export const getLogger: () => (TGlobalLogger | null)
export const getLogger: () => (TGlobalLogger)
}
123 changes: 70 additions & 53 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Luis Blanco <[email protected]>",
"name": "addon-tools-raub",
"version": "9.0.0",
"version": "9.1.0",
"description": "Helpers for Node.js addons and dependency packages",
"license": "MIT",
"main": "index.js",
Expand Down
33 changes: 22 additions & 11 deletions utils/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const exec = util.promisify(require('node:child_process').exec);

const { getBin, getPlatform } = require('../include');
const { mkdir, rmdir, rm, exists } = require('./files');
const { getLogger } = require('./logger');

const logger = getLogger('addon-tools');

const download = async (url) => {
const { stderr } = await exec([
Expand All @@ -14,7 +16,15 @@ const download = async (url) => {
url,
].join(' '));
if (stderr) {
console.warn(stderr);
logger.warn(stderr);
}
};


const unpack = async (gzPath, binPath) => {
const { stderr } = await exec(`tar -xzf ${gzPath} --directory ${binPath}`);
if (stderr) {
logger.warn(stderr);
}
};

Expand All @@ -27,18 +37,19 @@ const install = async (folderUrl) => {
await rmdir(binPath);
await mkdir(binPath);

await download(urlPath);

if (!(await exists(gzPath))) {
console.warn(`Could not download "${urlPath}" to "${gzPath}"`);
try {
await download(urlPath);

if (!(await exists(gzPath))) {
logger.warn(`Could not download "${urlPath}" to "${gzPath}"`);
return false;
}

await unpack(gzPath, binPath);
} catch (error) {
logger.warn(error);
return false;
}

const { stderr } = await exec(`tar -xzf ${gzPath} --directory ${binPath}`);
if (stderr) {
console.warn(stderr);
}

await rm(gzPath);
return true;
};
Expand Down
4 changes: 3 additions & 1 deletion utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const getLevel = () => currentLevel;

const getLoggers = () => ({ ...loggers });

const getLogger = (name) => loggers[name] || null;
const getLogger = (name) => loggers[name] || createLogger({ name });

if (!global.AddonTools.log) {
global.AddonTools.log = (name, level, ...args) => {
Expand All @@ -69,6 +69,8 @@ if (!global.AddonTools.log) {
};
}

createLogger({ name: 'addon-tools' });

module.exports = {
createLogger, setLevel, getLevel, getLoggers, getLogger,
};

0 comments on commit 3a2a516

Please sign in to comment.