├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md ├── test.js ├── test2.js └── test3.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | /** 3 | The name used to identify it. 4 | 5 | Usually, you would fetch the `name` field from package.json. 6 | */ 7 | readonly name: string; 8 | } 9 | 10 | /** 11 | Check if it's the first time the process is run. 12 | 13 | @example 14 | ``` 15 | // x.js 16 | import isFirstRun from 'first-run'; 17 | 18 | console.log(isFirstRun()); 19 | 20 | // $ node x.js 21 | // true 22 | // $ node x.js 23 | // false 24 | ``` 25 | */ 26 | export default function isFirstRun(options: Options): boolean; 27 | 28 | /** 29 | Clear the state. 30 | */ 31 | export function clearFirstRun(options: Options): void; 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import Configstore from 'configstore'; 2 | 3 | function getConfigStore({name} = {}) { 4 | if (!name) { 5 | throw new Error('Please specify the `name` option.'); 6 | } 7 | 8 | return new Configstore(`first-run_${name}`, {firstRun: true}); 9 | } 10 | 11 | export default function isFirstRun(options) { 12 | const configStore = getConfigStore(options); 13 | const isFirstRun = configStore.get('firstRun'); 14 | 15 | if (isFirstRun === true) { 16 | configStore.set('firstRun', false); 17 | } 18 | 19 | return isFirstRun; 20 | } 21 | 22 | export function clearFirstRun(options) { 23 | getConfigStore(options).set('firstRun', true); 24 | } 25 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import isFirstRun, {clearFirstRun} from './index.js'; 3 | 4 | expectType(isFirstRun({name: 'foo'})); 5 | 6 | clearFirstRun({name: 'foo'}); 7 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "first-run", 3 | "version": "3.0.0", 4 | "description": "Check if it's the first time the process is run", 5 | "license": "MIT", 6 | "repository": "sindresorhus/first-run", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && node test.js && node test2.js && node test3.js && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "first", 27 | "init", 28 | "initial", 29 | "run", 30 | "time", 31 | "process", 32 | "start" 33 | ], 34 | "dependencies": { 35 | "configstore": "^6.0.0" 36 | }, 37 | "devDependencies": { 38 | "tsd": "^0.18.0", 39 | "xo": "^0.45.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # first-run 2 | 3 | > Check if it's the first time the process is run 4 | 5 | Can be used to greet the user the first time they use your CLI app, show usage tip, initialize something, etc. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install first-run 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | // x.js 17 | import isFirstRun from 'first-run'; 18 | 19 | console.log(isFirstRun({name: 'x'})); 20 | ``` 21 | 22 | ``` 23 | $ node x.js 24 | true 25 | $ node x.js 26 | false 27 | ``` 28 | 29 | ## API 30 | 31 | ### isFirstRun(options) 32 | 33 | ### clearFirstRun(options) 34 | 35 | Clear the state. 36 | 37 | #### options 38 | 39 | Type: `object` 40 | 41 | ##### name 42 | 43 | Type: `string` 44 | 45 | The name used to identify it. 46 | 47 | Usually, you would fetch the `name` field from package.json. 48 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import Configstore from 'configstore'; 3 | import isFirstRun from './index.js'; 4 | 5 | (new Configstore('first-run_first-run')).clear(); 6 | 7 | // eslint-disable-next-line unicorn/no-process-exit 8 | process.exit(isFirstRun({name: 'first-run'}) ? 0 : 1); 9 | -------------------------------------------------------------------------------- /test2.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import isFirstRun from './index.js'; 3 | 4 | // eslint-disable-next-line unicorn/no-process-exit 5 | process.exit(isFirstRun({name: 'first-run'}) ? 1 : 0); 6 | -------------------------------------------------------------------------------- /test3.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import Configstore from 'configstore'; 3 | import isFirstRun, {clearFirstRun} from './index.js'; 4 | 5 | const name = 'first-run'; 6 | 7 | (new Configstore(`first-run_${name}`)).clear(); 8 | const shouldBeTrue = isFirstRun({name}); 9 | clearFirstRun({name}); 10 | const shouldBeTrueAgain = isFirstRun({name}); 11 | // eslint-disable-next-line unicorn/no-process-exit 12 | process.exit(shouldBeTrue && shouldBeTrueAgain ? 0 : 1); 13 | --------------------------------------------------------------------------------