Skip to content

Commit

Permalink
feat(generators): subfolder option (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker authored Aug 27, 2018
1 parent 814e477 commit 5559bb9
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"**/scripts/**/*.js": true,
"**/scripts/**/*.js.map": true,
"**/src/**/*.js": {
"when": "$(basename).ts"
"when": "$(basename).ts"
},
"**/src/**/*.js.map": true
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nstudio/schematics",
"version": "6.2.4",
"version": "6.2.5",
"description": "Cross-platform (xplat) tools for Nx workspaces.",
"readmeFilename": "README.md",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/component/_base_index_files/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './<%= name %>.base-component';
<% if (!forSubFolder && subFolder) { %>export * from './<%= subFolder %>';<% } else { %>export * from './<%= name %>.base-component';<% } %>
2 changes: 1 addition & 1 deletion src/component/_index_files/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { <%= utils.classify(name) %>Component } from './<%= name %>/<%= name %>.component';

export const <%= utils.sanitize(name).toUpperCase() %>_COMPONENTS = [
export const <%= subFolder ? utils.sanitize(subFolder).toUpperCase() : utils.sanitize(name).toUpperCase() %>_COMPONENTS = [
<%= utils.classify(name) %>Component
];

Expand Down
163 changes: 84 additions & 79 deletions src/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,125 +138,130 @@ export default function(options: featureOptions) {
projectChains.push(noop());
}

return chain([
prerun(),
// add component for base libs feature
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && !options.ignoreBase
? addToFeature("component", options, "libs", tree, "_base")(
tree,
context
)
: noop()(tree, context),
// adjust libs barrel
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && !options.ignoreBase
? adjustBarrelIndex(
const platformChains = [];
for (const platform of supportedPlatforms) {
if (targetPlatforms[platform]) {
if (!options.onlyProject) {
// add component
platformChains.push((tree: Tree, context: SchematicContext) => {
return addToFeature(
"component",
options,
`libs/features/${featureName}/base/index.ts`,
false,
true
)(tree, context)
: noop()(tree, context),
// add index barrel if needed
(tree: Tree, context: SchematicContext) =>
options.needsIndex
? addToFeature("component", options, "libs", tree, "_base_index")(
`xplat/${platform}`,
tree,
context
)
: noop()(tree, context),

// add component for {N}
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && targetPlatforms.nativescript
? addToFeature(
"component",
options,
"xplat/nativescript",
tree,
"_nativescript"
)(tree, context)
: noop()(tree, context),
// adjust {N} components barrel
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && targetPlatforms.nativescript
? adjustBarrelIndex(
"component",
options,
`xplat/nativescript/features/${featureName}/components/index.ts`,
`_${platform}`,
true
)(tree, context)
: noop()(tree, context),
// add index barrel if needed
(tree: Tree, context: SchematicContext) =>
options.needsIndex
? addToFeature(
)(tree, context);
});
if (options.subFolder) {
// adjust components barrel for subFolder
platformChains.push((tree: Tree, context: SchematicContext) => {
return adjustBarrelIndex(
"component",
options,
`xplat/${platform}/features/${featureName}/components/${
options.subFolder
}/index.ts`,
true
)(tree, context);
});
platformChains.push((tree: Tree, context: SchematicContext) => {
return options.needsIndex
? addToFeature(
"component",
options,
`xplat/${platform}`,
tree,
"_index",
true
)(tree, context)
: noop()(tree, context);
});
}
// adjust overall components barrel
platformChains.push((tree: Tree, context: SchematicContext) => {
return adjustBarrelIndex(
"component",
options,
"xplat/nativescript",
tree,
"_index"
)(tree, context)
: noop()(tree, context),
`xplat/${platform}/features/${featureName}/components/index.ts`,
true,
false,
true
)(tree, context);
});

platformChains.push((tree: Tree, context: SchematicContext) => {
return options.needsIndex
? addToFeature(
"component",
options,
`xplat/${platform}`,
tree,
"_index"
)(tree, context)
: noop()(tree, context);
});
}
}
}
if (platformChains.length === 0) {
// none specified, insert noop
platformChains.push(noop());
}

// add component for web
return chain([
prerun(),
// add component for base libs feature
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && targetPlatforms.web
? addToFeature("component", options, "xplat/web", tree, "_web")(
!options.onlyProject && !options.ignoreBase
? addToFeature("component", options, "libs", tree, "_base", true)(
tree,
context
)
: noop()(tree, context),
// adjust web components barrel
// adjust libs barrel for subFolder
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && targetPlatforms.web
options.subFolder && !options.onlyProject && !options.ignoreBase
? adjustBarrelIndex(
"component",
options,
`xplat/web/features/${featureName}/components/index.ts`,
`libs/features/${featureName}/base/${options.subFolder}/index.ts`,
false,
true
)(tree, context)
: noop()(tree, context),
// add index barrel if needed
// add index barrel if needed for subFolder
(tree: Tree, context: SchematicContext) =>
options.needsIndex
? addToFeature("component", options, "xplat/web", tree, "_index")(
? addToFeature("component", options, "libs", tree, "_base_index", true)(
tree,
context
)
: noop()(tree, context),
// add component for ionic
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && targetPlatforms.ionic
? addToFeature("component", options, "xplat/ionic", tree, "_ionic")(
tree,
context
)
: noop()(tree, context),
// adjust ionic components barrel
// adjust libs barrel
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && targetPlatforms.ionic
!options.onlyProject && !options.ignoreBase
? adjustBarrelIndex(
"component",
options,
`xplat/ionic/features/${featureName}/components/index.ts`,
`libs/features/${featureName}/base/index.ts`,
false,
true
)(tree, context)
: noop()(tree, context),
// add index barrel if needed
(tree: Tree, context: SchematicContext) =>
options.needsIndex
? addToFeature("component", options, "xplat/ionic", tree, "_index")(
? addToFeature("component", options, "libs", tree, "_base_index")(
tree,
context
)
: noop()(tree, context),

// add platform chains
...platformChains,
// project handling
...projectChains,
options.skipFormat
? noop()
: formatFiles(options)
options.skipFormat ? noop() : formatFiles(options)
]);
}
70 changes: 70 additions & 0 deletions src/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,76 @@ describe('component schematic', () => {
expect(barrelIndex.indexOf(`SignupComponent\n];`)).toBeGreaterThanOrEqual(0);
});

it('should create component for specified platforms with subFolder option', () => {
// console.log('appTree:', appTree);
let tree = schematicRunner.runSchematic('xplat', {
prefix: 'tt'
}, appTree);
tree = schematicRunner.runSchematic('app.nativescript', {
name: 'viewer',
prefix: 'tt'
}, tree);
tree = schematicRunner.runSchematic('feature', {
name: 'foo',
platforms: 'nativescript,web'
}, tree);
const options: GenerateOptions = { ...defaultOptions };
options.subFolder = 'registration';
tree = schematicRunner.runSchematic('component', options, tree);
const files = tree.files;
// console.log(files.slice(91,files.length));

// component
expect(files.indexOf('/libs/features/foo/base/registration/signup.base-component.ts')).toBeGreaterThanOrEqual(0);
expect(files.indexOf('/xplat/nativescript/features/foo/components/registration/signup/signup.component.html')).toBeGreaterThanOrEqual(0);
expect(files.indexOf('/xplat/nativescript/features/foo/components/registration/signup/signup.component.ts')).toBeGreaterThanOrEqual(0);
expect(files.indexOf('/xplat/web/features/foo/components/registration/signup/signup.component.html')).toBeGreaterThanOrEqual(0);
expect(files.indexOf('/xplat/web/features/foo/components/registration/signup/signup.component.ts')).toBeGreaterThanOrEqual(0);

// ensure base index was modified
let barrelPath = '/libs/features/foo/base/registration/index.ts';
let barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(barrelIndex.indexOf(`./signup.base-component`)).toBeGreaterThanOrEqual(0);

barrelPath = '/libs/features/foo/base/index.ts';
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(barrelIndex.indexOf(`./registration`)).toBeGreaterThanOrEqual(0);

// file content
barrelPath = '/xplat/nativescript/features/foo/components/registration/index.ts';
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(barrelIndex.indexOf(`SignupComponent\n];`)).toBeGreaterThanOrEqual(0);
expect(barrelIndex.indexOf(`./signup/signup.component`)).toBeGreaterThanOrEqual(0);

barrelPath = '/xplat/web/features/foo/components/registration/index.ts';
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(barrelIndex.indexOf(`SignupComponent\n];`)).toBeGreaterThanOrEqual(0);

// file content
barrelPath = '/xplat/nativescript/features/foo/components/index.ts';
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
// component symbol should be at end of components collection
expect(barrelIndex.indexOf(`REGISTRATION_COMPONENTS`)).toBeGreaterThanOrEqual(0);

barrelPath = '/xplat/web/features/foo/components/index.ts';
barrelIndex = getFileContent(tree, barrelPath);
// console.log(barrelPath + ':');
// console.log(barrelIndex);
expect(barrelIndex.indexOf(`REGISTRATION_COMPONENTS`)).toBeGreaterThanOrEqual(0);
});

it('should create component for specified projects only', () => {
// console.log('appTree:', appTree);
let tree = schematicRunner.runSchematic('xplat', {
Expand Down
6 changes: 5 additions & 1 deletion src/component/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export interface Schema {
name: string;
/**
* Target feature
* Target feature. Default is 'ui' if none specified.
*/
feature?: string;
/**
* Group it in a subfolder of the target feature
*/
subFolder?: string;
/**
* Target apps
*/
Expand Down
6 changes: 5 additions & 1 deletion src/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
},
"feature": {
"type": "string",
"description": "Target feature"
"description": "Target feature. Default is 'ui' if none specified."
},
"subFolder": {
"type": "string",
"description": "Group it in a subfolder of the target feature."
},
"projects": {
"type": "string",
Expand Down
Loading

0 comments on commit 5559bb9

Please sign in to comment.