@ansstory/hias 1.0.6 → 1.0.7

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.
@@ -13,6 +13,7 @@ const {
13
13
  const { handleLanguageAction } = require('./lang')
14
14
  const { handleClosePortAction } = require('./close-port')
15
15
  const { handleTranslateFileAction, handleTranslateFolderAction, handleSettingAction, handleGlobalSettingAction, handleRollbackAction, handleGitignoreAction } = require('./translate')
16
+ const { clearConfigDirectory, uninstallPackage } = require('../../scripts/cleanup-config')
16
17
 
17
18
  // 注册所有命令行指令
18
19
  const beginCommander = function (program) {
@@ -86,7 +87,19 @@ const beginCommander = function (program) {
86
87
  program.command('setting').description(t('commands.setting')).action(handleSettingAction)
87
88
 
88
89
  // 生成全局翻译配置文件
89
- program.command('globalsetting').description(t('commands.globalSetting')).action(handleGlobalSettingAction)
90
+ program.command('global-setting').description(t('commands.globalSetting')).action(handleGlobalSettingAction)
91
+
92
+ // 清理全局配置目录
93
+ program.command('clear-config').description(t('commands.clearConfig')).action(() => clearConfigDirectory())
94
+
95
+ // 清理配置并卸载全局包
96
+ program.command('uninstall').option('-g, --global', 'uninstall global @ansstory/hias package').description(t('commands.uninstall')).action((options) => {
97
+ if (!options.global) {
98
+ console.log(chalk.yellow.bold('Usage: hias uninstall -g'))
99
+ return
100
+ }
101
+ uninstallPackage()
102
+ })
90
103
 
91
104
  // 将 .hias 添加到 .gitignore
92
105
  program.command('gitignore').description(t('commands.gitignore')).action(handleGitignoreAction)
@@ -12,6 +12,8 @@
12
12
  "translateFolder": "recursively translate all supported files in a folder. For example: hias tfo src/views views",
13
13
  "setting": "generate .hias/setting.json configuration file",
14
14
  "globalSetting": "generate global ~/.hias-cli/config.json configuration file",
15
+ "clearConfig": "remove global ~/.hias-cli configuration directory",
16
+ "uninstall": "remove global config and uninstall @ansstory/hias",
15
17
  "rollback": "restore files from last translation and clean up generated output",
16
18
  "gitignore": "add .hias directory to .gitignore"
17
19
  },
@@ -12,6 +12,8 @@
12
12
  "translateFolder": "递归翻译文件夹内所有支持的文件。示例: hias tfo src/views views",
13
13
  "setting": "生成 .hias/setting.json 配置文件",
14
14
  "globalSetting": "生成全局 ~/.hias-cli/config.json 配置文件",
15
+ "clearConfig": "删除全局 ~/.hias-cli 配置目录",
16
+ "uninstall": "删除全局配置并卸载 @ansstory/hias",
15
17
  "rollback": "恢复上一次翻译的原文件并清理生成的输出",
16
18
  "gitignore": "将 .hias 目录添加到 .gitignore"
17
19
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ansstory/hias",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "private": false,
5
5
  "description": "个人自用脚手架内置个人常用功能",
6
6
  "author": "AnsStory story <story0809@163.com>",
@@ -0,0 +1,70 @@
1
+ const fs = require('fs')
2
+ const os = require('os')
3
+ const path = require('path')
4
+ const { spawnSync } = require('child_process')
5
+
6
+ function getConfigDirectory(homeDir = os.homedir()) {
7
+ return path.join(homeDir, '.hias-cli')
8
+ }
9
+
10
+ function clearConfigDirectory({ homeDir = os.homedir(), logger = console, dryRun = false } = {}) {
11
+ const configDir = getConfigDirectory(homeDir)
12
+
13
+ if (dryRun) {
14
+ if (logger) {
15
+ logger.log(`Would remove hias config directory: ${configDir}`)
16
+ }
17
+ return configDir
18
+ }
19
+
20
+ fs.rmSync(configDir, { recursive: true, force: true })
21
+
22
+ if (logger) {
23
+ logger.log(`Removed hias config directory: ${configDir}`)
24
+ }
25
+ return configDir
26
+ }
27
+
28
+ function uninstallPackage({ homeDir = os.homedir(), logger = console, runCommand = spawnSync } = {}) {
29
+ clearConfigDirectory({ homeDir, logger })
30
+
31
+ const result = runCommand('npm', ['uninstall', '@ansstory/hias', '-g'], {
32
+ stdio: 'inherit',
33
+ shell: true,
34
+ })
35
+
36
+ if (typeof result.status === 'number' && result.status !== 0) {
37
+ process.exitCode = result.status
38
+ }
39
+
40
+ return result
41
+ }
42
+
43
+ function printUsage(logger = console) {
44
+ logger.log('Usage: hias clear-config')
45
+ logger.log(' npm run clear-config')
46
+ logger.log('')
47
+ logger.log('Options:')
48
+ logger.log(' --dry-run Print the config directory without deleting it')
49
+ logger.log(' -h, --help Show this help')
50
+ }
51
+
52
+ if (require.main === module) {
53
+ try {
54
+ const args = process.argv.slice(2)
55
+ if (args.includes('--help') || args.includes('-h')) {
56
+ printUsage()
57
+ } else {
58
+ clearConfigDirectory({ dryRun: args.includes('--dry-run') })
59
+ }
60
+ } catch (error) {
61
+ console.warn(`Failed to remove hias config directory: ${error.message}`)
62
+ }
63
+ }
64
+
65
+ module.exports = {
66
+ clearConfigDirectory,
67
+ getConfigDirectory,
68
+ uninstallPackage,
69
+ printUsage,
70
+ }
@@ -0,0 +1,72 @@
1
+ const test = require('node:test')
2
+ const assert = require('node:assert/strict')
3
+ const fs = require('fs')
4
+ const os = require('os')
5
+ const path = require('path')
6
+
7
+ const { clearConfigDirectory, getConfigDirectory, uninstallPackage } = require('../scripts/cleanup-config')
8
+
9
+ test('clearConfigDirectory removes the hias cli config directory', () => {
10
+ const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'hias-clear-'))
11
+ const configDir = getConfigDirectory(tempHome)
12
+
13
+ fs.mkdirSync(configDir, { recursive: true })
14
+ fs.writeFileSync(path.join(configDir, 'config.json'), JSON.stringify({ language: 'zh-CN' }))
15
+
16
+ assert.equal(fs.existsSync(configDir), true)
17
+
18
+ clearConfigDirectory({ homeDir: tempHome, logger: null })
19
+
20
+ assert.equal(fs.existsSync(configDir), false)
21
+ })
22
+
23
+ test('clearConfigDirectory succeeds when the config directory is absent', () => {
24
+ const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'hias-clear-missing-'))
25
+ const configDir = getConfigDirectory(tempHome)
26
+
27
+ assert.equal(fs.existsSync(configDir), false)
28
+
29
+ clearConfigDirectory({ homeDir: tempHome, logger: null })
30
+
31
+ assert.equal(fs.existsSync(configDir), false)
32
+ })
33
+
34
+ test('clearConfigDirectory dry run leaves the config directory in place', () => {
35
+ const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'hias-clear-dry-run-'))
36
+ const configDir = getConfigDirectory(tempHome)
37
+
38
+ fs.mkdirSync(configDir, { recursive: true })
39
+ fs.writeFileSync(path.join(configDir, 'config.json'), JSON.stringify({ language: 'en' }))
40
+
41
+ const result = clearConfigDirectory({ homeDir: tempHome, logger: null, dryRun: true })
42
+
43
+ assert.equal(result, configDir)
44
+ assert.equal(fs.existsSync(configDir), true)
45
+ })
46
+
47
+ test('uninstallPackage clears config before uninstalling the global package', () => {
48
+ const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'hias-uninstall-'))
49
+ const configDir = getConfigDirectory(tempHome)
50
+ const calls = []
51
+
52
+ fs.mkdirSync(configDir, { recursive: true })
53
+ fs.writeFileSync(path.join(configDir, 'config.json'), JSON.stringify({ language: 'en' }))
54
+
55
+ uninstallPackage({
56
+ homeDir: tempHome,
57
+ logger: null,
58
+ runCommand: (command, args, options) => {
59
+ calls.push({ command, args, options })
60
+ return { status: 0 }
61
+ },
62
+ })
63
+
64
+ assert.equal(fs.existsSync(configDir), false)
65
+ assert.deepEqual(calls, [
66
+ {
67
+ command: 'npm',
68
+ args: ['uninstall', '@ansstory/hias', '-g'],
69
+ options: { stdio: 'inherit', shell: true },
70
+ },
71
+ ])
72
+ })
@@ -0,0 +1,14 @@
1
+ const test = require('node:test')
2
+ const assert = require('node:assert/strict')
3
+ const { spawnSync } = require('child_process')
4
+ const path = require('path')
5
+
6
+ test('help lists global-setting instead of globalsetting', () => {
7
+ const result = spawnSync(process.execPath, [path.join(__dirname, '..', 'lib', 'index.js'), '--help'], {
8
+ encoding: 'utf8',
9
+ })
10
+
11
+ assert.equal(result.status, 0)
12
+ assert.match(result.stdout, /\bglobal-setting\b/)
13
+ assert.doesNotMatch(result.stdout, /\bglobalsetting\b/)
14
+ })