Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanyunusov committed Feb 15, 2022
1 parent af8db58 commit 8e8c907
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 61 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/spawner.js → lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ async function which(tool, check) {
return ''
}

export async function spawner(cmd, args = [], opts = {}) {
export async function executor(cmd, args = [], opts = {}) {
let prefix = await getPrefix(process.cwd())

if (prefix) {
Expand Down
4 changes: 2 additions & 2 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, normalize } from 'path'

import { spawner } from './spawner.js'
import { executor } from './executor.js'
import { toArray } from './utils.js'

const ADDED = 'A'.charCodeAt(0)
Expand Down Expand Up @@ -56,7 +56,7 @@ export function createGit(cwd = process.cwd()) {

async exec(args = [], opts = {}) {
try {
return await spawner('git', args, {
return await executor('git', args, {
...opts,
cwd: opts.cwd || git.cwd,
})
Expand Down
13 changes: 6 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createReporter } from './create-reporter.js'
import { getConfig, validConfig } from './config.js'
import { NanoStagedError } from './error.js'
import { createReporter } from './reporter.js'
import { NanoStagedError } from './errors.js'
import { createRunner } from './runner.js'
import { toArray } from './utils.js'

const defaultOptions = {
config: undefined,
Expand All @@ -13,7 +14,7 @@ const defaultOptions = {
}

export default async function (opts = { ...defaultOptions, ...opts }) {
const report = createReporter(opts.stream)
const reporter = createReporter(opts.stream)

try {
const config = await getConfig(opts.cwd, opts.config)
Expand All @@ -40,10 +41,8 @@ export default async function (opts = { ...defaultOptions, ...opts }) {
await runner.run()
}
} catch (error) {
const errors = Array.isArray(error) ? error : [error]

for (const error of errors) {
report.error(error)
for (const error of toArray(error)) {
reporter.error(error)
}

throw error
Expand Down
2 changes: 1 addition & 1 deletion lib/create-reporter.js → lib/reporter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import c from 'picocolors'

import { NanoStagedError, TaskRunnerError } from './error.js'
import { NanoStagedError, TaskRunnerError } from './errors.js'

export function createReporter(stream = process.stderr) {
function print(lines) {
Expand Down
22 changes: 11 additions & 11 deletions lib/runner.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { createReporter } from './create-reporter.js'
import { createGitWorkflow } from './git-workflow.js'
import { createTaskRunner } from './task-runner.js'
import { NanoStagedError } from './error.js'
import { createTasksRunner } from './tasks-runner.js'
import { createReporter } from './reporter.js'
import { NanoStagedError } from './errors.js'
import { MultiSpinner } from './spinner.js'
import { createGit } from './git.js'

export function createRunner({ cwd, stream, allowEmpty, config }) {
const reporter = createReporter(stream)
const git = createGit(cwd)
const report = createReporter(stream)

const runner = {
async run(type = 'staged', { refs = [] } = {}) {
async run(type = 'staged', opts = {}) {
const { repoPath, dotGitPath } = await git.getRepoAndDotGitPaths()

if (!repoPath) {
Expand All @@ -21,28 +21,28 @@ export function createRunner({ cwd, stream, allowEmpty, config }) {
if (type === 'unstaged') {
files = await git.unstagedFiles({ cwd: repoPath })
} else if (type === 'diff') {
files = await git.changedFiles(refs, { cwd: repoPath })
files = await git.changedFiles(opts.refs, { cwd: repoPath })
} else {
files = await git.stagedFiles({ cwd: repoPath })
}

if (!files.working.length) {
report.error(new NanoStagedError('noFiles', type))
reporter.error(new NanoStagedError('noFiles', type))
return
}

const changes = [...files.changed, ...files.deleted]
const gitWorkflow = await createGitWorkflow({ allowEmpty, repoPath, dotGitPath })
const taskRunner = await createTaskRunner({
const tasksRunner = await createTasksRunner({
files: files.working,
repoPath,
config,
type,
cwd,
})

if (!taskRunner.tasks.some(({ files }) => files.length > 0)) {
report.error(new NanoStagedError('noMatchingFiles'))
if (!tasksRunner.tasks.some(({ files }) => files.length > 0)) {
reporter.error(new NanoStagedError('noMatchingFiles'))
return
}

Expand Down Expand Up @@ -82,7 +82,7 @@ export function createRunner({ cwd, stream, allowEmpty, config }) {
title: `Running tasks for ${type} files...`,
task: async (spin) => {
try {
await taskRunner.run(spin)
await tasksRunner.run(spin)
} catch (e) {
revert = true
throw e
Expand Down
8 changes: 4 additions & 4 deletions lib/task-runner.js → lib/tasks-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import c from 'picocolors'

import { globToRegex } from './glob-to-regex.js'
import { stringArgvToArray } from './utils.js'
import { TaskRunnerError } from './error.js'
import { spawner } from './spawner.js'
import { TaskRunnerError } from './errors.js'
import { executor } from './executor.js'
import { toArray } from './utils.js'

export async function createTaskRunner({
export async function createTasksRunner({
cwd = process.cwd(),
type = 'staged',
repoPath = '',
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function createTaskRunner({
continue
}

await spawner(command, task.cmdFn ? args : args.concat(task.files), {
await executor(command, task.cmdFn ? args : args.concat(task.files), {
cwd: repoPath,
})

Expand Down
2 changes: 1 addition & 1 deletion test/error.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { is } from 'uvu/assert'
import { test } from 'uvu'

import { NanoStagedError, TaskRunnerError } from '../lib/error.js'
import { NanoStagedError, TaskRunnerError } from '../lib/errors.js'
import { createStdout } from './utils/index.js'

let stdout = createStdout()
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ test('should runner run error', async () => {
try {
await nanoStaged({ stream: stdout })
} catch (error) {
is(stdout.out, '\n\x1B[31m×\x1B[39m \x1B[31mTask error\x1B[39m\n')
is(stdout.out, '\n\x1B[31mTask error\x1B[39m\n')
}
})

Expand Down
8 changes: 4 additions & 4 deletions test/create-reporter.test.js → test/reporter.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { is } from 'uvu/assert'
import { test } from 'uvu'

import { NanoStagedError, TaskRunnerError } from '../lib/errors.js'
import { createReporter } from '../lib/reporter.js'
import { createStdout } from './utils/index.js'
import { createReporter } from '../lib/create-reporter.js'
import { NanoStagedError, TaskRunnerError } from '../lib/error.js'

let stdout = createStdout()
let report = createReporter(stdout)
Expand All @@ -16,15 +16,15 @@ test('should reported error correctly', () => {
let err = new Error('Error')

report.error(err)
is(stdout.out, '\n\x1B[31m×\x1B[39m \x1B[31mError\x1B[39m\n')
is(stdout.out, '\n\x1B[31mError\x1B[39m\n')
})

test('should reported TaskRunnerError correctly', () => {
let err = new Error('TaskRunnerError')
err.name = 'TaskRunnerError'

report.error(err)
is(stdout.out, '\n\x1B[31m×\x1B[39m \x1B[31mTaskRunnerError\x1B[39m\n')
is(stdout.out, '\n\x1B[31mTaskRunnerError\x1B[39m\n')
})

test('should reported NanoStagedError correctly', () => {
Expand Down
37 changes: 19 additions & 18 deletions test/runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test('should return when git not found', async () => {
})

let runner = createRunner({ stream: stdout })

try {
await runner.run()
} catch (error) {
Expand Down Expand Up @@ -71,8 +72,8 @@ test('should return when no files match any configured task', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: [] }],
}),
},
Expand All @@ -95,8 +96,8 @@ test('should step success', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.resolve(),
}),
Expand Down Expand Up @@ -137,8 +138,8 @@ test('should backupOriginalState error', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.resolve(),
}),
Expand Down Expand Up @@ -171,8 +172,8 @@ test('should backupUnstagedFiles error', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.resolve(),
}),
Expand Down Expand Up @@ -211,8 +212,8 @@ test('should applyModifications error', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.resolve(),
}),
Expand Down Expand Up @@ -254,8 +255,8 @@ test('should restoreUnstagedFiles error', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.resolve(),
}),
Expand Down Expand Up @@ -297,8 +298,8 @@ test('should restoreOriginalState error', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.resolve(),
}),
Expand Down Expand Up @@ -335,8 +336,8 @@ test('should restoreOriginalState error', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.reject('Task runner error'),
}),
Expand Down Expand Up @@ -377,8 +378,8 @@ test('should cleanUp error', async () => {
stagedFiles: async () => ({ working: ['a.js'], deleted: [], changed: ['a.js'] }),
}),
},
'../lib/task-runner.js': {
createTaskRunner: () => ({
'../lib/tasks-runner.js': {
createTasksRunner: () => ({
tasks: [{ files: ['a.js'] }],
run: async () => Promise.resolve(),
}),
Expand Down
26 changes: 15 additions & 11 deletions test/task-runner.test.js → test/tasks-runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import esmock from 'esmock'
import { test } from 'uvu'

import { createStdout } from './utils/index.js'
import { Spinner } from '../lib/spinner.js'
import { MultiSpinner } from '../lib/spinner.js'

let stdout = createStdout()

Expand All @@ -14,9 +14,9 @@ test.before.each(() => {
})

test('should create runner and resolve tasks', async () => {
const { createTaskRunner } = await esmock('../lib/task-runner.js')
const { createTasksRunner } = await esmock('../lib/tasks-runner.js')

let runner = await createTaskRunner({
let runner = await createTasksRunner({
repoPath: join(homedir(), 'test'),
cwd: join(homedir(), 'test'),
files: ['a.js', '../../b.css'],
Expand All @@ -43,24 +43,28 @@ test('should create runner and resolve tasks', async () => {
})

test('should run handle error', async () => {
const { createTaskRunner } = await esmock('../lib/task-runner.js', {
'../lib/spawner.js': {
spawner: async () => Promise.reject('Run error'),
const { createTasksRunner } = await esmock('../lib/tasks-runner.js', {
'../lib/executor.js': {
executor: async () => Promise.reject('Run error'),
},
})

let runner = await createTaskRunner({
let runner = await createTasksRunner({
repoPath: 'test',
files: ['a.js', '../../b.css'],
config: { '*.js': ['prettier --write', 'prettier --write'], '*.css': () => 'prettier --write' },
stream: stdout,
})

const spinner = new Spinner({ stream: stdout })
const spinner = new MultiSpinner({ stream: stdout })

await runner.run(spinner)

spinner.stop()
try {
await runner.run(spinner)
} catch (error) {
is(stdout.out, '\x1B[?25l\x1B[33m\\\x1B[39m *.js\x1B[2m - 1 file\x1B[22m')
} finally {
spinner.stop()
}
})

test.run()

0 comments on commit 8e8c907

Please sign in to comment.