Skip to content

Commit

Permalink
fix(@angular/cli): make ng update to keep newline at the end of pac…
Browse files Browse the repository at this point in the history
…kage.json

As stated in angular#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 angular#11744
  • Loading branch information
ddereszkiewicz committed Jun 21, 2024
1 parent 094efca commit d4a319a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/angular/cli/src/commands/update/schematic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function _performUpdate(
logger: logging.LoggerApi,
migrateOnly: boolean,
): void {
const packageJsonContent = tree.read('/package.json');
const packageJsonContent = tree.read('/package.json')?.toString();
if (!packageJsonContent) {
throw new SchematicsException('Could not find a package.json. Are you in a Node project?');
}
Expand Down Expand Up @@ -311,10 +311,11 @@ function _performUpdate(
}
});

const newContent = JSON.stringify(packageJson, null, 2);
if (packageJsonContent.toString() != newContent || migrateOnly) {
const newContent =
JSON.stringify(packageJson, null, 2) + (packageJsonContent.endsWith('\n') ? '\n' : '');
if (packageJsonContent != newContent || migrateOnly) {
if (!migrateOnly) {
tree.overwrite('/package.json', JSON.stringify(packageJson, null, 2));
tree.overwrite('/package.json', newContent);
}

const externalMigrations: {}[] = [];
Expand Down
50 changes: 50 additions & 0 deletions packages/angular/cli/src/commands/update/schematic/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,54 @@ describe('@schematics/update', () => {
expect(hasPeerdepMsg('typescript')).toBeTruthy();
expect(hasPeerdepMsg('@angular/localize')).toBeFalsy();
}, 45000);

it('does not remove newline at the end of package.json', async () => {
const packageJsonContent = `{
"name": "blah",
"dependencies": {
"@angular-devkit-tests/update-base": "1.0.0"
}
}\n`;
const inputTree = new UnitTestTree(
new HostTree(
new virtualFs.test.TestHost({
'/package.json': packageJsonContent,
}),
),
);

const resultTree = await schematicRunner.runSchematic(
'update',
{ packages: ['@angular-devkit-tests/update-base'] },
inputTree,
);

const resultTreeContent = resultTree.readContent('/package.json');
expect(resultTreeContent.endsWith('\n')).toBeTrue();
});

it('does not add a newline at the end of package.json', async () => {
const packageJsonContent = `{
"name": "blah",
"dependencies": {
"@angular-devkit-tests/update-base": "1.0.0"
}
}`;
const inputTree = new UnitTestTree(
new HostTree(
new virtualFs.test.TestHost({
'/package.json': packageJsonContent,
}),
),
);

const resultTree = await schematicRunner.runSchematic(
'update',
{ packages: ['@angular-devkit-tests/update-base'] },
inputTree,
);

const resultTreeContent = resultTree.readContent('/package.json');
expect(resultTreeContent.endsWith('\n')).toBeFalse();
});
});

0 comments on commit d4a319a

Please sign in to comment.