Skip to content

Commit

Permalink
feat: refactor with typescript (#7)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Drop Node.js < 16 support

eggjs/egg#5257
  • Loading branch information
fengmk2 authored Oct 5, 2023
1 parent e4ad971 commit 9e2a9ff
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 74 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
16 changes: 16 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '16, 18, 20'
15 changes: 15 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Release

on:
push:
branches: [ master ]

jobs:
release:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ results
node_modules
npm-debug.log
coverage/
.tshy*
dist/
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

File renamed without changes.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
# node-homedir

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

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

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

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![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)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

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

```bash
$ npm install node-homedir --save
npm install node-homedir
```

## Usage

Commonjs

```js
const homedir = require('node-homedir');
console.log(homedir());
const { homedir } = require('node-homedir');

console.log(homedir());
```

ESM & TypeScript

```ts
import { homedir } from 'node-homedir';

console.log(homedir());
```

## License

[MIT](LICENSE)
17 changes: 0 additions & 17 deletions appveyor.yml

This file was deleted.

58 changes: 42 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "node-homedir",
"version": "1.1.1",
"description": "Get the effective user's homedir.",
"main": "index.js",
"scripts": {
"test": "npm run lint && mocha -r intelli-espower-loader test/*.test.js",
"test-cov": "nyc mocha -r intelli-espower-loader test/*.test.js",
"lint": "eslint test *.js",
"ci": "npm run lint && npm run test-cov"
"test": "npm run lint -- --fix && egg-bin test",
"lint": "eslint src test --ext ts",
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
"contributor": "git-contributor",
"prepublishOnly": "tshy && tshy-after"
},
"repository": {
"type": "git",
Expand All @@ -25,21 +25,47 @@
},
"homepage": "https://github.com/node-modules/node-homedir#readme",
"devDependencies": {
"eslint": "3",
"eslint-config-egg": "3",
"intelli-espower-loader": "^1.0.1",
"mm": "^2.0.0",
"mocha": "*",
"nyc": "^11.2.1",
"power-assert": "^1.4.1"
"@eggjs/tsconfig": "^1.3.3",
"@types/mocha": "^10.0.2",
"@types/node": "^20.8.2",
"egg-bin": "^6.5.2",
"eslint": "^8.50.0",
"eslint-config-egg": "^13.0.0",
"git-contributor": "^2.1.5",
"mm": "^3.3.0",
"tshy": "^1.2.2",
"tshy-after": "^1.0.0",
"typescript": "^5.2.2"
},
"files": [
"index.js"
"dist",
"src"
],
"engines": {
"node": ">=4.0.0"
"node": ">=16.0.0"
},
"ci": {
"version": "4, 6, 8, 10"
}
"version": "16, 18, 20"
},
"tshy": {
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"type": "module",
"types": "./dist/commonjs/index.d.ts"
}
14 changes: 7 additions & 7 deletions index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';
import os from 'node:os';

const os = require('os');

module.exports = () => {
export function homedir(): string {
if (process.env.MOCK_HOME_DIR) return process.env.MOCK_HOME_DIR;

if (typeof os.userInfo === 'function') {
try {
const homedir = os.userInfo().homedir;
if (homedir) return homedir;
} catch (err) {
} catch (err: any) {
if (err.code !== 'ENOENT') throw err;
}
}
Expand All @@ -18,5 +16,7 @@ module.exports = () => {
return os.homedir();
}

return process.env.HOME;
};
return process.env.HOME!;
}

export default homedir;
19 changes: 10 additions & 9 deletions test/index.test.js → test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict';
import { strict as assert } from 'node:assert';
import fs from 'node:fs';
import os from 'node:os';
import mm from 'mm';
import homedir from '../src/index.js';
import { homedir as homedir2 } from '../src/index.js';

const homedir = require('..');
const fs = require('fs');
const mm = require('mm');
const os = require('os');
const assert = require('power-assert');

describe('index.test.js', () => {
describe('index.test.ts', () => {
afterEach(mm.restore);

it('should work', () => {
assert.ok(fs.existsSync(homedir()));
assert.ok(fs.existsSync(homedir2()));
assert.equal(homedir(), homedir2());
});

it('should return homedir when process.env.HOME is not exist', () => {
Expand All @@ -28,7 +29,7 @@ describe('index.test.js', () => {
it('should return homedir when os.userInfo() throw ENOENT error', () => {
mm(os, 'userInfo', () => {
const err = new Error('no such file or directory, uv_os_get_passwd');
err.code = 'ENOENT';
Reflect.set(err, 'code', 'ENOENT');
throw err;
});

Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}

0 comments on commit 9e2a9ff

Please sign in to comment.