Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 15, 2019
1 parent be01f01 commit a401044
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 93 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Expand Up @@ -7,9 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitattributes
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
6 changes: 2 additions & 4 deletions .travis.yml
@@ -1,6 +1,4 @@
sudo: false
language: node_js
node_js:
- 'iojs'
- '0.12'
- '0.10'
- '10'
- '8'
35 changes: 35 additions & 0 deletions index.d.ts
@@ -0,0 +1,35 @@
declare namespace firstRun {
interface Options {
/**
The name used to identify it. Default: `name` field in your package.json
*/
readonly name?: string;
}
}

declare const firstRun: {
/**
Check if it's the first time the process is run.
@example
```
// x.js
import firstRun = require('first-run');
console.log(firstRun());
// $ node x.js
// true
// $ node x.js
// false
```
*/
(options?: firstRun.Options): boolean;

/**
Clear the state.
*/
clear(options?: firstRun.Options): void;
};

export = firstRun;
30 changes: 12 additions & 18 deletions index.js
@@ -1,12 +1,10 @@
'use strict';
var path = require('path');
var Configstore = require('configstore');
var readPkgUp = require('read-pkg-up');
const path = require('path');
const Configstore = require('configstore');
const readPkgUp = require('read-pkg-up');

function getConf(opts) {
opts = opts || {};

var name = opts.name;
function getConfigStore(options = {}) {
let {name} = options;

if (!name) {
delete require.cache[__filename];
Expand All @@ -17,26 +15,22 @@ function getConf(opts) {
throw new Error('Couldn\'t infer the package name. Please specify it in the options.');
}

return new Configstore('first-run_' + name, {firstRun: true});
return new Configstore(`first-run_${name}`, {firstRun: true});
}

function firstRun(opts) {
var firstRun;
var conf = getConf(opts);

if (firstRun === undefined) {
firstRun = conf.get('firstRun');
}
function firstRun(options) {
const configStore = getConfigStore(options);
const firstRun = configStore.get('firstRun');

if (firstRun === true) {
conf.set('firstRun', false);
configStore.set('firstRun', false);
}

return firstRun;
}

function clear(opts) {
getConf(opts).set('firstRun', true);
function clear(options) {
getConfigStore(options).set('firstRun', true);
}

module.exports = firstRun;
Expand Down
8 changes: 8 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,8 @@
import {expectType} from 'tsd';
import firstRun = require('.');

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

firstRun.clear();
firstRun.clear({name: 'foo'});
20 changes: 4 additions & 16 deletions license
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

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

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
72 changes: 37 additions & 35 deletions package.json
@@ -1,37 +1,39 @@
{
"name": "first-run",
"version": "1.2.0",
"description": "Check if it's the first time the process is run",
"license": "MIT",
"repository": "sindresorhus/first-run",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && node test.js && node test2.js && node test3.js"
},
"files": [
"index.js"
],
"keywords": [
"first",
"init",
"initial",
"run",
"time",
"process",
"start"
],
"dependencies": {
"configstore": "^2.0.0",
"read-pkg-up": "^1.0.1"
},
"devDependencies": {
"xo": "*"
}
"name": "first-run",
"version": "1.2.0",
"description": "Check if it's the first time the process is run",
"license": "MIT",
"repository": "sindresorhus/first-run",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && node test.js && node test2.js && node test3.js && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"first",
"init",
"initial",
"run",
"time",
"process",
"start"
],
"dependencies": {
"configstore": "^4.0.0",
"read-pkg-up": "^5.0.0"
},
"devDependencies": {
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
16 changes: 8 additions & 8 deletions readme.md
Expand Up @@ -8,15 +8,15 @@ Can be used to greet the user the first time they use your CLI app, show usage t
## Install

```
$ npm install --save first-run
$ npm install first-run
```


## Usage

```js
// x.js
var firstRun = require('first-run');
const firstRun = require('first-run');

console.log(firstRun());
```
Expand All @@ -33,18 +33,18 @@ false

### firstRun([options])

### firstRun.clear([options])

Clear the state.

#### options.name

Type: `string`
Type: `string`<br>
Default: `name` field in your package.json

The name used to identify it.

### firstRun.clear()

Clear the state.


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
7 changes: 5 additions & 2 deletions test.js
@@ -1,5 +1,8 @@
'use strict';
var Configstore = require('configstore');
var firstRun = require('./');

const Configstore = require('configstore');
const firstRun = require('.');

(new Configstore('first-run_first-run')).clear();
// eslint-disable-next-line unicorn/no-process-exit
process.exit(firstRun() ? 0 : 1);
5 changes: 4 additions & 1 deletion test2.js
@@ -1,3 +1,6 @@
'use strict';
var firstRun = require('./');

const firstRun = require('.');

// eslint-disable-next-line unicorn/no-process-exit
process.exit(firstRun() ? 1 : 0);
11 changes: 7 additions & 4 deletions test3.js
@@ -1,8 +1,11 @@
'use strict';
var Configstore = require('configstore');
var firstRun = require('./');

const Configstore = require('configstore');
const firstRun = require('.');

(new Configstore('first-run_first-run')).clear();
var shouldBeTrue = firstRun();
const shouldBeTrue = firstRun();
firstRun.clear();
var shouldBeTrueAgain = firstRun();
const shouldBeTrueAgain = firstRun();
// eslint-disable-next-line unicorn/no-process-exit
process.exit(shouldBeTrue && shouldBeTrueAgain ? 0 : 1);

0 comments on commit a401044

Please sign in to comment.