@spark-ui/cli-utils 2.13.3 → 2.13.5-beta.0

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,18 @@
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.13.5](https://github.com/adevinta/spark/compare/@spark-ui/cli-utils@2.13.4...@spark-ui/cli-utils@2.13.5) (2024-05-30)
7
+
8
+ ### Bug Fixes
9
+
10
+ - **cli-utils:** improve log messages ([9623f94](https://github.com/adevinta/spark/commit/9623f9490cda5af0d00bbb10545e3769922cfc63))
11
+
12
+ ## [2.13.4](https://github.com/adevinta/spark/compare/@spark-ui/cli-utils@2.13.3...@spark-ui/cli-utils@2.13.4) (2024-05-27)
13
+
14
+ ### Bug Fixes
15
+
16
+ - **cli-utils:** fix binary file indicator ([02af669](https://github.com/adevinta/spark/commit/02af669d5f7a583ee36ba86902c182384460c346))
17
+
6
18
  ## [2.13.3](https://github.com/adevinta/spark/compare/@spark-ui/cli-utils@2.13.2...@spark-ui/cli-utils@2.13.3) (2024-05-27)
7
19
 
8
20
  ### Bug Fixes
@@ -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.3",
3
+ "version": "2.13.5-beta.0",
4
4
  "description": "Spark CLI utils",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -11,6 +11,7 @@
11
11
  "utility"
12
12
  ],
13
13
  "bin": {
14
+ ".": "./bin/spark.mjs",
14
15
  "spark": "./bin/spark.mjs",
15
16
  "spark-generate": "./bin/spark-generate.mjs",
16
17
  "spark-scan": "./bin/spark-scan.mjs"
@@ -38,11 +39,11 @@
38
39
  "esbuild": "0.21.3",
39
40
  "fs-extra": "11.2.0",
40
41
  "glob": "8.1.0",
42
+ "lodash.merge": "4.6.2",
41
43
  "pascal-case": "3.1.2",
42
44
  "ts-morph": "22.0.0"
43
45
  },
44
46
  "devDependencies": {
45
47
  "@types/fs-extra": "11.0.4"
46
- },
47
- "gitHead": "706d373778dceb4a250980636e32d3236f248227"
48
+ }
48
49
  }
@@ -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,47 +1,52 @@
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'
6
8
  import { scanCallback } from './scanCallback.mjs'
7
- import { logger } from './utils/logger.mjs'
8
- import { scanDirectories } from './utils/scan-directories.mjs'
9
-
10
- const DEFAULT_CONFIG = {
11
- adoption: {
12
- details: false,
13
- sort: 'count',
14
- imports: ['@spark-ui'],
15
- extensions: ['.tsx', '.ts'],
16
- directory: '.',
17
- },
18
- }
9
+ import { Logger, scanDirectories } from './utils/index.mjs'
19
10
 
20
11
  export async function adoption(options) {
21
- let config = DEFAULT_CONFIG
12
+ const { configuration, ...optionsConfig } = options
13
+ const configFileRoute = path.join(process.cwd(), configuration || '.spark-ui.cjs')
14
+
15
+ let config = {
16
+ adoption: Object.assign(
17
+ { ...defaultConfig },
18
+ {
19
+ ...optionsConfig,
20
+ }
21
+ ),
22
+ }
23
+
24
+ const { verbose } = config.adoption
25
+ const logger = new Logger({ verbose })
22
26
 
23
- const configFileRoute = path.join(process.cwd(), options.configuration || '.spark-ui.cjs')
24
27
  try {
25
28
  if (existsSync(configFileRoute)) {
26
- console.log('✨✨✨ loading spark-ui custom configuration file ✨✨✨')
27
- const { default: customConfig } = await import(
28
- path.join(process.cwd(), options.configuration)
29
- )
30
- config = structuredClone(customConfig, DEFAULT_CONFIG)
29
+ logger.info('ℹ️ Loading spark-ui custom configuration file')
30
+ const { default: customConfig } = await import(configFileRoute)
31
+ config = merge(config, customConfig)
32
+ } else {
33
+ logger.warn('⚠️ No custom configuration file found')
34
+ logger.info('ℹ️ Loading default configuration')
31
35
  }
32
36
  } catch (error) {
33
- logger.info('ℹ️ Loading default configuration')
37
+ logger.error('💥 Something went wrong loading the custom configuration file')
34
38
  }
35
39
 
36
- const extensions = config.adoption.extensions
37
-
38
40
  let importCount = 0
39
41
  const importResults = {}
40
42
  let importsUsed = {}
41
43
  let importsCount = {}
42
- config.adoption.imports.forEach(moduleName => {
43
- console.log(`scanning adoption for ${moduleName}`)
44
- const directoryPath = path.join(process.cwd(), config.adoption.directory)
44
+
45
+ const { details, directory, extensions, imports, sort, output } = config.adoption
46
+
47
+ imports.forEach(moduleName => {
48
+ logger.info(`ℹ️ Scanning adoption for ${moduleName}`)
49
+ const directoryPath = path.join(process.cwd(), directory)
45
50
 
46
51
  const response = scanDirectories(directoryPath, moduleName, extensions, scanCallback, {
47
52
  importCount,
@@ -51,16 +56,18 @@ export async function adoption(options) {
51
56
  })
52
57
  if (importCount !== response.importCount) {
53
58
  logger.success(
54
- `Found ${response.importCount - importCount} imports with "${moduleName}" modules across directory ${directoryPath}.`
59
+ `🎉 Found ${response.importCount - importCount} imports with "${moduleName}" modules across directory ${directoryPath}.`
55
60
  )
56
61
  } else {
57
- logger.warn(`No files found with "${moduleName}" imports across directory ${directoryPath}.`)
62
+ logger.warn(
63
+ `⚠️ No files found with "${moduleName}" imports across directory ${directoryPath}.`
64
+ )
58
65
  }
59
66
  importCount = response.importCount
60
67
  })
61
68
 
62
69
  // Sort importsUsed by alphabet
63
- if (config.adoption.sort === 'alphabetical') {
70
+ if (sort === 'alphabetical') {
64
71
  importsUsed = Object.fromEntries(
65
72
  Object.entries(importsUsed)
66
73
  .sort(([pkgNameA], [pkgNameB]) => pkgNameA.localeCompare(pkgNameB))
@@ -79,7 +86,7 @@ export async function adoption(options) {
79
86
  ]
80
87
  })
81
88
  )
82
- } else if (config.adoption.sort === 'count') {
89
+ } else if (sort === 'count') {
83
90
  // Sort importsUsed by most used
84
91
  importsUsed = Object.fromEntries(
85
92
  Object.entries(importsUsed)
@@ -106,17 +113,25 @@ export async function adoption(options) {
106
113
  const result = Object.fromEntries(
107
114
  Object.entries(importsUsed).map(([pkgName, value]) => [
108
115
  pkgName,
109
- { ...value, ...(config.adoption.details && { results: importResults[pkgName] }) },
116
+ { ...value, ...(details && { results: importResults[pkgName] }) },
110
117
  ])
111
118
  )
112
119
 
113
- if (options.output) {
120
+ if (output) {
114
121
  try {
115
- appendFileSync(`${options.output}`, JSON.stringify(result, null, 2))
122
+ const { dir } = path.parse(path.join(process.cwd(), output))
123
+ if (!existsSync(dir)) {
124
+ mkdirSync(dir, { recursive: true })
125
+ }
126
+ appendFileSync(`${path.join(process.cwd(), output)}`, JSON.stringify(result, null, 2))
116
127
  } catch (err) {
117
- logger.error(`Error writing file: ${err}`)
128
+ logger.error(`💥 Error writing file: ${err}`)
129
+ process.exit(1)
118
130
  }
119
131
  } else {
120
- logger.info(JSON.stringify(result, null, 2))
132
+ // logger.force().info(JSON.stringify(result, null, 2))
133
+ process.exit(0)
121
134
  }
122
135
  }
136
+
137
+ export const config = { ...defaultConfig }
@@ -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
  }