Skip to content

Commit 9e2a9ff

Browse files
authored
feat: refactor with typescript (#7)
BREAKING CHANGE: Drop Node.js < 16 support eggjs/egg#5257
1 parent e4ad971 commit 9e2a9ff

12 files changed

Lines changed: 124 additions & 74 deletions

File tree

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "eslint-config-egg"
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
36
}

.github/workflows/nodejs.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
7+
pull_request:
8+
branches: [ master ]
9+
10+
jobs:
11+
Job:
12+
name: Node.js
13+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
14+
with:
15+
os: 'ubuntu-latest, macos-latest'
16+
version: '16, 18, 20'

.github/workflows/release.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
7+
jobs:
8+
release:
9+
name: Node.js
10+
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
11+
secrets:
12+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
13+
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
14+
with:
15+
checkTest: false

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ results
1313
node_modules
1414
npm-debug.log
1515
coverage/
16+
.tshy*
17+
dist/

.travis.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.
File renamed without changes.

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
# node-homedir
22

3-
Get the effective user's homedir, if node.js >= v6.0.0 .
3+
Get the effective user's homedir, if Node.js >= v16.0.0 .
44

5-
Aways return the homedir of the current executor, even execute with `sudo -u [user] node app.js` without `-i`.
5+
Always return the homedir of the current executor, even execute with `sudo -u [user] node app.js` without `-i`.
66

77
[Reference Issue](https://github.com/nodejs/node/issues/5582)
88

99
[![NPM version][npm-image]][npm-url]
10-
[![build status][travis-image]][travis-url]
10+
[![CI](https://github.com/node-modules/node-homedir/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/node-homedir/actions/workflows/nodejs.yml)
1111
[![Test coverage][codecov-image]][codecov-url]
12-
[![David deps][david-image]][david-url]
1312
[![Known Vulnerabilities][snyk-image]][snyk-url]
1413
[![npm download][download-image]][download-url]
1514

1615
[npm-image]: https://img.shields.io/npm/v/node-homedir.svg?style=flat-square
1716
[npm-url]: https://npmjs.org/package/node-homedir
18-
[travis-image]: https://img.shields.io/travis/node-modules/node-homedir.svg?style=flat-square
19-
[travis-url]: https://travis-ci.org/node-modules/node-homedir
2017
[codecov-image]: https://codecov.io/github/node-modules/node-homedir/coverage.svg?branch=master
2118
[codecov-url]: https://codecov.io/github/node-modules/node-homedir?branch=master
22-
[david-image]: https://img.shields.io/david/node-modules/node-homedir.svg?style=flat-square
23-
[david-url]: https://david-dm.org/node-modules/node-homedir
2419
[snyk-image]: https://snyk.io/test/npm/node-homedir/badge.svg?style=flat-square
2520
[snyk-url]: https://snyk.io/test/npm/node-homedir
2621
[download-image]: https://img.shields.io/npm/dm/node-homedir.svg?style=flat-square
@@ -29,15 +24,27 @@ Aways return the homedir of the current executor, even execute with `sudo -u [
2924
## Installation
3025

3126
```bash
32-
$ npm install node-homedir --save
27+
npm install node-homedir
3328
```
3429

3530
## Usage
3631

32+
Commonjs
33+
3734
```js
38-
const homedir = require('node-homedir');
39-
console.log(homedir());
35+
const { homedir } = require('node-homedir');
36+
37+
console.log(homedir());
38+
```
39+
40+
ESM & TypeScript
41+
42+
```ts
43+
import { homedir } from 'node-homedir';
44+
45+
console.log(homedir());
4046
```
4147

4248
## License
49+
4350
[MIT](LICENSE)

appveyor.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"name": "node-homedir",
33
"version": "1.1.1",
44
"description": "Get the effective user's homedir.",
5-
"main": "index.js",
65
"scripts": {
7-
"test": "npm run lint && mocha -r intelli-espower-loader test/*.test.js",
8-
"test-cov": "nyc mocha -r intelli-espower-loader test/*.test.js",
9-
"lint": "eslint test *.js",
10-
"ci": "npm run lint && npm run test-cov"
6+
"test": "npm run lint -- --fix && egg-bin test",
7+
"lint": "eslint src test --ext ts",
8+
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
9+
"contributor": "git-contributor",
10+
"prepublishOnly": "tshy && tshy-after"
1111
},
1212
"repository": {
1313
"type": "git",
@@ -25,21 +25,47 @@
2525
},
2626
"homepage": "https://github.com/node-modules/node-homedir#readme",
2727
"devDependencies": {
28-
"eslint": "3",
29-
"eslint-config-egg": "3",
30-
"intelli-espower-loader": "^1.0.1",
31-
"mm": "^2.0.0",
32-
"mocha": "*",
33-
"nyc": "^11.2.1",
34-
"power-assert": "^1.4.1"
28+
"@eggjs/tsconfig": "^1.3.3",
29+
"@types/mocha": "^10.0.2",
30+
"@types/node": "^20.8.2",
31+
"egg-bin": "^6.5.2",
32+
"eslint": "^8.50.0",
33+
"eslint-config-egg": "^13.0.0",
34+
"git-contributor": "^2.1.5",
35+
"mm": "^3.3.0",
36+
"tshy": "^1.2.2",
37+
"tshy-after": "^1.0.0",
38+
"typescript": "^5.2.2"
3539
},
3640
"files": [
37-
"index.js"
41+
"dist",
42+
"src"
3843
],
3944
"engines": {
40-
"node": ">=4.0.0"
45+
"node": ">=16.0.0"
4146
},
4247
"ci": {
43-
"version": "4, 6, 8, 10"
44-
}
48+
"version": "16, 18, 20"
49+
},
50+
"tshy": {
51+
"exports": {
52+
"./package.json": "./package.json",
53+
".": "./src/index.ts"
54+
}
55+
},
56+
"exports": {
57+
"./package.json": "./package.json",
58+
".": {
59+
"import": {
60+
"types": "./dist/esm/index.d.ts",
61+
"default": "./dist/esm/index.js"
62+
},
63+
"require": {
64+
"types": "./dist/commonjs/index.d.ts",
65+
"default": "./dist/commonjs/index.js"
66+
}
67+
}
68+
},
69+
"type": "module",
70+
"types": "./dist/commonjs/index.d.ts"
4571
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict';
1+
import os from 'node:os';
22

3-
const os = require('os');
4-
5-
module.exports = () => {
3+
export function homedir(): string {
64
if (process.env.MOCK_HOME_DIR) return process.env.MOCK_HOME_DIR;
75

86
if (typeof os.userInfo === 'function') {
97
try {
108
const homedir = os.userInfo().homedir;
119
if (homedir) return homedir;
12-
} catch (err) {
10+
} catch (err: any) {
1311
if (err.code !== 'ENOENT') throw err;
1412
}
1513
}
@@ -18,5 +16,7 @@ module.exports = () => {
1816
return os.homedir();
1917
}
2018

21-
return process.env.HOME;
22-
};
19+
return process.env.HOME!;
20+
}
21+
22+
export default homedir;

0 commit comments

Comments
 (0)