Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 14, 2021
1 parent c5834fc commit 5da56d9
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 97 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 25 additions & 29 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 8 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 3 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {expectType} from 'tsd';
import firstRun = require('.');
import isFirstRun, {clearFirstRun} from './index.js';

expectType<boolean>(firstRun());
expectType<boolean>(firstRun({name: 'foo'}));
expectType<boolean>(isFirstRun({name: 'foo'}));

firstRun.clear();
firstRun.clear({name: 'foo'});
clearFirstRun({name: 'foo'});
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
25 changes: 9 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'}));
```

```
Expand All @@ -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`<br>
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.
9 changes: 5 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 3 additions & 3 deletions test2.js
Original file line number Diff line number Diff line change
@@ -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);
16 changes: 9 additions & 7 deletions test3.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 5da56d9

Please sign in to comment.