-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
95 lines (82 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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'
let spawn = promisify(execFile)
let currentDir = dirname(fileURLToPath(import.meta.url))
let cwd = resolve(currentDir, `nano-staged-${nanoid()}`)
let runners = ['lint-staged', 'nano-staged']
let before
async function makeDir(dir = cwd) {
await fs.mkdir(dir)
}
async function appendFile(filename, content, dir = cwd) {
await fs.appendFile(resolve(dir, filename), content)
}
async function execGit(args) {
let git = createGit(cwd)
await git.exec(args, { cwd })
}
async function initGitRepo() {
await execGit(['init'])
await execGit(['config', 'user.name', '"test"'])
await execGit(['config', 'user.email', '"test@test.com"'])
await appendFile('README.md', '# Test\n')
await appendFile('.gitignore', `node_modules/\n`)
await execGit(['add', 'README.md'])
await execGit(['commit', '-m initial commit'])
}
async function initProject() {
await appendFile(
'package.json',
`{
"lint-staged": {
"*.js": "prettier --write",
"*.css": "prettier --write"
},
"nano-staged": {
"*.js": "prettier --write",
"*.css": "prettier --write"
}
}`
)
await spawn('yarn', ['add', 'lint-staged'], { cwd })
await spawn('yarn', ['add', resolve(cwd, '../../../../nano-staged')], {
cwd,
})
await appendFile('a.js', 'var test = {};')
await appendFile('b.js', 'var test = {};')
await appendFile('c.js', 'var test = {};')
await appendFile('d.js', 'var test = {};')
await appendFile('e.js', 'var test = {};')
await appendFile('a.css', 'body {color: red;}')
await appendFile('b.css', 'body {color: red;}')
await appendFile('c.css', 'body {color: red;}')
await appendFile('d.css', 'body {color: red;}')
await appendFile('e.css', 'body {color: red;}')
await execGit(['add', '--all'])
}
function showTime(name) {
let prefix = name === 'nano-staged' ? '+ ' : '- '
let after = performance.now()
let time = (Math.round(after - before) / 1000)
.toString()
.replace(/\.\d$/, '$&00')
.replace(/\.\d\d$/, '$&0')
process.stdout.write(prefix + name + '\x1B[1m' + time.padStart(6) + '\x1B[22m ms\n')
}
async function run() {
for (let runner of runners) {
before = performance.now()
await spawn(`./node_modules/.bin/${runner}`, { cwd })
showTime(runner)
}
}
await makeDir()
await initGitRepo()
await initProject()
await run()
await fs.remove(cwd)