1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict'
|
7 |
|
8 | const fs = require('fs')
|
9 |
|
10 | const isStream = (content) => !!content && typeof content !== 'string' && !!content.pipe
|
11 |
|
12 | async function hasDuplicate (filename, content) {
|
13 | if (isStream(content)) {
|
14 | return false
|
15 | }
|
16 | const exists = await new Promise((resolve) =>
|
17 | fs.exists(filename, (exists) => resolve(exists))
|
18 | )
|
19 | if (!exists) {
|
20 | return false
|
21 | }
|
22 | const existing = await new Promise((resolve, reject) =>
|
23 | fs.readFile(filename, (err, content) =>
|
24 | err ? reject(err) : resolve(content)
|
25 | )
|
26 | )
|
27 | return String(existing) === String(content)
|
28 | }
|
29 |
|
30 | module.exports = {hasDuplicate, isStream}
|