From 5da56d9cd260b0cc379a2afda1b91ebceb54f10f Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 14 Oct 2021 19:36:10 +0700 Subject: [PATCH] Require Node.js 12.20 and move to ESM --- .github/workflows/main.yml | 7 ++--- index.d.ts | 54 ++++++++++++++++++-------------------- index.js | 29 ++++++-------------- index.test-d.ts | 8 +++--- license | 2 +- package.json | 14 +++++----- readme.md | 25 +++++++----------- test.js | 9 ++++--- test2.js | 6 ++--- test3.js | 16 ++++++----- 10 files changed, 73 insertions(+), 97 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..441975c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,13 +10,10 @@ jobs: fail-fast: false matrix: node-version: - - 14 - - 12 - - 10 - - 8 + - 16 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 8d6af67..5f07c9b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,35 +1,31 @@ -declare namespace firstRun { - interface Options { - /** - The name used to identify it. Default: `name` field in your package.json - */ - readonly name?: string; - } -} - -declare const firstRun: { +export interface Options { /** - Check if it's the first time the process is run. + The name used to identify it. + + Usually, you would fetch the `name` field from package.json. + */ + readonly name: string; +} - @example - ``` - // x.js - import firstRun = require('first-run'); +/** +Check if it's the first time the process is run. - console.log(firstRun()); +@example +``` +// x.js +import isFirstRun from 'first-run'; - // $ node x.js - // true - // $ node x.js - // false - ``` - */ - (options?: firstRun.Options): boolean; +console.log(isFirstRun()); - /** - Clear the state. - */ - clear(options?: firstRun.Options): void; -}; +// $ node x.js +// true +// $ node x.js +// false +``` +*/ +export default function isFirstRun(options: Options): boolean; -export = firstRun; +/** +Clear the state. +*/ +export function clearFirstRun(options: Options): void; diff --git a/index.js b/index.js index 5add118..25d428c 100644 --- a/index.js +++ b/index.js @@ -1,37 +1,24 @@ -'use strict'; -const path = require('path'); -const Configstore = require('configstore'); -const readPkgUp = require('read-pkg-up'); - -function getConfigStore(options = {}) { - let {name} = options; +import Configstore from 'configstore'; +function getConfigStore({name} = {}) { if (!name) { - delete require.cache[__filename]; - name = readPkgUp.sync({cwd: path.dirname(module.parent.filename)}).pkg.name; - } - - if (!name) { - throw new Error('Couldn\'t infer the package name. Please specify it in the options.'); + throw new Error('Please specify the `name` option.'); } return new Configstore(`first-run_${name}`, {firstRun: true}); } -function firstRun(options) { +export default function isFirstRun(options) { const configStore = getConfigStore(options); - const firstRun = configStore.get('firstRun'); + const isFirstRun = configStore.get('firstRun'); - if (firstRun === true) { + if (isFirstRun === true) { configStore.set('firstRun', false); } - return firstRun; + return isFirstRun; } -function clear(options) { +export function clearFirstRun(options) { getConfigStore(options).set('firstRun', true); } - -module.exports = firstRun; -module.exports.clear = clear; diff --git a/index.test-d.ts b/index.test-d.ts index 0918b6c..a4b87d8 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,8 +1,6 @@ import {expectType} from 'tsd'; -import firstRun = require('.'); +import isFirstRun, {clearFirstRun} from './index.js'; -expectType(firstRun()); -expectType(firstRun({name: 'foo'})); +expectType(isFirstRun({name: 'foo'})); -firstRun.clear(); -firstRun.clear({name: 'foo'}); +clearFirstRun({name: 'foo'}); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index b8e27a0..9305550 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "description": "Check if it's the first time the process is run", "license": "MIT", "repository": "sindresorhus/first-run", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && node test.js && node test2.js && node test3.js && tsd" @@ -29,11 +32,10 @@ "start" ], "dependencies": { - "configstore": "^4.0.0", - "read-pkg-up": "^5.0.0" + "configstore": "^6.0.0" }, "devDependencies": { - "tsd": "^0.7.2", - "xo": "^0.24.0" + "tsd": "^0.18.0", + "xo": "^0.45.0" } } diff --git a/readme.md b/readme.md index 2bf8408..2646b37 100644 --- a/readme.md +++ b/readme.md @@ -4,21 +4,19 @@ Can be used to greet the user the first time they use your CLI app, show usage tip, initialize something, etc. - ## Install +```sh +npm install first-run ``` -$ npm install first-run -``` - ## Usage ```js // x.js -const firstRun = require('first-run'); +import isFirstRun from 'first-run'; -console.log(firstRun()); +console.log(isFirstRun({name: 'x'})); ``` ``` @@ -28,27 +26,22 @@ $ node x.js false ``` - ## API -### firstRun([options]) +### isFirstRun(options) -### firstRun.clear([options]) +### clearFirstRun(options) Clear the state. #### options -Type: `Object` +Type: `object` ##### name -Type: `string`
-Default: `name` field in your package.json +Type: `string` The name used to identify it. - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) +Usually, you would fetch the `name` field from package.json. diff --git a/test.js b/test.js index d2b67a0..6b7362d 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,8 @@ -'use strict'; -const Configstore = require('configstore'); -const firstRun = require('.'); +import process from 'node:process'; +import Configstore from 'configstore'; +import isFirstRun from './index.js'; (new Configstore('first-run_first-run')).clear(); + // eslint-disable-next-line unicorn/no-process-exit -process.exit(firstRun() ? 0 : 1); +process.exit(isFirstRun({name: 'first-run'}) ? 0 : 1); diff --git a/test2.js b/test2.js index ac63822..dd37ab5 100644 --- a/test2.js +++ b/test2.js @@ -1,5 +1,5 @@ -'use strict'; -const firstRun = require('.'); +import process from 'node:process'; +import isFirstRun from './index.js'; // eslint-disable-next-line unicorn/no-process-exit -process.exit(firstRun() ? 1 : 0); +process.exit(isFirstRun({name: 'first-run'}) ? 1 : 0); diff --git a/test3.js b/test3.js index 11ddc2c..d1d231e 100644 --- a/test3.js +++ b/test3.js @@ -1,10 +1,12 @@ -'use strict'; -const Configstore = require('configstore'); -const firstRun = require('.'); +import process from 'node:process'; +import Configstore from 'configstore'; +import isFirstRun, {clearFirstRun} from './index.js'; -(new Configstore('first-run_first-run')).clear(); -const shouldBeTrue = firstRun(); -firstRun.clear(); -const shouldBeTrueAgain = firstRun(); +const name = 'first-run'; + +(new Configstore(`first-run_${name}`)).clear(); +const shouldBeTrue = isFirstRun({name}); +clearFirstRun({name}); +const shouldBeTrueAgain = isFirstRun({name}); // eslint-disable-next-line unicorn/no-process-exit process.exit(shouldBeTrue && shouldBeTrueAgain ? 0 : 1);