Skip to content

Commit a72f93f

Browse files
committed
[FEATURE] add browsers cmd to show browserslist info to the cli
1 parent cafa378 commit a72f93f

7 files changed

Lines changed: 183 additions & 6 deletions

File tree

lib/args/args.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import minimist from 'minimist'
66
const commands = [
77
'info',
88
'init',
9+
'browsers',
910
'build',
1011
'watch',
1112
'fetch',

lib/args/help.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ function help (data) {
1616
console.log(`CLI Commands:
1717
rh info Info about the current project
1818
rh init Initialize a new project based on a predefined config
19+
rh browsers Show supported browsers and audience coverage
1920
`)
20-
console.log(`CMS Lib Commands:
21+
console.log(`HubSpot CMS Lib Commands:
2122
rh upload Upload all
2223
rh fetch Fetch all
2324
rh fetchModules Fetch modules

lib/cmd/browsers.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/// <reference path="../types/types.js" />
2+
import browserslist from 'browserslist'
3+
import { isFileDirExists } from '../utils/fs.js'
4+
import chalk from 'chalk'
5+
6+
/**
7+
* #### get browserslist info from ${cwdPath}/.browserlistrc
8+
* @async
9+
* @param {LOCALDATA} data - local data
10+
* @returns undefined
11+
*/
12+
async function browsers (data) {
13+
if (await isFileDirExists(`${data.cwd.path}/.browserslistrc`)) {
14+
const browsrs = browserslist(undefined, {
15+
path: `${data.cwd.path}/.browserslistrc`,
16+
config: '.browserslistrc'
17+
})
18+
const browserslistConfig = browserslist.readConfig(`${data.cwd.path}/.browserslistrc`)
19+
const configToString = browserslistConfig.defaults.join('__NEWLINE__').replace(/ /g, '+')
20+
const encodedString = encodeURI(configToString)
21+
const browserslistLink = encodedString.replace(/__NEWLINE__/g, '%0A') + '%0A'
22+
const global = browserslist.coverage(browserslist(browserslistConfig.defaults))
23+
const eu = browserslist.coverage(browserslist(browserslistConfig.defaults), 'alt-EU')
24+
const se = browserslist.coverage(browserslist(browserslistConfig.defaults), 'SE')
25+
const chromeFirst = browsrs.find(e => e.includes('chrome'))
26+
const chromeLast = browsrs.slice().reverse().find(e => e.includes('chrome'))
27+
const firefoxFirst = browsrs.find(e => e.includes('firefox'))
28+
const firefoxLast = browsrs.slice().reverse().find(e => e.includes('firefox'))
29+
const iosSafFirst = browsrs.find(e => e.includes('ios_saf'))
30+
const iosSafLast = browsrs.slice().reverse().find(e => e.includes('ios_saf'))
31+
const safariFirst = browsrs.find(e => e.includes('safari'))
32+
const safariLast = browsrs.slice().reverse().find(e => e.includes('safari'))
33+
const edgeFirst = browsrs.find(e => e.includes('edge'))
34+
const edgeLast = browsrs.slice().reverse().find(e => e.includes('edge'))
35+
const samsungFirst = browsrs.find(e => e.includes('samsung'))
36+
const samsungLast = browsrs.slice().reverse().find(e => e.includes('samsung'))
37+
const andChrFirst = browsrs.find(e => e.includes('and_chr'))
38+
const andChrLast = browsrs.slice().reverse().find(e => e.includes('and_chr'))
39+
console.log(chalk.green('\nSupported browsers:'))
40+
console.log(`Chrome ${chromeLast?.split(' ')[1]}-${chromeFirst?.split(' ')[1]}`)
41+
console.log(`Firefox ${firefoxLast?.split(' ')[1]}-${firefoxFirst?.split(' ')[1]}`)
42+
console.log(`iOS Safari ${iosSafLast?.split(' ')[1]}-${iosSafFirst?.split(' ')[1]}`)
43+
console.log(`Safari ${safariLast?.split(' ')[1]}-${safariFirst?.split(' ')[1]}`)
44+
console.log(`Edge ${edgeLast?.split(' ')[1]}-${edgeFirst?.split(' ')[1]}`)
45+
if (samsungFirst === samsungLast) {
46+
console.log(`Samsung ${samsungFirst?.split(' ')[1]}`)
47+
} else {
48+
console.log(`Samsung ${samsungLast?.split(' ')[1]}-${samsungFirst?.split(' ')[1]}`)
49+
}
50+
if (andChrFirst === andChrLast) {
51+
console.log(`Android Chrome ${andChrFirst?.split(' ')[1]}`)
52+
} else {
53+
console.log(`Android Chrome ${andChrLast?.split(' ')[1]}-${andChrFirst?.split(' ')[1]}`)
54+
}
55+
console.log(chalk.bold('\nAudience coverage:'))
56+
console.log(`Global: ${chalk.green(`${Math.ceil(global)}%`)}`)
57+
console.log(`Europe: ${chalk.green(`${Math.ceil(eu)}%`)}`)
58+
console.log(`Sweden: ${chalk.green(`${Math.ceil(se)}%`)}`)
59+
60+
console.log(`\nClick to open the browser compatibility list:\n${chalk.cyan(`https://browsersl.ist/?results#q=${browserslistLink}`)}`)
61+
} else {
62+
console.log(`No .browserslistrc found in ${data.cwd.path}`)
63+
}
64+
}
65+
66+
export { browsers }

lib/cmd/info.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path="../types/types.js" />
22
import chalk from 'chalk'
3+
import { isFileDirExists } from '../utils/fs.js'
34

45
/**
56
* #### theme info
@@ -18,14 +19,18 @@ function themeInfo (data) {
1819
/**
1920
* #### possible commands to run
2021
* @private
22+
* @async
2123
* @param {LOCALDATA} data - env variables
22-
* @returns {string} theme info
24+
* @returns {Promise<string>} possible commands
2325
*/
24-
function possibleCmds (data) {
26+
async function possibleCmds (data) {
2527
const cmds = []
2628
if (Array.isArray(data.env) && data.env.includes('GITHUB_TOKEN')) {
2729
cmds.push('rh init')
2830
}
31+
if (await isFileDirExists(`${data.cwd.path}/.browserslistrc`)) {
32+
cmds.push('rh browsers')
33+
}
2934
if (data.theme && data.theme instanceof Object && data.theme.name) {
3035
cmds.push('rh upload', 'rh fetch', 'rh fetchModules', 'rh build', 'rh watch', 'rh validate', 'rh fields', 'rh fetchDb', 'rh uploadDb', 'rh lighthouse')
3136
}
@@ -39,14 +44,15 @@ function possibleCmds (data) {
3944
/**
4045
* #### Show info about the current project
4146
* @param {LOCALDATA} data - local data
47+
* @async
4248
* @returns undefined
4349
*/
44-
function info (data) {
50+
async function info (data) {
4551
console.log(`Folder name: ${chalk.cyan(data.cwd.name)}
4652
Git repository: ${data.git.isRepo || chalk.red('not found')}
4753
${themeInfo(data)}
4854
`)
49-
console.log(possibleCmds(data))
55+
console.log(await possibleCmds(data))
5056
}
5157

5258
export { info }

lib/init.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { collectLocalData } from './localdata.js'
1111
import { info } from './cmd/info.js'
1212
import { init } from './cmd/init.js'
13+
import { browsers } from './cmd/browsers.js'
1314
import { help } from './args/help.js'
1415
import { debug } from './args/debug.js'
1516
import { debugV } from './args/debug-verbose.js'
@@ -75,11 +76,14 @@ async function cliInit () {
7576
switch (args._[0]) {
7677
case 'info':
7778
// show info about the current project
78-
info(data)
79+
await info(data)
7980
break
8081
case 'init':
8182
await init(data)
8283
break
84+
case 'browsers':
85+
await browsers(data)
86+
break
8387
case 'upload':
8488
// upload files to HubSpot
8589
checkIsHubSpotCmsTheme(data)

package-lock.json

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@inquirer/select": "2.3.10",
4343
"@octokit/request": "9.1.3",
4444
"@resultify/hubspot-cms-lib": "3.4.1",
45+
"browserslist": "4.23.2",
4546
"chalk": "5.3.0",
4647
"cli-table3": "0.6.5",
4748
"del": "7.1.0",

0 commit comments

Comments
 (0)