nano-staged
Advanced tools
Comparing version
@@ -1,22 +0,33 @@ | ||
import { promises as fs } from 'fs' | ||
import syncFs, { promises as fs } from 'fs' | ||
import { resolve } from 'path' | ||
import { findUp } from '../utils/index.js' | ||
const NODE_PACKAGE_JSON = 'package.json' | ||
const CONFIG_NAME = 'nano-staged' | ||
const PLACES = [`.${CONFIG_NAME}.json`, `${CONFIG_NAME}.json`, NODE_PACKAGE_JSON] | ||
export async function loadConfig(cwd = process.cwd()) { | ||
try { | ||
let rootPath = findUp(NODE_PACKAGE_JSON, cwd) | ||
let config | ||
let dir = resolve(cwd) | ||
if (!rootPath) { | ||
return undefined | ||
} | ||
do { | ||
cwd = dir | ||
let pkgPath = resolve(rootPath, NODE_PACKAGE_JSON) | ||
let pkgJson = JSON.parse(await fs.readFile(pkgPath, 'utf8')) | ||
let config = pkgJson[CONFIG_NAME] | ||
for (let place of PLACES) { | ||
let path = resolve(cwd, place) | ||
return config | ||
if (!config && syncFs.existsSync(path)) { | ||
if (place === 'package.json') { | ||
let pkg = JSON.parse(await fs.readFile(path, 'utf8')) | ||
config = pkg[CONFIG_NAME] | ||
} else { | ||
config = JSON.parse(await fs.readFile(path, 'utf8')) | ||
} | ||
return config | ||
} | ||
} | ||
dir = resolve(cwd, '../') | ||
} while (dir !== cwd) | ||
} catch (error) { | ||
@@ -23,0 +34,0 @@ return undefined |
@@ -1,4 +0,4 @@ | ||
import { join } from 'path' | ||
import { join, normalize } from 'path' | ||
import { spawn, findUp, toArray } from '../utils/index.js' | ||
import { spawn, toArray } from '../utils/index.js' | ||
@@ -54,2 +54,3 @@ const ADDED = 'A'.charCodeAt(0) | ||
/* c8 ignore next 3 */ | ||
if (threeWay) { | ||
@@ -67,9 +68,16 @@ args.push('--3way') | ||
async getRepoAndDotGitPaths(opts = {}) { | ||
let result = {} | ||
let repoPath = findUp('.git', opts.cwd || cwd) | ||
try { | ||
let result = await git.exec(['rev-parse', '--show-toplevel'], opts) | ||
let repositoriyPath = result ? normalize(result.trimLeft().replace(/[\r\n]+$/, '')) : '' | ||
result['repoPath'] = repoPath || null | ||
result['dotGitPath'] = repoPath ? join(repoPath, '.git') : null | ||
return result | ||
return { | ||
repoPath: repositoriyPath || null, | ||
dotGitPath: repositoriyPath ? join(repositoriyPath, '.git') : null, | ||
} | ||
} catch (error) { | ||
return { | ||
repoPath: null, | ||
dotGitPath: null, | ||
} | ||
} | ||
}, | ||
@@ -76,0 +84,0 @@ |
{ | ||
"name": "nano-staged", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "Tool to run commands only on git staged files", | ||
@@ -5,0 +5,0 @@ "author": "Usman Yunusov <usman.iunusov@gmail.com>", |
@@ -5,3 +5,3 @@ # Nano Staged | ||
- 📦 **Small**: 223x+ lighter than **lint-staged**. | ||
- 📦 **Small**: 200x+ lighter than **lint-staged**. | ||
- 🥇 **Single dependency** (Picocolors). | ||
@@ -8,0 +8,0 @@ |
import { spawn as baseSpawn } from 'child_process' | ||
import { resolve, join, delimiter } from 'path' | ||
import { existsSync, readFileSync } from 'fs' | ||
import { join, delimiter } from 'path' | ||
import { readFileSync } from 'fs' | ||
import { fileURLToPath } from 'url' | ||
@@ -13,13 +13,2 @@ import pico from 'picocolors' | ||
export function findUp(name, cwd = process.cwd()) { | ||
let dir = resolve(cwd) | ||
do { | ||
cwd = dir | ||
const foundPath = resolve(cwd, name) | ||
if (existsSync(foundPath)) return cwd | ||
dir = resolve(cwd, '../') | ||
} while (dir !== cwd) | ||
} | ||
export function showVersion(print) { | ||
@@ -26,0 +15,0 @@ let pkg = readFileSync(join(fileURLToPath(import.meta.url), '../..', 'package.json')) |
25285
1.26%817
1.24%