Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(yarn-detection): detect the presence of yarn and hadoop installations #34

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this line?

Text editor files should be in local ignore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback. It has been removed now.

node_modules/

coverage/
30 changes: 13 additions & 17 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,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);
mralaminahamed marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) /* c8 ignore start */ {
print(pico.red(e.stdout.toString()))
print(
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