-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 12.20 and move to ESM
- Loading branch information
1 parent
c5834fc
commit 5da56d9
Showing
10 changed files
with
73 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com) | ||
Copyright (c) Sindre Sorhus <[email protected]> (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: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": "[email protected]", | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |