Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only save files if they are unchanged #417

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ function css(css, file) {
const tasks = []

if (options.to) {
tasks.push(fs.outputFile(options.to, result.css))
tasks.push(outputFile(options.to, result.css))

if (result.map) {
const mapfile = getMapfile(options)
tasks.push(fs.outputFile(mapfile, result.map.toString()))
tasks.push(outputFile(mapfile, result.map.toString()))
}
} else process.stdout.write(result.css, 'utf8')

Expand All @@ -278,6 +278,13 @@ function css(css, file) {
.catch((err) => {
throw err
})

async function outputFile(file, string) {
const fileExists = await fs.pathExists(file)
const currentValue = fileExists ? await fs.readFile(file, 'utf8') : null
if (currentValue === string) return
return fs.outputFile(file, string)
}
}

function dependencies(results) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/unchanged-input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: red;
}
5 changes: 5 additions & 0 deletions test/fixtures/unchanged-output.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/unchanged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import fs from 'fs-extra'
import test from 'ava'

import cli from './helpers/cli.js'

test('files are not saved if the contents are the same', async (t) => {
const input = 'test/fixtures/unchanged-input.css'
const output = 'test/fixtures/unchanged-output.css'
const intialStat = await fs.stat(output)

const { error, stderr } = await cli([input, '-o', output])

t.falsy(error, stderr)

const finalStat = await fs.stat(output)
t.is(finalStat.mtimeMs, intialStat.mtimeMs)
})