@ansstory/hias 1.0.3 → 1.0.5

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.

Potentially problematic release.


This version of @ansstory/hias might be problematic. Click here for more details.

@@ -1,21 +1,28 @@
1
- const path = require('path')
2
- const ejs = require('ejs')
3
-
4
- function compileEjs(tempName, data) {
5
- return new Promise((resolve, reject) => {
6
- const tempPath = `../template/${tempName}`
7
- const absolutePath = path.resolve(__dirname, tempPath)
8
-
9
- ejs.renderFile(absolutePath, data, (err, result) => {
10
- if (err) {
11
- console.log('error:', err)
12
- reject(err)
13
- return
14
- }
15
-
16
- resolve(result)
17
- })
18
- })
19
- }
20
-
21
- module.exports = compileEjs
1
+ // EJS 模板编译工具 - 将 EJS 模板编译成最终的代码文件
2
+
3
+ const path = require('path')
4
+ const ejs = require('ejs')
5
+
6
+ // 编译 EJS 模板文件的函数,返回 Promise
7
+ function compileEjs(tempName, data) {
8
+ return new Promise((resolve, reject) => {
9
+ // 构建模板文件的相对路径
10
+ const tempPath = `../template/${tempName}`
11
+ // 将相对路径转换为绝对路径
12
+ const absolutePath = path.resolve(__dirname, tempPath)
13
+
14
+ // 使用 EJS 引擎渲染模板文件
15
+ ejs.renderFile(absolutePath, data, (err, result) => {
16
+ if (err) {
17
+ console.log('error:', err)
18
+ reject(err)
19
+ return
20
+ }
21
+
22
+ // 返回编译后的内容
23
+ resolve(result)
24
+ })
25
+ })
26
+ }
27
+
28
+ module.exports = compileEjs
@@ -1,10 +1,16 @@
1
- const fs = require('fs')
2
- const path = require('path')
3
-
4
- async function writeFile(filePath, content) {
5
- const dir = path.dirname(filePath)
6
- await fs.promises.mkdir(dir, { recursive: true })
7
- return fs.promises.writeFile(filePath, content)
8
- }
9
-
10
- module.exports = writeFile
1
+ // 文件写入工具 - 处理文件和目录的创建及内容写入
2
+
3
+ const fs = require('fs')
4
+ const path = require('path')
5
+
6
+ // 异步写入文件函数,自动创建必要的目录结构
7
+ async function writeFile(filePath, content) {
8
+ // 获取文件所在的目录
9
+ const dir = path.dirname(filePath)
10
+ // 递归创建目录结构(如果目录不存在)
11
+ await fs.promises.mkdir(dir, { recursive: true })
12
+ // 将内容写入文件
13
+ return fs.promises.writeFile(filePath, content)
14
+ }
15
+
16
+ module.exports = writeFile
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@ansstory/hias",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "private": false,
5
- "description": "创建项目模板",
5
+ "description": "个人自用脚手架内置个人常用功能",
6
6
  "author": "AnsStory story <story0809@163.com>",
7
7
  "main": "index.js",
8
8
  "bin": {
@@ -12,7 +12,10 @@
12
12
  "hias ansstory"
13
13
  ],
14
14
  "scripts": {
15
- "test": "echo \"Error: no test specified\" && exit 1"
15
+ "test": "node --test",
16
+ "addv": "npm version patch",
17
+ "asp": "npm publish --access public",
18
+ "prepublish": "npm run addv && npm run asp"
16
19
  },
17
20
  "license": "MIT",
18
21
  "dependencies": {
@@ -20,6 +23,7 @@
20
23
  "commander": "^14.0.0",
21
24
  "download-git-repo": "^3.0.2",
22
25
  "ejs": "^3.1.10",
26
+ "i18next": "^26.3.0",
23
27
  "inquirer": "^8.2.6",
24
28
  "ora": "^5.4.1"
25
29
  }
