Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanyunusov committed Aug 1, 2022
1 parent 99e1aff commit 514f1a1
Show file tree
Hide file tree
Showing 67 changed files with 2,763 additions and 3,335 deletions.
47 changes: 23 additions & 24 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@ on:
pull_request:
branches: [master]
jobs:
full:
name: Node.js 17 Full
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v2
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6.32.9
- name: Install Node.js
uses: actions/setup-node@v2
with:
node-version: 17
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run tests
run: pnpm test
env:
FORCE_COLOR: 2
# full:
# name: Node.js 17 Full
# runs-on: ubuntu-latest
# steps:
# - name: Checkout the repository
# uses: actions/checkout@v2
# - name: Install pnpm
# uses: pnpm/action-setup@v2
# with:
# version: 7.8.0
# - name: Install Node.js
# uses: actions/setup-node@v2
# with:
# node-version: 17
# cache: pnpm
# - name: Install dependencies
# run: pnpm install --frozen-lockfile
# - name: Run tests
# run: pnpm test
# env:
# FORCE_COLOR: 2
short:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version:
- 12
- 14
- 16
- 17
Expand All @@ -48,7 +47,7 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6.32.9
version: 7.8.0
- name: Install Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
Expand All @@ -69,7 +68,7 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6.32.9
version: 7.8.0
- name: Install Node.js
uses: actions/setup-node@v2
with:
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@

## Features

