Skip to content

Commit

Permalink
[TASK] create cmslib cli alias and make it executable
Browse files Browse the repository at this point in the history
  • Loading branch information
dmh committed Jan 17, 2024
1 parent e900602 commit 13081e0
Show file tree
Hide file tree
Showing 8 changed files with 541 additions and 399 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# @resultify/hubspot-cms-lib
Library with additional functionality on top of native HubSpot CMS CLI library ([cli-lib](https://github.com/HubSpot/hubspot-cli/tree/master/packages/cli-lib)) with command-line interface.
Library with additional functionality on top of native HubSpot CMS CLI library ([cli-lib](https://github.com/HubSpot/cli-lib)) with command-line interface.

❗It should be used instead of `hubspot-cli`

Expand Down Expand Up @@ -28,16 +28,16 @@ Library with additional functionality on top of native HubSpot CMS CLI library (
## What's inside:

- Commands
- Watch - watches for changes in files and uploads them to HubSpot
- FetchModules - fetches all modules from HubSpot
- Fetch - fetches all files from HubSpot
- Upload - uploads all files to HubSpot
- Build - builds all files (scss, js, css)
- Validate - HubSpot marketplace validation
- Lighthouse - HubSpot lighthouse validation
- Fields - converts module fields.js to fields.json
- fetchDb - fetches chosen HubDB tables
- uploadDb - uploads chosen HubDB tables
- `build` - compile all vendor files (scss, js, css)
- `watch` - tracks file changes, compiles, and uploads/moves/deletes them to HubSpot
- `fetch` - fetches all files from HubSpot
- `fetchModules` - fetches all modules from HubSpot theme
- `upload` - uploads all files to HubSpot
- `validate` - HubSpot Marketplace validation
- `lighthouse` - HubSpot Lighthouse validation
- `fields` - converts theme/modules `fields.js` to `fields.json`
- `fetchDb` - fetches chosen HubDB tables to local `.json` files
- `uploadDb` - uploads and publish chosen HubDB tables from local `.json` files

- Compilers:
- SCSS - SASS
Expand All @@ -58,14 +58,16 @@ npm i @resultify/hubspot-cms-lib
1. Add needed scripts to package.json
```
"scripts": {
"build": "node -e 'import(`@resultify/hubspot-cms-lib/build`)'",
"upload": "node -e 'import(`@resultify/hubspot-cms-lib/upload`)'",
"fields": "node -e 'import(`@resultify/hubspot-cms-lib/fields`)'",
"validate": "node -e 'import(`@resultify/hubspot-cms-lib/validate`)'",
"lighthouse": "node -e 'import(`@resultify/hubspot-cms-lib/lighthouse`)'",
"fetchModules": "node -e 'import(`@resultify/hubspot-cms-lib/fetchModules`)'",
"fetch": "node -e 'import(`@resultify/hubspot-cms-lib/fetch`)'",
"watch": "node -e 'import(`@resultify/hubspot-cms-lib/watch`)'"
"build": "cmslib --build",
"watch": "cmslib --watch'",
"fetch": "cmslib --fetch",
"fetchModules": "cmslib --fetchModules",
"upload": "cmslib --upload",
"validate": "cmslib --validate",
"lighthouse": "cmslib --lighthouse",
"fields": "cmslib --fields",
"fetchDb": "cmslib --fetchDb",
"uploadDb": "cmslib --uploadDb"
}
```
2. Add [cmslib configuration](#configuration) to package.json
Expand Down
3 changes: 3 additions & 0 deletions bin/cmslib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node
import { init } from '../lib/init.js'
init()
2 changes: 1 addition & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"useUnknownInCatchVariables": false,
"moduleResolution": "node"
},
"include": ["lib/**/*"],
"include": ["lib/**/*", "bin/**/*"],
}
12 changes: 9 additions & 3 deletions lib/hubspot/lighthouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getThemeOptions } from '../utils/options.js'
import * as ui from '../utils/ui.js'
import { cleanUploadThemeTemplates } from '../hubspot/upload.js'
import { throwErrorIfMissingScope } from './auth/scopes.js'
import minimist from 'minimist'

/**
* @ignore
Expand Down Expand Up @@ -83,8 +84,13 @@ async function lighthouseScore (config, themeName) {
let mobileScoreResult
let averageMobileScoreResult
let verbose = false
if (process.argv.slice(1)[0] === '--verbose') {
verbose = true
const args = minimist(process.argv.slice(2), { '--': true })['--']
if (args) {
for (const arg of args) {
if (arg === '--verbose') {
verbose = true
}
}
}
try {
averageMobileScoreResult = await getLighthouseScore(portalId, { isAverage: true, mobileId: requestResult.mobileId })
Expand Down Expand Up @@ -199,7 +205,7 @@ async function lighthouseScore (config, themeName) {
console.log(`${chalk.cyan(themeName)} Average Mobile Lighthouse scores`)
showAverageResults(averageMobileScoreResult)
console.log('This is the average of all theme templates')
console.log('Use the [-- -- --verbose] option to include individual template scores')
console.log('Use the [cmslib --lighthouse -- --verbose] command to include individual template scores')
}
}

Expand Down
36 changes: 36 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { parseArgs } from './utils/arg.js'
import { build } from './cmd/build.js'
import { watch } from './cmd/watch.js'
import { hubspotFetchAll } from './cmd/fetch.js'
import { hubspotFetchModules } from './cmd/fetchModules.js'
import { upload } from './cmd/upload.js'
import { validate } from './cmd/validate.js'
import { lighthouse } from './cmd/lighthouse.js'
import { fields } from './cmd/fields.js'
import { fetchDb, uploadDb } from './cmd/hubdb.js'

const args = parseArgs()
function init () {
if (args.build) {
build()
} else if (args.watch) {
watch()
} else if (args.fetch) {
hubspotFetchAll()
} else if (args.fetchModules) {
hubspotFetchModules()
} else if (args.upload) {
upload()
} else if (args.validate) {
validate()
} else if (args.lighthouse) {
lighthouse()
} else if (args.fields) {
fields()
} else if (args.fetchDb) {
fetchDb()
} else if (args.uploadDb) {
uploadDb()
}
}
export { init }
33 changes: 33 additions & 0 deletions lib/utils/arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import chalk from 'chalk'
import minimist from 'minimist'

const argOptions = {
boolean: [
'build',
'watch',
'fetch',
'fetchModules',
'upload',
'validate',
'lighthouse',
'fields',
'fetchDb',
'uploadDb'
],
stopEarly: true,
'--': true,
unknown: (/** @type {any} */ arg) => {
console.error(`${chalk.red('Unknown')} or unexpected option: ${arg}`)
process.exit(1)
}
}

/**
* #### parse cli args
* @returns {any} cli args
*/
function parseArgs () {
return minimist(process.argv.slice(2), argOptions)
}

export { parseArgs }

0 comments on commit 13081e0

Please sign in to comment.