-
Notifications
You must be signed in to change notification settings - Fork 7
/
scaffold-generator.js
143 lines (108 loc) · 2.92 KB
/
scaffold-generator.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const test = require('ava')
const Scaffold = require('../index')
const fs = require('fs-extra')
const mustache = require('mustache')
const path = require('path')
const _tmp = require('tmp')
mustache.escape = v => v
const template = (filepath = '') =>
path.join(__dirname, 'fixtures', 'template', filepath)
const expected = (filepath = '') =>
path.join(__dirname, 'fixtures', 'expected', filepath)
// @param {path} dest dest dir
async function equal (t, dest, name, dest_name, equal = true) {
const content = await fs.readFile(path.join(dest, dest_name || name))
const expect_content = await fs.readFile(expected(name))
if (equal) {
t.is(content.toString(), expect_content.toString(),
`content not the same: ${dest}/${name}`)
return
}
if (content.toString() === expect_content.toString()) {
t.fail(`should not equal: ${dest}/${name}`)
}
}
function notEqual (t, d, n) {
return equal(t, d, n, undefined, false)
}
async function expect (t, dest, name, expect_content) {
const content = await fs.readFile(path.join(dest, name))
t.is(content.toString(), expect_content, 'wrong content: ${dest}')
}
function s (o) {
o.render = mustache.render
return new Scaffold(o)
}
function tmp () {
return new Promise((resolve, reject) => {
_tmp.dir((err, path) => {
if (err) {
return reject(err)
}
resolve(path)
})
})
}
const data = {
name: 'foo',
main: 'index.js'
}
test('copy, override=false, not exists', async t => {
const to = await tmp()
await s({
data
})
.copy(template(), to)
await equal(t, to, 'index.js')
await equal(t, to, 'package.json')
})
test('copy, override=false, not exists, hierachical dirs', async t => {
const to = await tmp()
await s({
data: {
name: 'foo',
main: 'lib/index.js'
}
})
.copy(template(), to)
await equal(t, to, 'index.js', 'lib/index.js')
})
test('copy, override=false, exists', async t => {
const to = await tmp()
await fs.writeFile(path.join(to, 'index.js'), 'a')
await s({
data,
override: false
})
.copy(template(), to)
await notEqual(t, to, 'index.js')
await expect(t, to, 'index.js', 'a')
await equal(t, to, 'package.json')
})
test('copy, override=true, backup=true, exists', async t => {
const to = await tmp()
await fs.writeFile(path.join(to, 'index.js'), 'a')
await s({
data,
override: true
})
.copy(template(), to)
await equal(t, to, 'index.js')
await expect(t, to, 'index.js.bak', 'a')
await equal(t, to, 'package.json')
})
test('copy, override=true, backup=false, exists', async t => {
const to = await tmp()
await fs.writeFile(path.join(to, 'index.js'), 'a')
await s({
data,
override: true,
backup: false
})
.copy(template(), to)
await equal(t, to, 'index.js')
await equal(t, to, 'package.json')
const dest = path.join(to, 'index.js.bak')
const exists = await fs.exists(dest)
t.is(exists, false, 'should not backup')
})