- 📦 **Small**: [47kB](https://packagephobia.com/result?p=nano-staged) (142x+ lighter than **lint-staged**).
- 🥇 **Single dependency** ([`picocolors`](https://github.com/alexeyraspopov/picocolors)).
- 💨 **Dependency-free**
- 🤏 **Small**: [48kB](https://packagephobia.com/result?p=nano-staged) (142x+ lighter than **lint-staged**).
- ☯️ **Support multiple file states like staged, unstaged, last-commit, changed etc**
- 💪 **Multi configuration** (useful for monorepos)

## Benchmarks

Benchmarks running time for 10 file:

```diff
$ node bench/running-time/index.js
- lint-staged 1.394 ms
+ nano-staged 0.968 ms
- lint-staged 0.439 ms
+ nano-staged 0.257 ms
```

The space in node_modules including sub-dependencies:

```diff
$ node bench/size/index.js
Data from packagephobia.com
- lint-staged 6688 kB
+ nano-staged 47 kB
- lint-staged 6707 kB
+ nano-staged 48 kB
```

The performance results were generated on a MBP Late 2013, 2.3 GHz Intel Core i7 by running `npm run bench` in the library folder. See [bench/running-time/index.js](https://github.com/usmanyunusov/nano-staged/blob/master/bench/running-time/index.js)
Expand All @@ -40,7 +41,9 @@ The performance results were generated on a MBP Late 2013, 2.3 GHz Intel Core i7
```terminal
npm install --save-dev nano-staged
```

or

```terminal
yarn add nano-staged -D
```
Expand Down
34 changes: 21 additions & 13 deletions bench/running-time/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { execFile } from 'child_process'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
import { promisify } from 'util'
import { nanoid } from 'nanoid'
import fs from 'fs-extra'

import { createGit } from '../../lib/git.js'
import { create_git } from '../../lib/git.js'
import { executor } from '../../lib/executor.js'

let spawn = promisify(execFile)
let currentDir = dirname(fileURLToPath(import.meta.url))
let cwd = resolve(currentDir, `nano-staged-${nanoid()}`)
let runners = ['lint-staged', 'nano-staged']
Expand All @@ -22,7 +20,7 @@ async function appendFile(filename, content, dir = cwd) {
}

async function execGit(args) {
let git = createGit(cwd)
let git = create_git(cwd)
await git.exec(args, { cwd })
}

Expand All @@ -33,28 +31,32 @@ async function initGitRepo() {
await appendFile('README.md', '# Test\n')
await appendFile('.gitignore', `node_modules/\n`)
await execGit(['add', 'README.md'])
await execGit(['commit', '-m initial commit'])
await execGit(['commit', '-m', '"initial commit"'])
}

async function initProject() {
await appendFile(
'package.json',
`{
"lint-staged": {
"*.js": "prettier --write",
"*.css": "prettier --write"
"*.js": "echo test",
"*.css": "echo test"
},
"nano-staged": {
"*.js": "prettier --write",
"*.css": "prettier --write"
"*.js": "echo test",
"*.css": "echo test"
}
}`
)

await spawn('yarn', ['add', 'lint-staged'], { cwd })
await spawn('yarn', ['add', resolve(cwd, '../../../../nano-staged')], {
await executor('pnpm', ['add', 'lint-staged'], {
cwd,
})

await executor('pnpm', ['add', resolve(cwd, '../../../../nano-staged')], {
cwd,
})

await appendFile('a.js', 'var test = {};')
await appendFile('b.js', 'var test = {};')
await appendFile('c.js', 'var test = {};')
Expand Down Expand Up @@ -82,7 +84,13 @@ function showTime(name) {
async function run() {
for (let runner of runners) {
before = performance.now()
await spawn(`./node_modules/.bin/${runner}`, { cwd })

try {
await executor(`./node_modules/.bin/${runner}`, [], { cwd })
} catch (error) {
console.log(error)
}

showTime(runner)
}
}
Expand Down
2 changes: 1 addition & 1 deletion bench/size/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import { get } from 'https'
import c from 'picocolors'
import c from '../../lib/colors.js'

async function getJSON(url) {
return new Promise((resolve) => {
Expand Down
51 changes: 33 additions & 18 deletions lib/bin.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
#!/usr/bin/env node

import nanoStaged from './index.js'
import * as utils from './utils.js'
import { is_color_support } from './colors.js'
import { create_debug } from './debug.js'
import nano_staged from './index.js'

const FORCE_COLOR_LEVEL = utils.getForceColorLevel()

if (FORCE_COLOR_LEVEL) {
process.env.FORCE_COLOR = FORCE_COLOR_LEVEL.toString()
if (is_color_support) {
process.env.FORCE_COLOR = '1'
}

process.on('SIGINT', () => {})

const debug = create_debug('nano-staged:bin')

function run() {
let options = {}
const args = process.argv.reduce((prev, arg) => [...prev, ...arg.split('=')], [])
const opts = {}

for (let i = 2; i < process.argv.length; i++) {
let arg = process.argv[i]
for (let i = 2; i < args.length; i++) {
let arg = args[i]

if (arg === '-c' || arg === '--config') {
options.config = process.argv[++i]
} else if (arg === '-u' || arg === '--unstaged') {
options.unstaged = true
if (arg === '-u' || arg === '--unstaged') {
opts.unstaged = true
} else if (arg === '-d' || arg === '--debug') {
process.env.NS_DEBUG = true
} else if (arg === '--allow-empty') {
options.allowEmpty = true
opts.allow_empty = true
} else if (arg === '--shell') {
opts.shell = true
} else if (arg === '--cwd') {
opts.unstaged = args[++i]
} else if (arg === '-c' || arg === '--config') {
opts.config_path = args[++i]
} else if (arg === '--max-arg-length') {
opts.max_arg_length = Number(args[++i])
} else if (arg === '--diff-filter') {
opts.diff_filter = args[++i]
} else if (arg === '--diff') {
options.diff = []
} else if (options.diff && options.diff.length !== 2) {
options.diff.push(process.argv[i])
opts.diff = []
} else if (opts.diff && opts.diff.length !== 2) {
opts.diff.push(args[i])
}
}

return nanoStaged(options)
debug('Options parsed from CLI:', opts)
debug('FORCE_COLOR:', process.env.FORCE_COLOR)

return nano_staged(opts)
}

run().catch(() => {
Expand Down
Loading

0 comments on commit 514f1a1

Please sign in to comment.