@mouse_484/eslint-config 3.5.13 → 3.5.15
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/bin/cli.js +111 -61
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { execSync, spawn } from 'node:child_process'
|
|
3
3
|
import fs from 'node:fs/promises'
|
|
4
4
|
import path from 'node:path'
|
|
5
5
|
import process from 'node:process'
|
|
6
|
-
import { promisify } from 'node:util'
|
|
7
6
|
import { resolveCommand } from 'package-manager-detector/commands'
|
|
8
7
|
import { detect } from 'package-manager-detector/detect'
|
|
9
8
|
|
|
10
|
-
const exec = promisify(_exec)
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* @typedef {object} PackageInfo
|
|
14
11
|
* @property {string} name - Package name
|
|
15
12
|
* @property {string} import - Import name in config
|
|
13
|
+
* @property {string} [version] - Package version
|
|
16
14
|
*/
|
|
17
15
|
|
|
18
16
|
/** @type {PackageInfo} */
|
|
@@ -27,77 +25,129 @@ const TARGET = {
|
|
|
27
25
|
import: 'mouse',
|
|
28
26
|
}
|
|
29
27
|
|
|
28
|
+
// infinite loop prevention
|
|
29
|
+
const isRunningFromSourcePackage = process.argv[1].includes(SOURCE.name)
|
|
30
|
+
|
|
30
31
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @param {string} command
|
|
33
|
-
* @param {string} args
|
|
34
|
-
* @
|
|
35
|
-
* @returns {Promise} - Promise Spawn Result
|
|
32
|
+
* Execute command with spawn
|
|
33
|
+
* @param {string} command - Command to execute
|
|
34
|
+
* @param {string[]} args - Command arguments
|
|
35
|
+
* @returns {Promise<void>}
|
|
36
36
|
*/
|
|
37
|
-
function
|
|
37
|
+
function runCommand(command, args) {
|
|
38
|
+
console.info(`Running: ${command} ${args.join(' ')}`)
|
|
39
|
+
const spawnedProcess = spawn(command, args, { stdio: 'inherit' })
|
|
40
|
+
|
|
38
41
|
return new Promise((resolve, reject) => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
spawnedProcess.on('close', (code) => {
|
|
43
|
+
if (code === 0) {
|
|
44
|
+
resolve()
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
reject(new Error(`Command failed with exit code ${code}`))
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
spawnedProcess.on('error', reject)
|
|
42
51
|
})
|
|
43
52
|
}
|
|
44
53
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
console.log(`Start replacing the config from ${SOURCE.name} to ${TARGET.name}`)
|
|
58
|
-
|
|
59
|
-
// Update package.json
|
|
60
|
-
const cwd = process.cwd()
|
|
61
|
-
const packageJSONPath = path.join(cwd, 'package.json')
|
|
62
|
-
const packageJSON = await fs.readFile(packageJSONPath, 'utf-8')
|
|
63
|
-
/** @type {Record<string,unknown>} */
|
|
64
|
-
const pkg = JSON.parse(packageJSON)
|
|
65
|
-
|
|
66
|
-
// Initialize devDependencies if it doesn't exist
|
|
67
|
-
pkg.devDependencies = pkg.devDependencies || {}
|
|
68
|
-
delete pkg.devDependencies[SOURCE.name]
|
|
69
|
-
|
|
70
|
-
// Get latest version of target package
|
|
71
|
-
const { stdout } = await exec(`npm view ${TARGET.name} dist-tags.latest`)
|
|
72
|
-
.catch(() => ({ stdout: 'latest' }))
|
|
73
|
-
pkg.devDependencies[TARGET.name] = stdout.trim()
|
|
74
|
-
|
|
75
|
-
// Update scripts
|
|
76
|
-
pkg.scripts = {
|
|
77
|
-
...pkg.scripts,
|
|
78
|
-
'lint': 'eslint .',
|
|
79
|
-
'lint:fix': 'eslint --fix .',
|
|
80
|
-
}
|
|
81
|
-
await fs.writeFile(packageJSONPath, JSON.stringify(pkg, null, 2))
|
|
82
|
-
|
|
83
|
-
// Update or create ESLint config
|
|
84
|
-
const configExt = pkg.type === 'module' ? 'js' : 'mjs'
|
|
85
|
-
const configPath = path.join(cwd, `eslint.config.${configExt}`)
|
|
54
|
+
/**
|
|
55
|
+
* Update JSON file with transform function
|
|
56
|
+
* @param {string} filePath - JSON file path
|
|
57
|
+
* @param {(data: any) => any} updateFn - Transform function
|
|
58
|
+
*/
|
|
59
|
+
async function updateJSONFile(filePath, updateFn) {
|
|
60
|
+
const content = await fs.readFile(filePath, 'utf-8')
|
|
61
|
+
const data = JSON.parse(content)
|
|
62
|
+
const updated = updateFn(data)
|
|
63
|
+
await fs.writeFile(filePath, JSON.stringify(updated, null, 2))
|
|
64
|
+
return updated
|
|
65
|
+
}
|
|
86
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Update eslint.config file content
|
|
69
|
+
* @param {string} configPath - Config file path
|
|
70
|
+
* @param {PackageInfo} source - Source package info
|
|
71
|
+
* @param {PackageInfo} target - Target package info
|
|
72
|
+
*/
|
|
73
|
+
async function updateConfigFile(configPath, source, target) {
|
|
87
74
|
let configContent = await fs.readFile(configPath, 'utf-8')
|
|
88
75
|
configContent = configContent
|
|
89
76
|
.replace(
|
|
90
|
-
`import ${
|
|
91
|
-
`import ${
|
|
77
|
+
`import ${source.import} from '${source.name}'`,
|
|
78
|
+
`import ${target.import} from '${target.name}'`,
|
|
92
79
|
)
|
|
93
|
-
.replace(
|
|
80
|
+
.replace(source.import, target.import)
|
|
94
81
|
await fs.writeFile(configPath, configContent)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get latest version of a package
|
|
86
|
+
* @param {string} packageName - Package name
|
|
87
|
+
* @returns {string} Version number
|
|
88
|
+
*/
|
|
89
|
+
function getLatestVersion(packageName) {
|
|
90
|
+
try {
|
|
91
|
+
return execSync(`npm view ${packageName} dist-tags.latest`).toString().trim()
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
console.warn(`Warning: Could not fetch latest version for ${packageName}, using 'latest' tag`)
|
|
95
|
+
return 'latest'
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function main() {
|
|
100
|
+
console.info('Starting ESLint config setup...')
|
|
101
|
+
|
|
102
|
+
if (isRunningFromSourcePackage) {
|
|
103
|
+
console.warn(`Detected execution from ${SOURCE.name}. Skipping to avoid recursive calls.`)
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const pm = await detect()
|
|
109
|
+
|
|
110
|
+
const installCmd = resolveCommand(pm.agent, 'add', ['-D', SOURCE.name])
|
|
111
|
+
await runCommand(installCmd.command, installCmd.args)
|
|
112
|
+
console.info(`Installed ${SOURCE.name}`)
|
|
113
|
+
|
|
114
|
+
const execCmd = resolveCommand(pm.agent, 'execute', [SOURCE.name])
|
|
115
|
+
await runCommand(execCmd.command, execCmd.args)
|
|
116
|
+
|
|
117
|
+
console.info(`Start replacing the config from ${SOURCE.name} to ${TARGET.name}`)
|
|
95
118
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
await spawnAsync(finalInstallCmd.command, finalInstallCmd.args)
|
|
119
|
+
const cwd = process.cwd()
|
|
120
|
+
const packageJSONPath = path.join(cwd, 'package.json')
|
|
99
121
|
|
|
100
|
-
|
|
122
|
+
const pkg = await updateJSONFile(packageJSONPath, (pkgData) => {
|
|
123
|
+
pkgData.devDependencies = pkgData.devDependencies || {}
|
|
124
|
+
delete pkgData.devDependencies[SOURCE.name]
|
|
125
|
+
|
|
126
|
+
TARGET.version = getLatestVersion(TARGET.name)
|
|
127
|
+
pkgData.devDependencies[TARGET.name] = TARGET.version
|
|
128
|
+
|
|
129
|
+
pkgData.scripts = {
|
|
130
|
+
...pkgData.scripts,
|
|
131
|
+
'lint': 'eslint .',
|
|
132
|
+
'lint:fix': 'eslint --fix .',
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return pkgData
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
const configExt = pkg.type === 'module' ? 'js' : 'mjs'
|
|
139
|
+
const configPath = path.join(cwd, `eslint.config.${configExt}`)
|
|
140
|
+
await updateConfigFile(configPath, SOURCE, TARGET)
|
|
141
|
+
|
|
142
|
+
const finalInstallCmd = resolveCommand(pm.agent, 'install')
|
|
143
|
+
await runCommand(finalInstallCmd.command, finalInstallCmd.args)
|
|
144
|
+
|
|
145
|
+
console.info(`Successfully replaced the config from ${SOURCE.name} to ${TARGET.name}`)
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
console.error('Error during ESLint config setup:', error.message)
|
|
149
|
+
process.exit(1)
|
|
150
|
+
}
|
|
101
151
|
}
|
|
102
152
|
|
|
103
153
|
main()
|