Skip to content

Commit

Permalink
feat: set default theme also as default for override schematic (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi authored Apr 26, 2024
1 parent 456ce51 commit 4755c15
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
6 changes: 5 additions & 1 deletion schematics/customization/add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ const overrideSchematic = './schematics/src/helpers/override/schema.json';
const schematicsJson = parse(fs.readFileSync(overrideSchematic, { encoding: 'UTF-8' }));

if (!schematicsJson.properties.theme.enum.includes(theme)) {
schematicsJson.properties.theme.enum.unshift(theme);
if (setDefault) {
schematicsJson.properties.theme.enum = [theme, 'all'];
} else {
schematicsJson.properties.theme.enum.unshift(theme);
}
fs.writeFileSync(overrideSchematic, stringify(schematicsJson, null, 2));
execSync('npx prettier --write ' + overrideSchematic);
execSync('npm run build:schematics');
Expand Down
70 changes: 35 additions & 35 deletions schematics/src/helpers/override/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('override Schematic', () => {
});

it('should do nothing when no override was specified', async () => {
const tree = await runOverride({ from: 'src/app/foo/dummy/dummy.component.ts', theme: 'b2b' });
const tree = await runOverride({ from: 'src/app/foo/dummy/dummy.component.ts', theme: 'all' });

expect(tree.files.filter(x => x.includes('dummy.component'))).toMatchInlineSnapshot(`
[
Expand All @@ -114,64 +114,64 @@ describe('override Schematic', () => {
});

it('should throw an error if html override is specified on a non-component', async done => {
await runOverride({ from: 'core/services/dummy/dummy.service.ts', theme: 'b2b', html: true }).catch(err => {
await runOverride({ from: 'core/services/dummy/dummy.service.ts', theme: 'all', html: true }).catch(err => {
expect(err).toMatchInlineSnapshot(`[Error: Template and Style overrides only work on components.]`);
done();
});
});

it('should throw an error if scss override is specified on a non-component', async done => {
await runOverride({ from: 'core/services/dummy/dummy.service.ts', theme: 'b2b', scss: true }).catch(err => {
await runOverride({ from: 'core/services/dummy/dummy.service.ts', theme: 'all', scss: true }).catch(err => {
expect(err).toMatchInlineSnapshot(`[Error: Template and Style overrides only work on components.]`);
done();
});
});

it('should override component html if specified', async () => {
const tree = await runOverride({ from: 'foo/dummy/dummy.component.ts', theme: 'b2b', html: true });
const tree = await runOverride({ from: 'foo/dummy/dummy.component.ts', theme: 'all', html: true });

expect(tree.files.filter(x => x.includes('dummy.component'))).toMatchInlineSnapshot(`
[
"/src/app/foo/dummy/dummy.component.html",
"/src/app/foo/dummy/dummy.component.spec.ts",
"/src/app/foo/dummy/dummy.component.ts",
"/src/app/foo/dummy/dummy.component.b2b.html",
"/src/app/foo/dummy/dummy.component.all.html",
]
`);

expect(appTree.readContent('/src/app/foo/dummy/dummy.component.b2b.html')).toMatchInlineSnapshot(`"OVERRIDE"`);
expect(appTree.readContent('/src/app/foo/dummy/dummy.component.all.html')).toMatchInlineSnapshot(`"OVERRIDE"`);
});

it('should override component scss for components with css if specified', async () => {
const tree = await runOverride({ from: 'src/app/foo/foobar/foobar.component.ts', theme: 'b2b', scss: true });
const tree = await runOverride({ from: 'src/app/foo/foobar/foobar.component.ts', theme: 'all', scss: true });

expect(tree.files.filter(x => x.includes('foobar.component'))).toMatchInlineSnapshot(`
[
"/src/app/foo/foobar/foobar.component.html",
"/src/app/foo/foobar/foobar.component.scss",
"/src/app/foo/foobar/foobar.component.spec.ts",
"/src/app/foo/foobar/foobar.component.ts",
"/src/app/foo/foobar/foobar.component.b2b.scss",
"/src/app/foo/foobar/foobar.component.all.scss",
]
`);

expect(appTree.exists('/src/app/foo/foobar/foobar.component.b2b.scss')).toBeTrue();
expect(appTree.exists('/src/app/foo/foobar/foobar.component.all.scss')).toBeTrue();
});

it('should override component scss for components without it', async () => {
const tree = await runOverride({ from: 'foo/dummy/dummy.component.ts', theme: 'b2b', scss: true });
const tree = await runOverride({ from: 'foo/dummy/dummy.component.ts', theme: 'all', scss: true });

expect(tree.files.filter(x => x.includes('dummy.component'))).toMatchInlineSnapshot(`
[
"/src/app/foo/dummy/dummy.component.html",
"/src/app/foo/dummy/dummy.component.spec.ts",
"/src/app/foo/dummy/dummy.component.ts",
"/src/app/foo/dummy/dummy.component.scss",
"/src/app/foo/dummy/dummy.component.b2b.scss",
"/src/app/foo/dummy/dummy.component.all.scss",
]
`);

expect(appTree.exists('/src/app/foo/dummy/dummy.component.b2b.scss')).toBeTrue();
expect(appTree.exists('/src/app/foo/dummy/dummy.component.all.scss')).toBeTrue();
expect(appTree.exists('/src/app/foo/dummy/dummy.component.scss')).toBeTrue();
const dummyComponent = appTree.readContent('/src/app/foo/dummy/dummy.component.ts');
expect(componentDecorator(dummyComponent)).toMatchInlineSnapshot(
Expand All @@ -180,26 +180,26 @@ describe('override Schematic', () => {
});

it('should override component ts if specified', async () => {
const tree = await runOverride({ from: 'foo/dummy/dummy.component.ts', theme: 'b2b', ts: true });
const tree = await runOverride({ from: 'foo/dummy/dummy.component.ts', theme: 'all', ts: true });

expect(tree.files.filter(x => x.includes('dummy.component'))).toMatchInlineSnapshot(`
[
"/src/app/foo/dummy/dummy.component.html",
"/src/app/foo/dummy/dummy.component.spec.ts",
"/src/app/foo/dummy/dummy.component.ts",
"/src/app/foo/dummy/dummy.component.b2b.ts",
"/src/app/foo/dummy/dummy.component.all.ts",
]
`);

expect(appTree.readContent('/src/app/foo/dummy/dummy.component.b2b.ts')).toEqual(
expect(appTree.readContent('/src/app/foo/dummy/dummy.component.all.ts')).toEqual(
appTree.readContent('/src/app/foo/dummy/dummy.component.ts')
);
});

it('should override everything on components with css if specified', async () => {
const tree = await runOverride({
from: 'src/app/foo/foobar/foobar.component.ts',
theme: 'b2b',
theme: 'all',
scss: true,
html: true,
ts: true,
Expand All @@ -211,23 +211,23 @@ describe('override Schematic', () => {
"/src/app/foo/foobar/foobar.component.scss",
"/src/app/foo/foobar/foobar.component.spec.ts",
"/src/app/foo/foobar/foobar.component.ts",
"/src/app/foo/foobar/foobar.component.b2b.html",
"/src/app/foo/foobar/foobar.component.b2b.scss",
"/src/app/foo/foobar/foobar.component.b2b.ts",
"/src/app/foo/foobar/foobar.component.all.html",
"/src/app/foo/foobar/foobar.component.all.scss",
"/src/app/foo/foobar/foobar.component.all.ts",
]
`);

expect(appTree.exists('/src/app/foo/foobar/foobar.component.b2b.scss')).toBeTrue();
expect(appTree.readContent('/src/app/foo/foobar/foobar.component.b2b.html')).toMatchInlineSnapshot(`"OVERRIDE"`);
expect(appTree.readContent('/src/app/foo/foobar/foobar.component.b2b.ts')).toEqual(
expect(appTree.exists('/src/app/foo/foobar/foobar.component.all.scss')).toBeTrue();
expect(appTree.readContent('/src/app/foo/foobar/foobar.component.all.html')).toMatchInlineSnapshot(`"OVERRIDE"`);
expect(appTree.readContent('/src/app/foo/foobar/foobar.component.all.ts')).toEqual(
appTree.readContent('/src/app/foo/foobar/foobar.component.ts')
);
});

it('should override everything on components without css if specified', async () => {
const tree = await runOverride({
from: 'foo/dummy/dummy.component.ts',
theme: 'b2b',
theme: 'all',
scss: true,
html: true,
ts: true,
Expand All @@ -238,62 +238,62 @@ describe('override Schematic', () => {
"/src/app/foo/dummy/dummy.component.html",
"/src/app/foo/dummy/dummy.component.spec.ts",
"/src/app/foo/dummy/dummy.component.ts",
"/src/app/foo/dummy/dummy.component.b2b.html",
"/src/app/foo/dummy/dummy.component.all.html",
"/src/app/foo/dummy/dummy.component.scss",
"/src/app/foo/dummy/dummy.component.b2b.scss",
"/src/app/foo/dummy/dummy.component.b2b.ts",
"/src/app/foo/dummy/dummy.component.all.scss",
"/src/app/foo/dummy/dummy.component.all.ts",
]
`);

expect(appTree.exists('/src/app/foo/dummy/dummy.component.b2b.scss')).toBeTrue();
expect(appTree.readContent('/src/app/foo/dummy/dummy.component.b2b.html')).toMatchInlineSnapshot(`"OVERRIDE"`);
expect(appTree.readContent('/src/app/foo/dummy/dummy.component.b2b.ts')).toEqual(
expect(appTree.exists('/src/app/foo/dummy/dummy.component.all.scss')).toBeTrue();
expect(appTree.readContent('/src/app/foo/dummy/dummy.component.all.html')).toMatchInlineSnapshot(`"OVERRIDE"`);
expect(appTree.readContent('/src/app/foo/dummy/dummy.component.all.ts')).toEqual(
appTree.readContent('/src/app/foo/dummy/dummy.component.ts')
);
});

it('should override service ts if requested', async () => {
const tree = await runOverride({
from: 'src/app/core/services/dummy/dummy.service.ts',
theme: 'b2b',
theme: 'all',
ts: true,
});

expect(tree.files.filter(x => x.includes('dummy.service'))).toMatchInlineSnapshot(`
[
"/src/app/core/services/dummy/dummy.service.spec.ts",
"/src/app/core/services/dummy/dummy.service.ts",
"/src/app/core/services/dummy/dummy.service.b2b.ts",
"/src/app/core/services/dummy/dummy.service.all.ts",
]
`);
});

it('should override any ts if requested', async () => {
const tree = await runOverride({
from: 'core/routing/product.route.ts',
theme: 'b2b',
theme: 'all',
ts: true,
});

expect(tree.files.filter(x => x.includes('product.route'))).toMatchInlineSnapshot(`
[
"/src/app/core/routing/product.route.ts",
"/src/app/core/routing/product.route.b2b.ts",
"/src/app/core/routing/product.route.all.ts",
]
`);
});

it('should override file if path is windows-styled', async () => {
const tree = await runOverride({
from: 'core\\routing\\product.route.ts',
theme: 'b2b',
theme: 'all',
ts: true,
});

expect(tree.files.filter(x => x.includes('product.route'))).toMatchInlineSnapshot(`
[
"/src/app/core/routing/product.route.ts",
"/src/app/core/routing/product.route.b2b.ts",
"/src/app/core/routing/product.route.all.ts",
]
`);
});
Expand Down

0 comments on commit 4755c15

Please sign in to comment.