Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanyunusov committed Jun 20, 2022
1 parent 33b0332 commit ef458f7
Show file tree
Hide file tree
Showing 68 changed files with 2,947 additions and 1,747 deletions.
58 changes: 6 additions & 52 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,16 @@ on:
branches: [master]
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
short:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version:
- 12
- 14
- 16
- 17
os:
- ubuntu-latest
- macos-latest
- windows-latest
node-version: [16]
os: [windows-latest]
fail-fast: false

name: Node.js v${{ matrix.node-version }} on ${{ matrix.os }}
steps:
- if: matrix.os == 'windows-latest'
Expand All @@ -48,7 +23,7 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 6.32.9
version: 7.1.2
- name: Install Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
Expand All @@ -60,24 +35,3 @@ jobs:
run: pnpm unit
env:
FORCE_COLOR: 2
benchmark:
name: Benchmark
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: 16
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Running time benchmark
run: pnpm bench
env:
FORCE_COLOR: 2
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
45 changes: 30 additions & 15 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((acc, arg) => [...acc, ...arg.split('=')], [])
const options = {}

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

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

return nano_staged(options)
}

run().catch(() => {
Expand Down
130 changes: 0 additions & 130 deletions lib/cmd-runner.js

This file was deleted.

29 changes: 29 additions & 0 deletions lib/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as tty from 'tty'

export const is_color_support =
!('NO_COLOR' in process.env || process.argv.includes('--no-color')) &&
('FORCE_COLOR' in process.env ||
process.argv.includes('--color') ||
process.platform === 'win32' ||
(tty.isatty(1) && process.env.TERM !== 'dumb') ||
'CI' in process.env)

const format = (start, end) => (input) => {
const open = `\x1b[${start}m`
const close = `\x1b[${end}m`
const string = '' + input
const regex = new RegExp(`\\x1b\\[${end}m`, 'g')

return is_color_support ? open + string.replace(regex, open) + close : String(string)
}

export default {
inverse: format(7, 27),
yellow: format(93, 39),
green: format(92, 39),
cyan: format(96, 39),
gray: format(90, 39),
bold: format(1, 22),
red: format(91, 39),
dim: format(2, 22),
}
Loading

0 comments on commit ef458f7

Please sign in to comment.