Skip to content

Commit 6c0f60d

Browse files
ddereszkiewiczdgp1130
authored andcommittedJun 27, 2024·
fix(@angular/cli): make ng update to keep newline at the end of package.json
As stated in #11744, `ng update` command removed the newline at the end of the package.json file. This commit makes `ng update` to preserve newline, if it was present before running the command. Fixes #11744 (cherry picked from commit 4947f29)
1 parent 3aff351 commit 6c0f60d

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed
 

‎packages/angular/cli/src/commands/update/schematic/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function _performUpdate(
263263
logger: logging.LoggerApi,
264264
migrateOnly: boolean,
265265
): void {
266-
const packageJsonContent = tree.read('/package.json');
266+
const packageJsonContent = tree.read('/package.json')?.toString();
267267
if (!packageJsonContent) {
268268
throw new SchematicsException('Could not find a package.json. Are you in a Node project?');
269269
}
@@ -310,11 +310,12 @@ function _performUpdate(
310310
logger.warn(`Package ${name} was not found in dependencies.`);
311311
}
312312
});
313-
314-
const newContent = JSON.stringify(packageJson, null, 2);
315-
if (packageJsonContent.toString() != newContent || migrateOnly) {
313+
const eofMatches = packageJsonContent.match(/\r?\n$/);
314+
const eof = eofMatches?.[0] ?? '';
315+
const newContent = JSON.stringify(packageJson, null, 2) + eof;
316+
if (packageJsonContent != newContent || migrateOnly) {
316317
if (!migrateOnly) {
317-
tree.overwrite('/package.json', JSON.stringify(packageJson, null, 2));
318+
tree.overwrite('/package.json', newContent);
318319
}
319320

320321
const externalMigrations: {}[] = [];

‎packages/angular/cli/src/commands/update/schematic/index_spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,57 @@ describe('@schematics/update', () => {
282282
expect(hasPeerdepMsg('typescript')).toBeTruthy();
283283
expect(hasPeerdepMsg('@angular/localize')).toBeFalsy();
284284
}, 45000);
285+
286+
it('does not remove newline at the end of package.json', async () => {
287+
const newlineStyles = ['\n', '\r\n'];
288+
for (const newline of newlineStyles) {
289+
const packageJsonContent = `{
290+
"name": "blah",
291+
"dependencies": {
292+
"@angular-devkit-tests/update-base": "1.0.0"
293+
}
294+
}${newline}`;
295+
const inputTree = new UnitTestTree(
296+
new HostTree(
297+
new virtualFs.test.TestHost({
298+
'/package.json': packageJsonContent,
299+
}),
300+
),
301+
);
302+
303+
const resultTree = await schematicRunner.runSchematic(
304+
'update',
305+
{ packages: ['@angular-devkit-tests/update-base'] },
306+
inputTree,
307+
);
308+
309+
const resultTreeContent = resultTree.readContent('/package.json');
310+
expect(resultTreeContent.endsWith(newline)).toBeTrue();
311+
}
312+
});
313+
314+
it('does not add a newline at the end of package.json', async () => {
315+
const packageJsonContent = `{
316+
"name": "blah",
317+
"dependencies": {
318+
"@angular-devkit-tests/update-base": "1.0.0"
319+
}
320+
}`;
321+
const inputTree = new UnitTestTree(
322+
new HostTree(
323+
new virtualFs.test.TestHost({
324+
'/package.json': packageJsonContent,
325+
}),
326+
),
327+
);
328+
329+
const resultTree = await schematicRunner.runSchematic(
330+
'update',
331+
{ packages: ['@angular-devkit-tests/update-base'] },
332+
inputTree,
333+
);
334+
335+
const resultTreeContent = resultTree.readContent('/package.json');
336+
expect(resultTreeContent.endsWith('}')).toBeTrue();
337+
});
285338
});

0 commit comments

Comments
 (0)
Please sign in to comment.