Skip to content

Commit

Permalink
Merge pull request #34 from mralaminahamed/main
Browse files Browse the repository at this point in the history
Feat(yarn-detection): detect the presence of yarn and hadoop installations
  • Loading branch information
ai authored May 1, 2024
2 parents fd63de4 + f8367d3 commit 0aa5fa5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
28 changes: 12 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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 {
Expand All @@ -232,22 +233,17 @@ 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)
} catch (e) /* c8 ignore start */ {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
})
}

Expand Down
7 changes: 5 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' '
Expand Down

0 comments on commit 0aa5fa5

Please sign in to comment.