Skip to content

Commit 6e340ab

Browse files
committed
fix: hotfix preserve final newline
1 parent 259b051 commit 6e340ab

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const modifyJsonFile: ModifyJsonFileGenericFunction = async (path, modify
9898
removeJsonc = false,
9999
} = options
100100
try {
101-
let { json, indent } = await loadJsonFile(path, { encoding, tabSize, removeJsonc })
101+
let { json, indent, hasFinalNewline } = await loadJsonFile(path, { encoding, tabSize, removeJsonc })
102102
if (typeof modifyProperties === 'function') {
103103
// TODO why arg is never
104104
json = await (modifyProperties as any)(json)
@@ -120,7 +120,10 @@ export const modifyJsonFile: ModifyJsonFileGenericFunction = async (path, modify
120120
}
121121
}
122122

123-
await fs.promises.writeFile(path, JSON.stringify(json, undefined, indent))
123+
// TODO don't use this fix
124+
let string = JSON.stringify(json, undefined, indent)
125+
if (hasFinalNewline) string += '\n'
126+
await fs.promises.writeFile(path, string)
124127
} catch (err) {
125128
if (throws) throw err
126129
}

src/loadJsonFile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ export const loadJsonFile = async (filePath: string, { encoding, tabSize, remove
1919
return {
2020
json: parseJson(contents, filePath) as JsonRoot,
2121
indent: tabSize === 'preserve' ? detectIndent(contents).indent : tabSize === 'hard' ? '\t' : tabSize === null ? undefined : ' '.repeat(tabSize),
22+
hasFinalNewline: contents.split('\n').slice(-1)[0]! === '',
2223
}
2324
}

tests/index.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,47 @@ test("Don't throw error if option is provided on missing property in input", asy
190190
)
191191
})
192192

193+
test('Preserves empty newline', async () => {
194+
expect.assertions(1)
195+
prepare({
196+
data: `
197+
{
198+
"author": "eldar",
199+
"bin": "src/bin.ts",
200+
"dependencies": {
201+
"fdir": ">=2",
202+
"type-fest": "*"
203+
}
204+
}
205+
`,
206+
json: false,
207+
writeCallback(data) {
208+
expect(data).toMatchInlineSnapshot(`
209+
"{
210+
\\"author\\": \\"eldar\\",
211+
\\"bin\\": \\"build/bin.js\\",
212+
\\"dependencies\\": {
213+
\\"fdir\\": \\">=2\\",
214+
\\"type-fest\\": \\"*\\"
215+
},
216+
\\"somethingWeird\\": \\"new-item\\"
217+
}
218+
"
219+
`)
220+
},
221+
})
222+
await modifyJsonFile(
223+
'',
224+
{
225+
bin: 'build/bin.js',
226+
somethingWeird: () => 'new-item',
227+
},
228+
{
229+
ifPropertyIsMissingForSetter: 'pass',
230+
},
231+
)
232+
})
233+
193234
test('loader removes comments and trailing commas', async () => {
194235
const { json } = await loadJsonFile(join(__dirname, './tsconfig.fixture.json'), { encoding: 'utf-8', removeJsonc: true, tabSize: 'preserve' })
195236
expect(json).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)