Skip to content

Commit

Permalink
chore(schematics): replace template line endings with platform specific
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Dec 25, 2024
1 parent 416dd8b commit ef9165d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
15 changes: 14 additions & 1 deletion .github/workflows/schematic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
pull_request:

jobs:
schematics:
migration:
runs-on: ubuntu-latest
name: Run the latest migration
steps:
Expand All @@ -13,6 +13,19 @@ jobs:
id: info
- run: npx nx run cdk:schematics --v=${{ steps.info.outputs.root-package-major-version }}

unit-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
name: Run the unit test on ${{ matrix.os }}
steps:
- uses: actions/[email protected]
- uses: taiga-family/ci/actions/setup/[email protected]
- uses: taiga-family/ci/actions/run/[email protected]
- run: npx jest projects/cdk/schematics/**/*.spec.ts --coverage=false

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
2 changes: 2 additions & 0 deletions projects/cdk/schematics/ng-update/v4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {migrateEditor} from './steps/migrate-editor';
import {migrateImportProvidersFrom} from './steps/migrate-providers-from';
import {migrateRoot} from './steps/migrate-root';
import {replaceFunctions} from './steps/replace-functions';
import {replaceTemplateLineEndings} from './steps/replace-template-line-endings';
import {replaceModulesWithProviders} from './steps/utils/replace-modules-with-providers';

function main(options: TuiSchema): Rule {
Expand Down Expand Up @@ -104,6 +105,7 @@ export function updateToV4(options: TuiSchema): Rule {

return chain([
main(options),
(tree) => replaceTemplateLineEndings(getFileSystem(tree)),
() => {
const executionTime = getExecutionTime(t0, performance.now());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference lib="es2021" />
import {EOL} from 'node:os';

import type {FileSystem} from 'ng-morph';
import {getActiveProject, getSourceFiles, saveActiveProject} from 'ng-morph';

import {ALL_FILES} from '../../../constants';

export function replaceTemplateLineEndings(
fileSystem: FileSystem,
pattern = ALL_FILES,
): void {
getActiveProject();

getSourceFiles(pattern).forEach((file) => {
file.replaceWithText(file.getFullText().replaceAll(/\r?\n/g, EOL));
});

fileSystem.commitEdits();
saveActiveProject();
}
95 changes: 95 additions & 0 deletions projects/cdk/schematics/ng-update/v4/tests/replace-crlf.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {type} from 'node:os';
import {join} from 'node:path';

import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import type {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema';
import {
createProject,
createSourceFile,
resetActiveProject,
saveActiveProject,
setActiveProject,
} from 'ng-morph';

import {createAngularJson} from '../../../utils/create-angular-json';

const collectionPath = join(__dirname, '../../../migration.json');

const COMPONENT_BEFORE = `\r\n
@Component({
standalone: true,
templateUrl: './test.template.html',
})
export class Test {}\r\n`;

const COMPONENT_AFTER = type().startsWith('Windows')
? `\r\n
@Component({
standalone: true,
templateUrl: './test.template.html',
})
export class Test {}\r\n`
: `\n
@Component({
standalone: true,
templateUrl: './test.template.html',
})
export class Test {}\n`;

const TEMPLATE_BEFORE = '\r\n<p>Hello</p>\r\n';

const TEMPLATE_AFTER = type().startsWith('Windows')
? '\r\n<p>Hello</p>\r\n'
: '\n<p>Hello</p>\n';

describe('ng-update', () => {
let host: UnitTestTree;
let runner: SchematicTestRunner;

beforeEach(() => {
host = new UnitTestTree(new HostTree());
runner = new SchematicTestRunner('schematics', collectionPath);

setActiveProject(createProject(host));

createMainFiles();

saveActiveProject();
});

it('should migrate badge in template', async () => {
const tree = await runner.runSchematic(
'updateToV4',
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>,
host,
);

expect(tree.readContent('test/app/test.template.html')).toEqual(TEMPLATE_AFTER);
});

it('should migrate badge references in ts files', async () => {
const tree = await runner.runSchematic(
'updateToV4',
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>,
host,
);

expect(tree.readContent('test/app/test.component.ts')).toEqual(COMPONENT_AFTER);
});

afterEach(() => {
resetActiveProject();
});
});

function createMainFiles(): void {
createSourceFile('test/app/test.component.ts', COMPONENT_BEFORE);
createSourceFile('test/app/test.template.html', TEMPLATE_BEFORE);

createAngularJson();
createSourceFile(
'package.json',
'{\r\n"dependencies": {\r\n"@angular/core": "~13.0.0",\r\n "@taiga-ui/addon-commerce": "~3.42.0"\r\n}\r\n}',
);
}

0 comments on commit ef9165d

Please sign in to comment.