diff --git a/.eslintrc b/.eslintrc index c799fe5..9bcdb46 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "eslint-config-egg" + "extends": [ + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" + ] } diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..e807d60 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -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' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..a4e1158 --- /dev/null +++ b/.github/workflows/release.yml @@ -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 diff --git a/.gitignore b/.gitignore index 8280239..41aee8d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ results node_modules npm-debug.log coverage/ +.tshy* +dist/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9edb713..0000000 --- a/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -sudo: false -language: node_js -node_js: - - '4' - - '6' - - '8' - - '10' -install: - - npm i npminstall && npminstall -script: - - npm run ci -after_script: - - npminstall codecov && codecov diff --git a/History.md b/CHANGELOG.md similarity index 100% rename from History.md rename to CHANGELOG.md diff --git a/README.md b/README.md index ba9219f..1ae379f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 2ef3e61..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,17 +0,0 @@ -environment: - matrix: - - nodejs_version: '4' - - nodejs_version: '6' - - nodejs_version: '8' - - nodejs_version: '10' - -install: - - ps: Install-Product node $env:nodejs_version - - npm i npminstall && node_modules\.bin\npminstall - -test_script: - - node --version - - npm --version - - npm run ci - -build: off diff --git a/package.json b/package.json index c9bf7aa..afb3aed 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } diff --git a/index.js b/src/index.ts similarity index 70% rename from index.js rename to src/index.ts index 9e8ccd9..6eb476e 100644 --- a/index.js +++ b/src/index.ts @@ -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; } } @@ -18,5 +16,7 @@ module.exports = () => { return os.homedir(); } - return process.env.HOME; -}; + return process.env.HOME!; +} + +export default homedir; diff --git a/test/index.test.js b/test/index.test.ts similarity index 65% rename from test/index.test.js rename to test/index.test.ts index 54387a5..4c0a6dc 100644 --- a/test/index.test.js +++ b/test/index.test.ts @@ -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', () => { @@ -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; }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ff41b73 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext" + } +}