@spark-ui/cli-utils 2.13.8 → 2.14.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.14.0](https://github.com/adevinta/spark/compare/@spark-ui/cli-utils@2.13.8...@spark-ui/cli-utils@2.14.0) (2024-06-14)
7
+
8
+ ### Features
9
+
10
+ - **accordion:** accordion component ([023c2f9](https://github.com/adevinta/spark/commit/023c2f94cd5af6dc6ed48d39fe8224e3c7373847))
11
+
6
12
  ## [2.13.8](https://github.com/adevinta/spark/compare/@spark-ui/cli-utils@2.13.7...@spark-ui/cli-utils@2.13.8) (2024-06-13)
7
13
 
8
14
  **Note:** Version bump only for package @spark-ui/cli-utils
@@ -1,15 +1,21 @@
1
1
  #! /usr/bin/env node
2
2
 
3
3
  import { Command } from 'commander'
4
- import { adoption } from '../src/scan/index.mjs'
4
+ import { adoption, config } from '../src/scan/index.mjs'
5
5
 
6
6
  const program = new Command()
7
7
 
8
8
  program
9
9
  .command('adoption')
10
10
  .description('Scan @spark-ui adoption for .tsx files with given imports')
11
- .option('-c, --configuration <path>', 'configuration file route', '.spark-ui.cjs')
12
- .option('-o, --output <path>', 'output file route')
11
+ .option('-c, --configuration <config>', 'configuration file route', '.spark-ui.cjs')
12
+ .option('-o, --output <output>', 'output file route')
13
+ .option('-v, --verbose', 'output log information', false)
14
+ .option('-d, --details', 'output information about each match', config.details)
15
+ .option('-s, --sort <sort>', 'sort results (count or alphabetical)', config.sort)
16
+ .option('-dir, --directory <directory>', 'directory to parse', config.directory)
17
+ .option('-ext, --extensions <extensions...>', 'file extensions to parse', config.extensions)
18
+ .option('-i, --imports <imports...>', 'import patterns to identify', config.imports)
13
19
  .action(adoption)
14
20
 
15
21
  program.parse(process.argv)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spark-ui/cli-utils",
3
- "version": "2.13.8",
3
+ "version": "2.14.0-beta.2",
4
4
  "description": "Spark CLI utils",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -39,11 +39,11 @@
39
39
  "esbuild": "0.21.5",
40
40
  "fs-extra": "11.2.0",
41
41
  "glob": "8.1.0",
42
+ "lodash.merge": "4.6.2",
42
43
  "pascal-case": "3.1.2",
43
44
  "ts-morph": "22.0.0"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@types/fs-extra": "11.0.4"
47
- },
48
- "gitHead": "9fce4dc9e8baec940c2f81e16e55e7631d45f2f8"
48
+ }
49
49
  }
