@mouse_484/eslint-config 3.4.8 → 3.5.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/bin/cli.js CHANGED
@@ -4,66 +4,100 @@ import fs from 'node:fs/promises'
4
4
  import path from 'node:path'
5
5
  import process from 'node:process'
6
6
  import { promisify } from 'node:util'
7
+ import { resolveCommand } from 'package-manager-detector/commands'
8
+ import { detect } from 'package-manager-detector/detect'
7
9
 
8
10
  const exec = promisify(_exec)
9
11
 
10
- const antfu = '@antfu/eslint-config'
11
- const mouse = '@mouse_484/eslint-config'
12
+ /**
13
+ * @typedef {object} PackageInfo
14
+ * @property {string} name - Package name
15
+ * @property {string} import - Import name in config
16
+ */
12
17
 
13
- // Run the original CLI
14
- const execCLI = () => {
15
- const child = spawn('npx', [antfu], {
16
- stdio: 'inherit',
17
- })
18
+ /** @type {PackageInfo} */
19
+ const SOURCE = {
20
+ name: '@antfu/eslint-config',
21
+ import: 'antfu',
22
+ }
23
+
24
+ /** @type {PackageInfo} */
25
+ const TARGET = {
26
+ name: '@mouse_484/eslint-config',
27
+ import: 'mouse',
28
+ }
29
+
30
+ /**
31
+ *
32
+ * @param {string} command
33
+ * @param {string} args
34
+ * @param {import('node:child_process').SpawnOptions} options
35
+ * @returns {Promise} - Promise Spawn Result
36
+ */
37
+ function spawnAsync(command, args, options = { stdio: 'inherit' }) {
18
38
  return new Promise((resolve, reject) => {
19
- child.on('exit', resolve)
39
+ const child = spawn(command, args, options)
40
+ child.on('close', resolve)
20
41
  child.on('error', reject)
21
42
  })
22
43
  }
23
44
 
24
- await execCLI().catch(console.error).then(console.info)
25
-
26
- console.log(`Start replacing the config from ${antfu} to ${mouse}`)
27
-
28
- // Replace the config
29
- const cwd = process.cwd()
30
-
31
- const packageJSONPath = path.join(cwd, 'package.json')
32
- const packageJSON = await fs.readFile(packageJSONPath, 'utf-8')
33
- /** @type {Record<string,unknown>} */
34
- const pkg = JSON.parse(packageJSON)
35
-
36
- // Replace ESLint config
37
- const eslintConfigPath = path.join(cwd, `eslint.config.${pkg.type === 'module' ? 'js' : 'mjs'}`)
38
- let eslintConfig = await fs.readFile(eslintConfigPath, 'utf-8')
39
-
40
- eslintConfig = eslintConfig.replace(
41
- `import antfu from '${antfu}'`,
42
- `import { mouse } from '${mouse}'`,
43
- )
44
- eslintConfig = eslintConfig.replace(
45
- 'antfu',
46
- 'mouse',
47
- )
48
-
49
- await fs.writeFile(eslintConfigPath, eslintConfig)
50
-
51
- // Replace package.json
52
- delete pkg.devDependencies[antfu]
53
-
54
- const version = (await exec(`npm view ${mouse} dist-tags.latest`).catch(() => {
55
- console.info(`Failed to get the latest version of ${mouse}`)
56
- return { stdout: 'latest' }
57
- })).stdout.trim()
58
-
59
- pkg.devDependencies[mouse] = version
60
-
61
- pkg.scripts = {
62
- ...pkg.scripts,
63
- 'lint': 'eslint .',
64
- 'lint:fix': 'eslint --fix .',
45
+ async function main() {
46
+ const pm = await detect()
47
+
48
+ // Install source config
49
+ const installCmd = resolveCommand(pm.agent, 'add', ['-D', SOURCE.name])
50
+ await spawnAsync(installCmd.command, installCmd.args)
51
+ console.log(`Installed ${SOURCE.name}`)
52
+
53
+ // Run source config setup
54
+ const execCmd = resolveCommand(pm.agent, 'execute', [SOURCE.name])
55
+ await spawnAsync(execCmd.command, execCmd.args)
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}`)
86
+
87
+ let configContent = await fs.readFile(configPath, 'utf-8')
88
+ configContent = configContent
89
+ .replace(
90
+ `import ${SOURCE.import} from '${SOURCE.name}'`,
91
+ `import ${TARGET.import} from '${TARGET.name}'`,
92
+ )
93
+ .replace(SOURCE.import, TARGET.import)
94
+ await fs.writeFile(configPath, configContent)
95
+
96
+ // Install dependencies after all changes
97
+ const finalInstallCmd = resolveCommand(pm.agent, 'install')
98
+ await spawnAsync(finalInstallCmd.command, finalInstallCmd.args)
99
+
100
+ console.log(`Successfully replaced the config from ${SOURCE.name} to ${TARGET.name}`)
65
101
  }
66
102
 
67
- await fs.writeFile(packageJSONPath, JSON.stringify(pkg, null, 2))
68
-
69
- console.log(`Replaced the config from ${antfu} to ${mouse}`)
103
+ main()
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mouse_484/eslint-config",
3
3
  "type": "module",
4
- "version": "3.4.8",
4
+ "version": "3.5.0",
5
5
  "author": "mouse_484",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/mouse484/config/tree/main/packages/eslint",
@@ -19,9 +19,7 @@
19
19
  "types": "src/index.d.ts",
20
20
  "bin": "bin/cli.js",
21
21
  "dependencies": {
22
- "@antfu/eslint-config": "^4.4.0"
23
- },
24
- "devDependencies": {
25
- "@typescript-eslint/eslint-plugin": "^8.26.0"
22
+ "@antfu/eslint-config": "^4.4.0",
23
+ "package-manager-detector": "^0.2.11"
26
24
  }
27
25
  }
package/src/index.d.ts CHANGED
@@ -5,4 +5,5 @@ declare function mouse(
5
5
  ...userConfigs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[]>[]
6
6
  ): Promise<TypedFlatConfigItem>
7
7
 
8
+ export default mouse
8
9
  export { mouse }
package/src/index.js CHANGED
@@ -2,15 +2,10 @@
2
2
  import antfu, {
3
3
  GLOB_ASTRO,
4
4
  GLOB_SRC,
5
- GLOB_TS,
6
- GLOB_TSX,
7
- interopDefault,
8
- renameRules,
9
- resolveSubOptions,
10
5
  } from '@antfu/eslint-config'
11
6
 
12
7
  /** @type {import('@antfu/eslint-config')["antfu"]} */
13
- export async function mouse(options, ...userConfigs) {
8
+ async function mouse(options, ...userConfigs) {
14
9
  options = {
15
10
  lessOpinionated: true,
16
11
  ...options,
@@ -34,40 +29,6 @@ export async function mouse(options, ...userConfigs) {
34
29
  },
35
30
  })
36
31
 
37
- if (options?.typescript) {
38
- const pluginTs = await interopDefault(
39
- import('@typescript-eslint/eslint-plugin'),
40
- )
41
-
42
- /**
43
- * @param {string} key
44
- * @returns {import("eslint").Linter.RulesRecord}
45
- * if tsconfigPath is defined, append '-type-checked' to the key
46
- */
47
- const getRules = (key) => {
48
- const typescriptOptions = resolveSubOptions(options, 'typescript')
49
- const tsconfigPath
50
- = 'tsconfigPath' in typescriptOptions
51
- ? typescriptOptions.tsconfigPath
52
- : undefined
53
-
54
- return renameRules(
55
- pluginTs.configs[tsconfigPath ? `${key}-type-checked` : key]?.rules
56
- ?? {},
57
- { '@typescript-eslint': 'ts' },
58
- )
59
- }
60
-
61
- configs.push({
62
- name: 'mouse/typescript',
63
- files: [GLOB_TS, GLOB_TSX],
64
- rules: {
65
- ...getRules('strict'),
66
- ...getRules('stylistic'),
67
- },
68
- })
69
- }
70
-
71
32
  if (options?.stylistic) {
72
33
  configs.push({
73
34
  name: 'mouse/stylistic',
@@ -93,3 +54,7 @@ export async function mouse(options, ...userConfigs) {
93
54
 
94
55
  return antfu(options, ...configs, ...userConfigs)
95
56
  }
57
+
58
+ export default mouse
59
+ export { mouse }
60
+ export * from '@antfu/eslint-config'