-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (73 loc) · 1.7 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
const path = require('path')
const tmp = require('tmp-promise')
const fse = require('fs-extra')
const npminstall = require('npminstall')
const {isObject} = require('core-util-is')
class Fixtures {
// @param {Array<string>} args
constructor (...args) {
this._root = this._root()
this._path = path.resolve(this._root, ...args)
this.resolve = this.resolve.bind(this)
this.copy = this.copy.bind(this)
this.install = this.install.bind(this)
this.fixture = this.fixture.bind(this)
}
// Method for override
// @returns the root of
_root () {
return path.resolve('test', 'fixtures')
}
get root () {
return this._root
}
fixture (...args) {
return path.resolve(this._root, ...args)
}
resolve (...args) {
return path.resolve(this._path, ...args)
}
install (options = {}) {
return npminstall({
trace: false,
// We cant disable spinners due to
// https://github.com/cnpm/npminstall/issues/302
detail: true,
...options,
root: this._path
})
}
async copy (options) {
const {
to,
install = false,
clean = false
} = isObject(options)
? options
: {
to: options
}
if (to && clean) {
try {
await fse.remove(to)
} catch (err) {
// istanbul ignore next
if (err.code !== 'ENOENT') {
// istanbul ignore next
throw err
}
}
}
const toDir = to
? to
: (await tmp.dir()).path
await fse.copy(this._path, toDir)
this._path = toDir
if (install) {
await this.install()
}
return toDir
}
}
module.exports = (...args) => new Fixtures(...args)
module.exports.Fixtures = Fixtures