forked from sindresorhus/gulp-changed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (53 loc) · 1.83 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
import process from 'node:process';
import fs from 'node:fs/promises';
import path from 'node:path';
import changeFileExtension from 'change-file-extension';
import {gulpPlugin} from 'gulp-plugin-extras';
// Only push through files changed more recently than the destination files
export async function compareLastModifiedTime(sourceFile, targetPath) {
const targetStat = await fs.stat(targetPath, {bigint: true});
if (sourceFile.stat && sourceFile.stat.mtimeMs > targetStat.mtimeMs) {
return sourceFile;
}
}
// Only push through files with different contents than the destination files
export async function compareContents(sourceFile, targetPath) {
const targetData = await fs.readFile(targetPath);
if (!sourceFile.contents.equals(targetData)) {
return sourceFile;
}
}
export default function gulpChanged(destination, options) {
options = {
cwd: process.cwd(),
hasChanged: compareLastModifiedTime,
...options,
};
if (!destination) {
throw new Error('gulp-changed: `dest` required');
}
if (options.transformPath !== undefined && typeof options.transformPath !== 'function') {
throw new Error('gulp-changed: `options.transformPath` needs to be a function');
}
return gulpPlugin('gulp-changed', async file => {
const destination2 = typeof destination === 'function' ? destination(file) : destination;
let newPath = path.resolve(options.cwd, destination2, file.relative);
if (options.extension) {
newPath = changeFileExtension(newPath, options.extension);
}
if (options.transformPath) {
newPath = options.transformPath(newPath);
if (typeof newPath !== 'string') {
throw new TypeError('`options.transformPath` needs to return a string');
}
}
try {
return await options.hasChanged(file, newPath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
return file;
}
});
}