@@ -38,7 +38,7 @@ export default ({ name, description }) => `{
38
38
  "category": "components"
39
39
  },
40
40
  "bugs": {
41
- "url": "https://github.com/adevinta/spark/issues?q=is%3Aopen+label%3Autility+label%3A${name}"
41
+ "url": "https://github.com/adevinta/spark/issues?q=is%3Aopen+label%3A%22Component%3A+${name}%22"
42
42
  },
43
43
  "homepage": "https://sparkui.vercel.app",
44
44
  "license": "MIT"
@@ -0,0 +1,7 @@
1
+ export const details = false
2
+ export const sort = 'count'
3
+ export const imports = ['@spark-ui']
4
+ export const extensions = ['.tsx', '.ts']
5
+ export const directory = '.'
6
+ export const verbose = false
7
+ export const output = null
@@ -1,49 +1,42 @@
1
1
  import * as process from 'node:process'
2
2
 
3
- import { appendFileSync, existsSync } from 'fs'
3
+ import { appendFileSync, existsSync, mkdirSync } from 'fs'
4
+ import merge from 'lodash.merge'
4
5
  import path from 'path'
5
6
 
7
+ import * as defaultConfig from './config.mjs'
8
+ import { loadConfig } from './loadConfig.mjs'
6
9
  import { scanCallback } from './scanCallback.mjs'
7
- import { logger, scanDirectories } from './utils/index.mjs'
8
-
9
- const DEFAULT_CONFIG = {
10
- adoption: {
11
- details: false,
12
- sort: 'count',
13
- imports: ['@spark-ui'],
14
- extensions: ['.tsx', '.ts'],
15
- directory: '.',
16
- },
17
- }
10
+ import { Logger, scanDirectories } from './utils/index.mjs'
18
11
 
19
12
  export async function adoption(options) {
20
- let config = DEFAULT_CONFIG
13
+ const { configuration, ...optionsConfig } = options
14
+ const configFileRoute = path.join(process.cwd(), configuration || '.spark-ui.cjs')
21
15
 
22
- const configFileRoute = path.join(process.cwd(), options.configuration || '.spark-ui.cjs')
23
- try {
24
- if (existsSync(configFileRoute)) {
25
- logger.info('ℹ️ Loading spark-ui custom configuration file')
26
- const { default: customConfig } = await import(
27
- path.join(process.cwd(), options.configuration)
28
- )
29
- config = structuredClone(customConfig, DEFAULT_CONFIG)
30
- } else {
31
- logger.warn('⚠️ No custom configuration file found')
32
- logger.info('ℹ️ Loading default configuration')
33
- }
34
- } catch (error) {
35
- logger.error('💥 Something went wrong loading the custom configuration file')
36
- }
16
+ const logger = new Logger({ verbose: optionsConfig.verbose })
17
+ let config = await loadConfig(configFileRoute, { logger })
37
18
 
38
- const extensions = config.adoption.extensions
19
+ config = merge(config, {
20
+ adoption: Object.assign(
21
+ { ...defaultConfig },
22
+ {
23
+ ...optionsConfig,
24
+ }
25
+ ),
26
+ })
27
+
28
+ console.log(JSON.stringify(config, null, 2))
39
29
 
40
30
  let importCount = 0
41
31
  const importResults = {}
42
32
  let importsUsed = {}
43
33
  let importsCount = {}
44
- config.adoption.imports.forEach(moduleName => {
34
+
35
+ const { details, directory, extensions, imports, sort, output } = config.adoption
36
+
37
+ imports.forEach(moduleName => {
45
38
  logger.info(`ℹ️ Scanning adoption for ${moduleName}`)
46
- const directoryPath = path.join(process.cwd(), config.adoption.directory)
39
+ const directoryPath = path.join(process.cwd(), directory)
47
40
 
48
41
  const response = scanDirectories(directoryPath, moduleName, extensions, scanCallback, {
49
42
  importCount,
@@ -64,7 +57,7 @@ export async function adoption(options) {
64
57
  })
65
58
 
66
59
  // Sort importsUsed by alphabet
67
- if (config.adoption.sort === 'alphabetical') {
60
+ if (sort === 'alphabetical') {
68
61
  importsUsed = Object.fromEntries(
69
62
  Object.entries(importsUsed)
70
63
  .sort(([pkgNameA], [pkgNameB]) => pkgNameA.localeCompare(pkgNameB))
@@ -83,7 +76,7 @@ export async function adoption(options) {
83
76
  ]
84
77
  })
85
78
  )
86
- } else if (config.adoption.sort === 'count') {
79
+ } else if (sort === 'count') {
87
80
  // Sort importsUsed by most used
88
81
  importsUsed = Object.fromEntries(
89
82
  Object.entries(importsUsed)
@@ -110,17 +103,25 @@ export async function adoption(options) {
110
103
  const result = Object.fromEntries(
111
104
  Object.entries(importsUsed).map(([pkgName, value]) => [
112
105
  pkgName,
113
- { ...value, ...(config.adoption.details && { results: importResults[pkgName] }) },
106
+ { ...value, ...(details && { results: importResults[pkgName] }) },
114
107
  ])
115
108
  )
116
109
 
117
- if (options.output) {
110
+ if (output) {
118
111
  try {
119
- appendFileSync(`${options.output}`, JSON.stringify(result, null, 2))
112
+ const { dir } = path.parse(path.join(process.cwd(), output))
113
+ if (!existsSync(dir)) {
114
+ mkdirSync(dir, { recursive: true })
115
+ }
116
+ appendFileSync(`${path.join(process.cwd(), output)}`, JSON.stringify(result, null, 2))
120
117
  } catch (err) {
121
118
  logger.error(`💥 Error writing file: ${err}`)
119
+ process.exit(1)
122
120
  }
123
121
  } else {
124
- logger.info(JSON.stringify(result, null, 2))
122
+ logger.force().info(JSON.stringify(result, null, 2))
123
+ process.exit(0)
125
124
  }
126
125
  }
126
+
127
+ export const config = { ...defaultConfig }
@@ -0,0 +1,21 @@
1
+ import { existsSync } from 'fs'
2
+
3
+ export async function loadConfig(configFileRoute, { logger }) {
4
+ try {
5
+ if (existsSync(configFileRoute)) {
6
+ logger.info('ℹ️ Loading spark-ui custom configuration file')
7
+ const { default: customConfig } = await import(configFileRoute)
8
+
9
+ return customConfig
10
+ } else {
11
+ logger.warn('⚠️ No custom configuration file found')
12
+ logger.info('ℹ️ Loading default configuration')
13
+
14
+ return {}
15
+ }
16
+ } catch (error) {
17
+ logger.error('💥 Something went wrong loading the custom configuration file')
18
+
19
+ return {}
20
+ }
21
+ }
@@ -1,5 +1,5 @@
1
1
  export { extractImports } from './extract-imports.mjs'
2
2
  export { fileContainsImport } from './file-contains-import.mjs'
3
3
  export { getFormatedTimestamp } from './get-formated-timestamp.mjs'
4
- export { logger } from './logger.mjs'
4
+ export { Logger } from './logger.mjs'
5
5
  export { scanDirectories } from './scan-directories.mjs'
@@ -1,19 +1,55 @@
1
1
  import chalk from 'chalk'
2
2
 
3
- export const logger = {
3
+ export class Logger {
4
+ #force = false
5
+
6
+ constructor({ verbose }) {
7
+ this.verbose = verbose
8
+
9
+ if (typeof Logger.instance === 'object') {
10
+ return Logger.instance
11
+ }
12
+
13
+ Logger.instance = this
14
+
15
+ return this
16
+ }
17
+
18
+ #log({ type = v => v, force, verbose }, ...args) {
19
+ if (force || verbose) {
20
+ console.log(type(...args)) // eslint-disable-line no-console
21
+ }
22
+ this.#force = false
23
+ }
24
+
25
+ force() {
26
+ this.#force = true
27
+
28
+ return this
29
+ }
30
+
4
31
  error(...args) {
5
- console.log(chalk.red(...args))
6
- },
32
+ this.#log({ type: chalk.red, force: this.#force, verbose: this.verbose }, ...args)
33
+ // this.verbose && console.log(chalk.red(...args))
34
+ }
35
+
7
36
  warn(...args) {
8
- console.log(chalk.yellow(...args))
9
- },
37
+ this.#log({ type: chalk.yellow, force: this.#force, verbose: this.verbose }, ...args)
38
+ // this.verbose && console.log(chalk.yellow(...args))
39
+ }
40
+
10
41
  info(...args) {
11
- console.log(chalk.cyan(...args))
12
- },
42
+ this.#log({ type: chalk.cyan, force: this.#force, verbose: this.verbose }, ...args)
43
+ // this.verbose && console.log(chalk.cyan(...args))
44
+ }
45
+
13
46
  success(...args) {
14
- console.log(chalk.green(...args))
15
- },
47
+ this.#log({ type: chalk.green, force: this.#force, verbose: this.verbose }, ...args)
48
+ // this.verbose && console.log(chalk.green(...args))
49
+ }
50
+
16
51
  break() {
17
- console.log('')
18
- },
52
+ this.#log({ type: chalk.green, force: this.#force, verbose: this.verbose }, '')
53
+ // this.verbose && console.log('')
54
+ }
19
55
  }