From 6fe76cac586baf3048abba1101f1d6af945042fd Mon Sep 17 00:00:00 2001 From: Al Amin Ahamed <34349365+mralaminahamed@users.noreply.github.com> Date: Tue, 30 Apr 2024 23:48:16 +0600 Subject: [PATCH 1/3] Feat(yarn-detection): detect the presence of yarn and hadoop installations --- .gitignore | 1 + index.js | 30 +++++++++++++----------------- test/index.test.js | 6 +++++- utils.js | 7 +++++-- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index c6b9df2..dee2ba2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ node_modules/ coverage/ diff --git a/index.js b/index.js index e90e653..cf1dc23 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,10 @@ function BrowserslistUpdateError(message) { BrowserslistUpdateError.prototype = Error.prototype +// Check if HADOOP_HOME is set to determine if this is running in a Hadoop environment +const IsHadoopExists = !! process.env.HADOOP_HOME +const yarnCommand = IsHadoopExists ? 'yarnpkg' : 'yarn' + /* c8 ignore next 3 */ function defaultPrint(str) { process.stdout.write(str) @@ -59,12 +63,9 @@ function detectLockfile() { function getLatestInfo(lock) { if (lock.mode === 'yarn') { if (lock.version === 1) { - return JSON.parse(execSync('yarnpkg info caniuse-lite --json').toString()) - .data + return JSON.parse(execSync(yarnCommand + ' info caniuse-lite --json').toString()).data } else { - return JSON.parse( - execSync('yarnpkg npm info caniuse-lite --json').toString() - ) + return JSON.parse(execSync(yarnCommand + ' npm info caniuse-lite --json').toString()) } } if (lock.mode === 'pnpm') { @@ -209,10 +210,10 @@ function updatePackageManually(print, lock, latest) { ) writeFileSync(lock.file, lockfileData.content) - let install = lock.mode === 'yarn' ? 'yarnpkg add -W' : lock.mode + ' install' + let install = lock.mode === 'yarn' ? yarnCommand + ' add -W' : lock.mode + ' install' print( 'Installing new caniuse-lite version\n' + - pico.yellow('$ ' + install.replace('yarnpkg', 'yarn') + ' caniuse-lite') + + pico.yellow('$ ' + install + ' caniuse-lite') + '\n' ) try { @@ -232,24 +233,19 @@ function updatePackageManually(print, lock, latest) { process.exit(1) } /* c8 ignore end */ - let del = - lock.mode === 'yarn' ? 'yarnpkg remove -W' : lock.mode + ' uninstall' + let del = lock.mode === 'yarn' ? yarnCommand + ' remove -W' : lock.mode + ' uninstall' print( 'Cleaning package.json dependencies from caniuse-lite\n' + - pico.yellow('$ ' + del.replace('yarnpkg', 'yarn') + ' caniuse-lite') + + pico.yellow('$ ' + del + ' caniuse-lite') + '\n' ) execSync(del + ' caniuse-lite') } function updateWith(print, cmd) { - print( - 'Updating caniuse-lite version\n' + - pico.yellow('$ ' + cmd.replace('yarnpkg', 'yarn')) + - '\n' - ) + print( 'Updating caniuse-lite version\n' + pico.yellow('$ ' + cmd) + '\n' ) try { - execSync(cmd) + execSync(cmd); } catch (e) /* c8 ignore start */ { print(pico.red(e.stdout.toString())) print( @@ -282,7 +278,7 @@ module.exports = function updateDB(print = defaultPrint) { print('Latest version: ' + pico.bold(pico.green(latest.version)) + '\n') if (lock.mode === 'yarn' && lock.version !== 1) { - updateWith(print, 'yarnpkg up -R caniuse-lite') + updateWith(print, yarnCommand + ' up -R caniuse-lite') } else if (lock.mode === 'pnpm') { updateWith(print, 'pnpm up caniuse-lite') } else { diff --git a/test/index.test.js b/test/index.test.js index aad1998..667b8c1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -8,6 +8,10 @@ let { join } = require('node:path') let updateDb = require('..') +// Check if HADOOP_HOME is set to determine if this is running in a Hadoop environment +const IsHadoopExists = !! process.env.HADOOP_HOME +const yarnCommand = IsHadoopExists ? 'yarnpkg' : 'yarn' + let testDir test.after.each(async () => { process.chdir(__dirname) @@ -239,7 +243,7 @@ if ( 'caniuse-lite has been successfully updated\n' ) checkYarnLockfile(dir, 2) - execSync('yarn set version classic') + execSync(yarnCommand + ' set version classic') }) } diff --git a/utils.js b/utils.js index d5186b3..355eaca 100644 --- a/utils.js +++ b/utils.js @@ -3,8 +3,11 @@ const { EOL } = require('node:os') const getFirstRegexpMatchOrDefault = (text, regexp, defaultValue) => { regexp.lastIndex = 0 // https://stackoverflow.com/a/11477448/4536543 let match = regexp.exec(text) - if (match !== null) return match[1] - return defaultValue + if (match !== null) { + return match[1] + } else { + return defaultValue + } } const DEFAULT_INDENT = ' ' From 864b13cefb70b92c88887be982013a6de4e26ef8 Mon Sep 17 00:00:00 2001 From: Al Amin Ahamed <34349365+mralaminahamed@users.noreply.github.com> Date: Wed, 1 May 2024 00:00:07 +0600 Subject: [PATCH 2/3] remove: PHPStorm ide config from gitignore file --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index dee2ba2..c6b9df2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -.idea/ node_modules/ coverage/ From f8367d3e1fdaceae8ffecfd1ec3f279c91567cc1 Mon Sep 17 00:00:00 2001 From: Al Amin Ahamed <34349365+mralaminahamed@users.noreply.github.com> Date: Wed, 1 May 2024 00:02:25 +0600 Subject: [PATCH 3/3] remove: unwanted semicolon from function call --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index cf1dc23..1bfd129 100644 --- a/index.js +++ b/index.js @@ -245,7 +245,7 @@ function updatePackageManually(print, lock, latest) { function updateWith(print, cmd) { print( 'Updating caniuse-lite version\n' + pico.yellow('$ ' + cmd) + '\n' ) try { - execSync(cmd); + execSync(cmd) } catch (e) /* c8 ignore start */ { print(pico.red(e.stdout.toString())) print(