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 #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)
  • Loading branch information
ddereszkiewicz authored and dgp1130 committed Jun 27, 2024
1 parent fc928f6 commit 67bf901
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
11 changes: 6 additions & 5 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 @@ -310,11 +310,12 @@ function _performUpdate(
logger.warn(`Package ${name} was not found in dependencies.`);
}
});

const newContent = JSON.stringify(packageJson, null, 2);
if (packageJsonContent.toString() != newContent || migrateOnly) {
const eofMatches = packageJsonContent.match(/\r?\n$/);
const eof = eofMatches?.[0] ?? '';
const newContent = JSON.stringify(packageJson, null, 2) + eof;
if (packageJsonContent != newContent || migrateOnly) {
if (!migrateOnly) {
tree.overwrite('/package.json', JSON.stringify(packageJson, null, 2));
tree.overwrite('/package.json', newContent);
}

const externalMigrations: {}[] = [];
Expand Down
53 changes: 53 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,57 @@ 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 newlineStyles = ['\n', '\r\n'];
for (const newline of newlineStyles) {
const packageJsonContent = `{
"name": "blah",
"dependencies": {
"@angular-devkit-tests/update-base": "1.0.0"
}
}${newline}`;
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(newline)).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('}')).toBeTrue();
});
});

0 comments on commit 67bf901

Please sign in to comment.