Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nativescript): adjustSandbox flag for feature module generator #93

Merged
merged 1 commit into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 60 additions & 18 deletions src/feature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mergeWith,
Tree,
SchematicContext,
// SchematicsException,
SchematicsException,
branchAndMerge,
// schematic,
Rule,
Expand All @@ -27,21 +27,26 @@ import {
generatorError,
optionsMissingError,
unsupportedPlatformError,
formatFiles
formatFiles,
supportedSandboxPlatforms,
PlatformTypes,
createOrUpdate
} from "../utils";
import { Schema as featureOptions } from "./schema";
import * as ts from "typescript";
import { getFileContent } from "@schematics/angular/utility/test";
import { capitalize } from "@angular-devkit/core/src/utils/strings";

let featureName: string;
let projectNames: Array<string>;
export default function(options: featureOptions) {
if (!options.name) {
throw new Error(
throw new SchematicsException(
`You did not specify the name of the feature you'd like to generate. For example: ng g feature my-feature`
);
}
if (options.routing && !options.onlyProject) {
throw new Error(
throw new SchematicsException(
`When generating a feature with the --routing option, please also specify --onlyProject. Support for shared code routing is under development and will be available in the future.`
);
}
Expand All @@ -66,17 +71,15 @@ export default function(options: featureOptions) {
platforms = options.platforms.split(",");
}
if (platforms.length === 0) {
let error = projects
? platformAppPrefixError()
: generatorError('feature');
throw new Error(optionsMissingError(error));
let error = projects ? platformAppPrefixError() : generatorError("feature");
throw new SchematicsException(optionsMissingError(error));
}
const targetPlatforms: ITargetPlatforms = {};
for (const t of platforms) {
if (supportedPlatforms.includes(t)) {
targetPlatforms[t] = true;
} else {
throw new Error(unsupportedPlatformError(t));
throw new SchematicsException(unsupportedPlatformError(t));
}
}

Expand All @@ -87,11 +90,9 @@ export default function(options: featureOptions) {
let srcDir = platPrefix !== "nativescript" ? "src/" : "";
// check for 2 different naming conventions on routing modules
const routingModulePathOptions = [];
const routingModulePath = `apps/${projectName}/${srcDir}app/`;
routingModulePathOptions.push(`${routingModulePath}app.routing.ts`);
routingModulePathOptions.push(
`${routingModulePath}app-routing.module.ts`
);
const appDirectory = `apps/${projectName}/${srcDir}app/`;
routingModulePathOptions.push(`${appDirectory}app.routing.ts`);
routingModulePathOptions.push(`${appDirectory}app-routing.module.ts`);

projectChains.push((tree: Tree, context: SchematicContext) => {
return addFiles(options, platPrefix, projectName)(tree, context);
Expand All @@ -103,6 +104,14 @@ export default function(options: featureOptions) {
context
);
});
if (options.adjustSandbox) {
projectChains.push((tree: Tree, context: SchematicContext) => {
return adjustSandbox(<PlatformTypes>platPrefix, appDirectory)(
tree,
context
);
});
}
}
if (!options.onlyModule) {
projectChains.push((tree: Tree, context: SchematicContext) => {
Expand Down Expand Up @@ -164,7 +173,9 @@ export default function(options: featureOptions) {
: noop()(tree, context),
// add starting component unless onlyModule
(tree: Tree, context: SchematicContext) =>
!options.onlyProject && !options.onlyModule && targetPlatforms.nativescript
!options.onlyProject &&
!options.onlyModule &&
targetPlatforms.nativescript
? addFiles(options, "nativescript", null, "_component")(tree, context)
: noop()(tree, context),
// ionic
Expand All @@ -184,9 +195,7 @@ export default function(options: featureOptions) {
: noop()(tree, context),
// project handling
...projectChains,
options.skipFormat
? noop()
: formatFiles(options)
options.skipFormat ? noop() : formatFiles(options)
]);
}

Expand Down Expand Up @@ -303,3 +312,36 @@ function adjustRouting(
return host;
};
}

function adjustSandbox(platform: PlatformTypes, appDirectory: string): Rule {
return (tree: Tree) => {
if (supportedSandboxPlatforms.includes(platform)) {
const homeCmpPath = `${appDirectory}/features/home/components/home.component.html`;
let homeTemplate = getFileContent(tree, homeCmpPath);
switch (platform) {
case "nativescript":
let buttonEndIndex = homeTemplate.indexOf("</Button>");
if (buttonEndIndex === -1) {
// check for lowercase
buttonEndIndex = homeTemplate.indexOf("</button>");
}
const featureNameParts = featureName.split("-");
let routeName = featureName;
if (featureNameParts.length > 1) {
routeName = capitalize(featureNameParts[featureNameParts.length - 1]);
}
homeTemplate =
homeTemplate.slice(0, buttonEndIndex + 9) +
`<Button text="${routeName}" (tap)="goTo('/${featureName}')" class="btn"></Button>` +
homeTemplate.slice(buttonEndIndex + 9);
break;
}
createOrUpdate(tree, homeCmpPath, homeTemplate);
} else {
throw new SchematicsException(
`The --adjustSandbox option is only supported on the following at the moment: ${supportedSandboxPlatforms}`
);
}
return tree;
};
}
46 changes: 45 additions & 1 deletion src/feature/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getFileContent } from '@schematics/angular/utility/test';
import * as path from 'path';

import { Schema as FeatureOptions } from './schema';
import { createXplatWithApps, isInModuleMetadata } from '../utils';
import { createXplatWithApps, isInModuleMetadata, createOrUpdate } from '../utils';

describe('feature schematic', () => {
const schematicRunner = new SchematicTestRunner(
Expand Down Expand Up @@ -348,4 +348,48 @@ describe('feature schematic', () => {
expect(featureModule).toMatch(`export const FOOWITHDASH_COMPONENTS`);

});

it('should create feature module for specified project WITH Routing and adjustSandbox', () => {
const options: FeatureOptions = {
...defaultOptions,
projects: 'nativescript-viewer'
};
let tree = schematicRunner.runSchematic('xplat', {
prefix: 'tt',
sample: true,
platforms: 'nativescript'
}, appTree);
tree = schematicRunner.runSchematic('app.nativescript', {
name: 'viewer',
prefix: 'tt',
routing: true
}, tree);

// manually update home.component to prep for sandobx
const homeCmpPath = `/apps/nativescript-viewer/app/features/home/components/home.component.html`;
createOrUpdate(tree, homeCmpPath, sandboxHomeSetup());
// console.log('homecmp:', getFileContent(tree, homeCmpPath));

options.onlyProject = true;
options.adjustSandbox = true;
options.routing = true;
options.name = 'foo-with-dash';
tree = schematicRunner.runSchematic('feature', options, tree);
// console.log('---------')
// console.log('homecmp:', getFileContent(tree, homeCmpPath));

});
});

export function sandboxHomeSetup() {
return `<ActionBar title="Sandbox" class="action-bar">
</ActionBar>
<StackLayout>
<ScrollView>
<StackLayout class="p-20">
<Button text="Buttons" (tap)="goTo('/page-buttons')" class="btn"></Button>
</StackLayout>
</ScrollView>
</StackLayout>
`;
}
4 changes: 4 additions & 0 deletions src/feature/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface Schema {
* Configure routing
*/
routing?: boolean;
/**
* Add link to route for sandbox
*/
adjustSandbox?: boolean;
/**
* Skip formatting
*/
Expand Down
5 changes: 5 additions & 0 deletions src/feature/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"type": "boolean",
"default": false
},
"adjustSandbox": {
"type": "boolean",
"description": "Automatically add a button to link to the feature route. Supported on NativeScript only right now. Requires flags: --onlyProject --routing",
"default": false
},
"skipFormat": {
"description": "Skip formatting files",
"type": "boolean",
Expand Down
2 changes: 2 additions & 0 deletions src/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface NodeDependency {
// list of all supported helpers
// TODO: add more convenient helpers (like firebase or Travis ci support files)
export const supportedHelpers = ['imports', 'applitools'];
// list of platforms that support adjustSandbox flag
export const supportedSandboxPlatforms: Array<PlatformTypes> = ['nativescript'];

let npmScope: string;
// selector prefix to use when generating various boilerplate for xplat support
Expand Down