@@ -0,0 +1,88 @@
1
+ const test = require('node:test')
2
+ const assert = require('node:assert/strict')
3
+
4
+ const { closePort, closePorts, normalizePort, parsePortInputs, parsePidsFromWindowsNetstat } = require('../lib/core/close-port')
5
+
6
+ test('normalizePort accepts valid numeric port strings', () => {
7
+ assert.equal(normalizePort('3000'), 3000)
8
+ })
9
+
10
+ test('normalizePort rejects invalid ports', () => {
11
+ assert.throws(() => normalizePort('abc'), /Invalid port/)
12
+ assert.throws(() => normalizePort('0'), /Invalid port/)
13
+ assert.throws(() => normalizePort('65536'), /Invalid port/)
14
+ })
15
+
16
+ test('parsePortInputs supports space-separated and comma-separated ports', () => {
17
+ assert.deepEqual(parsePortInputs(['8076', '8077,8078']), [8076, 8077, 8078])
18
+ })
19
+
20
+ test('parsePortInputs removes duplicate ports', () => {
21
+ assert.deepEqual(parsePortInputs(['8076,8077', '8076']), [8076, 8077])
22
+ })
23
+
24
+ test('parsePidsFromWindowsNetstat returns unique PIDs listening on the requested port', () => {
25
+ const output = [
26
+ ' TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING 1234',
27
+ ' TCP [::]:3000 [::]:0 LISTENING 1234',
28
+ ' TCP 127.0.0.1:3001 0.0.0.0:0 LISTENING 7777',
29
+ ' TCP 127.0.0.1:3000 127.0.0.1:54511 ESTABLISHED 2222',
30
+ ' UDP 0.0.0.0:3000 *:* 5678',
31
+ ].join('\n')
32
+
33
+ assert.deepEqual(parsePidsFromWindowsNetstat(output, 3000), ['1234', '5678'])
34
+ })
35
+
36
+ test('closePort kills every process occupying the port', async () => {
37
+ const commands = []
38
+ const exec = async (command) => {
39
+ commands.push(command)
40
+ if (command.startsWith('netstat')) {
41
+ return {
42
+ stdout: [
43
+ ' TCP 0.0.0.0:5173 0.0.0.0:0 LISTENING 1001',
44
+ ' UDP 0.0.0.0:5173 *:* 1002',
45
+ ].join('\n'),
46
+ }
47
+ }
48
+ return { stdout: '' }
49
+ }
50
+
51
+ const result = await closePort('5173', { exec, platform: 'win32' })
52
+
53
+ assert.deepEqual(result, { port: 5173, pids: ['1001', '1002'], killed: ['1001', '1002'] })
54
+ assert.deepEqual(commands, ['netstat -ano', 'taskkill /F /PID 1001', 'taskkill /F /PID 1002'])
55
+ })
56
+
57
+ test('closePorts closes each requested port', async () => {
58
+ const commands = []
59
+ const exec = async (command) => {
60
+ commands.push(command)
61
+ if (command === 'netstat -ano') {
62
+ return {
63
+ stdout: [
64
+ ' TCP 0.0.0.0:8076 0.0.0.0:0 LISTENING 9001',
65
+ ' TCP 0.0.0.0:8077 0.0.0.0:0 LISTENING 9002',
66
+ ].join('\n'),
67
+ }
68
+ }
69
+ return { stdout: '' }
70
+ }
71
+
72
+ const results = await closePorts(['8076', '8077'], { exec, platform: 'win32' })
73
+
74
+ assert.deepEqual(results, [
75
+ { port: 8076, pids: ['9001'], killed: ['9001'] },
76
+ { port: 8077, pids: ['9002'], killed: ['9002'] },
77
+ ])
78
+ assert.deepEqual(commands, ['netstat -ano', 'taskkill /F /PID 9001', 'netstat -ano', 'taskkill /F /PID 9002'])
79
+ })
80
+
81
+ test('closePort returns an empty result when no process uses the port', async () => {
82
+ const result = await closePort('5173', {
83
+ exec: async () => ({ stdout: '' }),
84
+ platform: 'win32',
85
+ })
86
+
87
+ assert.deepEqual(result, { port: 5173, pids: [], killed: [] })
88
+ })
@@ -1,13 +0,0 @@
1
- <script setup>
2
-
3
- </script>
4
-
5
- <template>
6
- <div class="demo">
7
- demo
8
- </div>
9
- </template>
10
-
11
- <style lang="scss" scoped>
12
- .demo {}
13
- </style>
@@ -1,13 +0,0 @@
1
- <script setup>
2
-
3
- </script>
4
-
5
- <template>
6
- <div class="text">
7
- text
8
- </div>
9
- </template>
10
-
11
- <style lang="scss" scoped>
12
- .text {}
13
- </style>