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

Fix sorting oneliners #6

Merged
merged 1 commit into from
Nov 21, 2024
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
20 changes: 16 additions & 4 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ const exampleA = {
sorted: writeMultiline(add => {
add('# AWS ACCESS TOKEN')
add('ACCESS_TOKEN=ID123')
add('')
add('HOME=/tmp')
add('')
add('PATH="HOME/lib/bin:$PATH"')
add('')
add('# Server environment')
Expand All @@ -108,14 +106,26 @@ const exampleB = {
}),
sorted: writeMultiline(add => {
add('ACME_COMMIT=4566')
add('')
add('ACME_VERSION=1234')
add('')
add('# Staging number which is used when forming domain name: app1.dev.example.com')
add('STAGING_NUMBER=45')
})
}

const exampleC = {
unsorted: writeMultiline(add => {
add('C=123')
add('B=456')
add('A=789')
}),
sorted: writeMultiline(add => {
add('A=789')
add('B=456')
add('C=123')
})
}

it('outputs properly', async () => {
process.argv = ['/usr/bin/node', 'index.js', await writeFile(exampleA.unsorted)]
await launchApp()
Expand All @@ -126,7 +136,8 @@ it('outputs properly', async () => {
it('overwrites file properly', async () => {
const fileA = await writeFile(exampleA.unsorted)
const fileB = await writeFile(exampleB.unsorted)
process.argv = ['/usr/bin/node', 'index.js', '-w', fileA, fileB]
const fileC = await writeFile(exampleC.unsorted)
process.argv = ['/usr/bin/node', 'index.js', '-w', fileA, fileB, fileC]

await launchApp()
expect(process.stderr.write).toBeCalled()
Expand All @@ -135,4 +146,5 @@ it('overwrites file properly', async () => {
const fs = require('node:fs/promises')
expect(await fs.readFile(fileA, 'utf-8')).toEqual(exampleA.sorted)
expect(await fs.readFile(fileB, 'utf-8')).toEqual(exampleB.sorted)
expect(await fs.readFile(fileC, 'utf-8')).toEqual(exampleC.sorted)
})
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ async function sortEnvContent (content) {
}
})

return groupedEnvFiles.join(NEWLINE + NEWLINE)
return groupedEnvFiles.reduce((output, elem) => {
if (output === null) return elem

const lines = elem.split(NEWLINE)
const shouldAddNewline = lines.length > 1 || lines[0].trim().startsWith('#')

return output + NEWLINE + (shouldAddNewline ? NEWLINE : '') + elem
}, null)
}

module.exports = main()
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@del-systems/sort-env-file",
"version": "1.0.1",
"version": "1.0.2",
"private": false,
"description": "Sort your .env files",
"keywords": [
Expand Down