@spark-ui/cli-utils 2.13.4 → 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 +6 -0
- package/bin/spark-scan.mjs +9 -3
- package/package.json +3 -3
- package/src/scan/config.mjs +7 -0
- package/src/scan/index.mjs +50 -35
- package/src/scan/utils/index.mjs +1 -1
- package/src/scan/utils/logger.mjs +47 -11
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.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
|
+
|
|
6
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)
|
|
7
13
|
|
|
8
14
|
### Bug Fixes
|
package/bin/spark-scan.mjs
CHANGED
|
@@ -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 <
|
|
12
|
-
.option('-o, --output <
|
|
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
|
+
"version": "2.13.5-beta.0",
|
|
4
4
|
"description": "Spark CLI utils",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -39,11 +39,11 @@
|
|
|
39
39
|
"esbuild": "0.21.3",
|
|
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": "af658fa8212d61109674b7fc0584678c0ded401b"
|
|
48
|
+
}
|
|
49
49
|
}
|
package/src/scan/index.mjs
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
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
|
-
|
|
27
|
-
const { default: customConfig } = await import(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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.
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
59
|
+
`🎉 Found ${response.importCount - importCount} imports with "${moduleName}" modules across directory ${directoryPath}.`
|
|
55
60
|
)
|
|
56
61
|
} else {
|
|
57
|
-
logger.warn(
|
|
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 (
|
|
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 (
|
|
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, ...(
|
|
116
|
+
{ ...value, ...(details && { results: importResults[pkgName] }) },
|
|
110
117
|
])
|
|
111
118
|
)
|
|
112
119
|
|
|
113
|
-
if (
|
|
120
|
+
if (output) {
|
|
114
121
|
try {
|
|
115
|
-
|
|
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(
|
|
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 }
|
package/src/scan/utils/index.mjs
CHANGED
|
@@ -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 {
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
18
|
-
|
|
52
|
+
this.#log({ type: chalk.green, force: this.#force, verbose: this.verbose }, '')
|
|
53
|
+
// this.verbose && console.log('')
|
|
54
|
+
}
|
|
19
55
|
}
|