Skip to content
This repository was archived by the owner on Jun 4, 2023. It is now read-only.

Commit e714a2d

Browse files
authored
Merge pull request #5 from stackbit/skip-unchanged
Skip writing unchanged files
2 parents ef150aa + ba7bc52 commit e714a2d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/core/src/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
2-
32
const fs = require('fs');
43
const path = require('path');
4+
const _ = require('lodash');
55
const fse = require('fs-extra');
66
const yaml = require('js-yaml');
77
const toml = require('@iarna/toml');
@@ -77,11 +77,19 @@ function writeFiles(encodedFiles) {
7777
for (let i = 0; i < encodedFiles.length; i++) {
7878
const fullPath = path.join(process.cwd(), encodedFiles[i].filePath);
7979
fse.ensureDirSync(path.dirname(fullPath));
80-
if (fs.existsSync(fullPath) && ['yml', 'yaml', 'toml', 'json'].includes(path.extname(fullPath).substring(1))) {
81-
encodedFiles[i].data = mergeFile(fullPath, encodedFiles[i].data);
80+
let fileContentChanged = true;
81+
if (fs.existsSync(fullPath)) {
82+
if (['yml', 'yaml', 'toml', 'json'].includes(path.extname(fullPath).substring(1))) {
83+
encodedFiles[i].data = mergeFile(fullPath, encodedFiles[i].data);
84+
}
85+
fileContentChanged = !_.isEqual(encodedFiles[i].data, fs.readFileSync(fullPath, 'utf8'));
86+
}
87+
if (fileContentChanged) {
88+
console.log('creating file', fullPath);
89+
fs.writeFileSync(fullPath, encodedFiles[i].data);
90+
} else {
91+
console.log('content unchanged, skipping write', fullPath);
8292
}
83-
console.log('creating file', fullPath);
84-
fs.writeFileSync(fullPath, encodedFiles[i].data);
8593
}
8694
}
8795

0 commit comments

Comments
 (0)