Skip to content

Commit d0160f0

Browse files
sebawitasis0k0
authored andcommitted
feat: generate stylesheets when migrating components and modules (#128)
use `--no-style` to skip the generation of [css | scss] files
1 parent db9ada9 commit d0160f0

File tree

10 files changed

+77
-6
lines changed

10 files changed

+77
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
Add your NativeScript specific styles here.
3+
To learn more about styling in NativeScript see:
4+
https://docs.nativescript.org/angular/ui/styling
5+
*/

src/migrate-component/component-info-utils.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface ComponentInfo {
1515
modulePath: string;
1616
componentPath: string;
1717
componentHtmlPath: string;
18-
// componentStylePath: string;
18+
componentStylePath: string;
1919
}
2020

2121
let projectSettings: AngularProjectSettings;
@@ -33,11 +33,14 @@ export const parseComponentInfo = (options: MigrateComponentSchema) => (tree: Tr
3333
const componentPath = findComponentPath(className, modulePath, options, tree);
3434
const componentHtmlPath = findTemplateUrl(componentPath, className, tree);
3535

36+
const componentStylePath = findStyleUrl(componentPath, className, tree);
37+
3638
const componentInfo: ComponentInfo = {
3739
className,
3840
modulePath,
3941
componentPath,
40-
componentHtmlPath
42+
componentHtmlPath,
43+
componentStylePath
4144
}
4245

4346
context.logger.info(`ComponentInfo
@@ -179,3 +182,23 @@ const findTemplateUrl = (componentPath: string, componentClassName: string, tree
179182
}
180183
}
181184

185+
const findStyleUrl = (componentPath: string, componentClassName: string, tree: Tree): string => {
186+
const source = getSourceFile(tree, componentPath);
187+
188+
const node = findDecoratorPropertyNode(source, componentClassName, 'Component', 'styleUrls');
189+
if (node === null) {
190+
return '';
191+
}
192+
193+
if (ts.isArrayLiteralExpression(node) && node.elements.length > 0) {
194+
const stylePath = (node.elements[0] as ts.StringLiteral).text;
195+
196+
return join(
197+
dirname(componentPath),
198+
stylePath
199+
);
200+
}
201+
202+
return '';
203+
}
204+

src/migrate-component/index.ts

+24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
branchAndMerge,
1010
mergeWith,
1111
SchematicsException,
12+
noop,
1213
} from '@angular-devkit/schematics';
1314
import { InsertChange } from '@schematics/angular/utility/change';
1415
import { addDeclarationToModule } from '@schematics/angular/utility/ast-utils';
@@ -40,6 +41,9 @@ export default function(options: MigrateComponentSchema): Rule {
4041
(tree: Tree, context: SchematicContext) =>
4142
addNsFiles(componentInfo, options)(tree, context),
4243

44+
(tree: Tree, context: SchematicContext) =>
45+
addNsStyle(componentInfo, options)(tree, context),
46+
4347
(tree: Tree) =>
4448
addComponentToNsModuleProviders(componentInfo, options)(tree)
4549
]);
@@ -104,3 +108,23 @@ or if you just want to update the component without updating its module, then re
104108
);
105109
tree.commitUpdate(recorder);
106110
}
111+
112+
const addNsStyle = (componentInfo: ComponentInfo, options: MigrateComponentSchema) => (tree: Tree, context: SchematicContext) => {
113+
if (!componentInfo.componentStylePath || !options.style) {
114+
return noop;
115+
}
116+
117+
context.logger.info('Adding {N} StyleSheet');
118+
119+
const templateOptions = {
120+
path: dirname(componentInfo.componentHtmlPath),
121+
styleFileName: basename(componentInfo.componentStylePath),
122+
123+
addNsExtension: (path: string) => addExtension(path, extensions.ns),
124+
};
125+
126+
const templateSource = apply(url('./_ns-style'), [
127+
template(templateOptions)
128+
]);
129+
return branchAndMerge(mergeWith(templateSource))(tree, context);
130+
}

src/migrate-component/index_spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('Migrate component schematic', () => {
2929
const options: MigrateComponentOptions = {
3030
project,
3131
name: componentName,
32+
style: true
3233
};
3334
const htmlComponentPath = `/src/app/${componentName}/${componentName}.component.html`;
3435
const xmlComponentPath = `/src/app/${componentName}/${componentName}.component.tns.html`;
@@ -70,6 +71,7 @@ describe('Migrate component schematic', () => {
7071
project,
7172
name: componentName,
7273
module: moduleName,
74+
style: true
7375
};
7476
const htmlComponentPath = `/src/app/${componentName}/${componentName}.component.html`;
7577
const xmlComponentPath = `/src/app/${componentName}/${componentName}.component.tns.html`;

src/migrate-component/schema.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export interface Schema {
1919
* Notifies us that the component doesn't belong to any module.
2020
*/
2121
skipModule?: boolean;
22-
22+
/**
23+
* Specifies whether stylesheets should be generated.
24+
*/
25+
style: boolean;
2326
nsext?: string;
2427
webext?: string;
2528
/**

src/migrate-component/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
"skipModule": {
2323
"type": "boolean"
2424
},
25+
"style": {
26+
"type": "boolean",
27+
"default": true
28+
},
2529
"nsext": {
2630
"type": "string"
2731
},

src/migrate-module/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function(options: MigrateModuleSchema): Rule {
3737

3838
addModuleFile(options.name, options.project),
3939

40-
(tree, context) => migrateComponents(moduleInfo, options.project)(tree, context),
40+
(tree, context) => migrateComponents(moduleInfo, options)(tree, context),
4141
migrateProviders()
4242
]);
4343
}
@@ -55,7 +55,7 @@ const addModuleFile =
5555
common: true,
5656
})(tree, context);
5757

58-
const migrateComponents = (moduleInfo: ModuleInfo, project: string) => {
58+
const migrateComponents = (moduleInfo: ModuleInfo, options: MigrateModuleSchema) => {
5959
const components = moduleInfo.declarations.filter(d => d.name.endsWith('Component'));
6060

6161
return chain(
@@ -64,7 +64,8 @@ const migrateComponents = (moduleInfo: ModuleInfo, project: string) => {
6464
name: component.name,
6565
modulePath: moduleInfo.modulePath,
6666
nsext,
67-
project,
67+
project: options.project,
68+
style: options.style
6869
}
6970
return schematic<MigrateComponentSchema>('migrate-component', convertComponentOptions);
7071
}),

src/migrate-module/index_spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Migrate module Schematic', () => {
1919
const defaultOptions: MigrateModuleOptions = {
2020
name: moduleName,
2121
project,
22+
style: true
2223
};
2324
const nsModulePath = '/src/app/admin/admin.module.tns.ts';
2425
const webModulePath = '/src/app/admin/admin.module.ts';

src/migrate-module/schema.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface Schema {
77
* Specifies the module path.
88
*/
99
modulePath?: string;
10+
/**
11+
* Specifies whether stylesheets should be generated for all components.
12+
*/
13+
style: boolean;
1014
nsext?: string;
1115
/**
1216
* Allows specification of the project to be updated

src/migrate-module/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"type": "string",
1313
"alias": "p"
1414
},
15+
"style": {
16+
"type": "boolean",
17+
"default": true
18+
},
1519
"nsext": {
1620
"type": "string"
1721
},

0 commit comments

Comments
 (0)