@iobroker/js-controller-common 4.0.5 → 4.0.6-alpha.0-20220209-3a6580b2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/common/tools.js +55 -47
  2. package/package.json +2 -2
@@ -10,7 +10,7 @@ const cpPromise = require('promisify-child-process');
10
10
  const jwt = require('jsonwebtoken');
11
11
  const { createInterface } = require('readline');
12
12
  const { PassThrough } = require('stream');
13
- const { detectPackageManager } = require('@alcalzone/pak');
13
+ const { detectPackageManager, packageManagers } = require('@alcalzone/pak');
14
14
  const EXIT_CODES = require('./exitCodes');
15
15
  const zlib = require('zlib');
16
16
 
@@ -1258,34 +1258,32 @@ function getRepositoryFile(urlOrPath, additionalInfo, callback) {
1258
1258
  */
1259
1259
  async function getRepositoryFileAsync(url, hash, force, _actualRepo) {
1260
1260
  let _hash;
1261
- if (_actualRepo && !force && hash && (url.startsWith('http://') || url.startsWith('https://'))) {
1262
- axios = axios || require('axios');
1263
- _hash = await axios({ url: url.replace(/\.json$/, '-hash.json'), timeout: 10000 });
1264
- if (_hash && _hash.data && hash === _hash.data.hash) {
1265
- return _actualRepo;
1266
- }
1267
- }
1268
-
1269
1261
  let data;
1270
1262
 
1271
1263
  if (url.startsWith('http://') || url.startsWith('https://')) {
1272
1264
  axios = axios || require('axios');
1273
- if (!_hash) {
1265
+ try {
1274
1266
  _hash = await axios({ url: url.replace(/\.json$/, '-hash.json'), timeout: 10000 });
1267
+ } catch {
1268
+ // ignore missing hash file
1275
1269
  }
1276
1270
 
1277
- if (_actualRepo && hash && _hash && _hash.data && _hash.data.hash === hash) {
1271
+ if (_actualRepo && !force && hash && _hash && _hash.data && _hash.data.hash === hash) {
1278
1272
  data = _actualRepo;
1279
1273
  } else {
1280
1274
  const agent = `${module.exports.appName}, RND: ${randomID}, Node:${process.version}, V:${
1281
1275
  require('../../package.json').version
1282
1276
  }`;
1283
- data = await axios({
1284
- url,
1285
- timeout: 10000,
1286
- headers: { 'User-Agent': agent }
1287
- });
1288
- data = data.data;
1277
+ try {
1278
+ data = await axios({
1279
+ url,
1280
+ timeout: 10000,
1281
+ headers: { 'User-Agent': agent }
1282
+ });
1283
+ data = data.data;
1284
+ } catch (e) {
1285
+ throw new Error(`Cannot download repository file from "${url}": ${e.message}`);
1286
+ }
1289
1287
  }
1290
1288
  } else {
1291
1289
  if (fs.existsSync(url)) {
@@ -1456,6 +1454,43 @@ function getSystemNpmVersion(callback) {
1456
1454
 
1457
1455
  const getSystemNpmVersionAsync = promisify(getSystemNpmVersion);
1458
1456
 
1457
+ /**
1458
+ * @private
1459
+ * Figure out which package manager is in charge, but with a fallback to npm.
1460
+ * @param {string} [cwd] Which directory to work in. If none is given, this defaults to ioBroker's root directory.
1461
+ * @returns {Promise<PackageManager>}
1462
+ */
1463
+ async function detectPackageManagerWithFallback(cwd) {
1464
+ try {
1465
+ // For the first attempt, use pak's default of requiring a lockfile. This makes sure we find ioBroker's root dir
1466
+ return await detectPackageManager(
1467
+ typeof cwd === 'string'
1468
+ ? // If a cwd was provided, use it
1469
+ { cwd }
1470
+ : // Otherwise try to find the ioBroker root dir
1471
+ {
1472
+ cwd: __dirname,
1473
+ setCwdToPackageRoot: true
1474
+ }
1475
+ );
1476
+ } catch {
1477
+ // Lockfile not found, try again without requiring a lockfile
1478
+ }
1479
+
1480
+ // Since we have no lockfile to rely on, assume the root dir is 2 levels above js-controller
1481
+ const ioBrokerRootDir = path.join(getControllerDir(), '../..');
1482
+ try {
1483
+ return await detectPackageManager({ cwd: ioBrokerRootDir, requireLockfile: false });
1484
+ } catch {
1485
+ // Lockfile not found, use default
1486
+ }
1487
+
1488
+ // Fallback to npm
1489
+ const pak = new packageManagers.npm();
1490
+ pak.cwd = ioBrokerRootDir;
1491
+ return pak;
1492
+ }
1493
+
1459
1494
  /**
1460
1495
  * @typedef {object} InstallNodeModuleOptions
1461
1496
  * @property {boolean} [unsafePerm] Whether the `--unsafe-perm` flag should be used
@@ -1471,16 +1506,7 @@ const getSystemNpmVersionAsync = promisify(getSystemNpmVersion);
1471
1506
  */
1472
1507
  async function installNodeModule(npmUrl, options = {}) {
1473
1508
  // Figure out which package manager is in charge (probably npm at this point)
1474
- const pak = await detectPackageManager(
1475
- typeof options.cwd === 'string'
1476
- ? // If a cwd was provided, use it
1477
- { cwd: options.cwd }
1478
- : // Otherwise find the ioBroker root dir
1479
- {
1480
- cwd: __dirname,
1481
- setCwdToPackageRoot: true
1482
- }
1483
- );
1509
+ const pak = await detectPackageManagerWithFallback(options.cwd);
1484
1510
  // By default, don't print all the stuff the package manager spits out
1485
1511
  if (!options.debug) {
1486
1512
  pak.loglevel = 'error';
@@ -1520,16 +1546,7 @@ async function installNodeModule(npmUrl, options = {}) {
1520
1546
  */
1521
1547
  async function uninstallNodeModule(packageName, options = {}) {
1522
1548
  // Figure out which package manager is in charge (probably npm at this point)
1523
- const pak = await detectPackageManager(
1524
- typeof options.cwd === 'string'
1525
- ? // If a cwd was provided, use it
1526
- { cwd: options.cwd }
1527
- : // Otherwise find the ioBroker root dir
1528
- {
1529
- cwd: __dirname,
1530
- setCwdToPackageRoot: true
1531
- }
1532
- );
1549
+ const pak = await detectPackageManagerWithFallback(options.cwd);
1533
1550
  // By default, don't print all the stuff the package manager spits out
1534
1551
  if (!options.debug) {
1535
1552
  pak.loglevel = 'error';
@@ -1563,16 +1580,7 @@ async function uninstallNodeModule(packageName, options = {}) {
1563
1580
  */
1564
1581
  async function rebuildNodeModules(options = {}) {
1565
1582
  // Figure out which package manager is in charge (probably npm at this point)
1566
- const pak = await detectPackageManager(
1567
- typeof options.cwd === 'string'
1568
- ? // If a cwd was provided, use it
1569
- { cwd: options.cwd }
1570
- : // Otherwise find the ioBroker root dir
1571
- {
1572
- cwd: __dirname,
1573
- setCwdToPackageRoot: true
1574
- }
1575
- );
1583
+ const pak = await detectPackageManagerWithFallback(options.cwd);
1576
1584
  // By default, don't print all the stuff the package manager spits out
1577
1585
  if (!options.debug) {
1578
1586
  pak.loglevel = 'error';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/js-controller-common",
3
- "version": "4.0.5",
3
+ "version": "4.0.6-alpha.0-20220209-3a6580b2",
4
4
  "engines": {
5
5
  "node": ">=12.0.0"
6
6
  },
@@ -47,5 +47,5 @@
47
47
  "lib/",
48
48
  "index.js"
49
49
  ],
50
- "gitHead": "ba55e7d15a1e68e18d03254d0fa8030d7f785651"
50
+ "gitHead": "ce7aaf729286e88c7a82bca5d9b29c9581d308ba"
51
51
  }