Skip to content

feat: add migration schematic from {N} to web+{N} shared project #3

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

Merged
merged 6 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ src/**/*.js.map
.idea/
jsconfig.json
.vscode/
.history

# Misc
node_modules/
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "@nativescript/schematics",
"version": "0.0.7",
"version": "0.0.8",
"description": "Schematics for NativeScript Angular apps.",
"scripts": {
"build": "tsc -p tsconfig.json",
"watch": "tsc -w -p tsconfig.json",
"test": "npm run build && jasmine **/*_spec.js",
"prepare": "npm run build"
"prepare": "npm run build",
"debug": "node --debug-brk ./node_modules/@angular/cli/bin/ng g command"
},
"schematics": "./src/collection.json",
"dependencies": {
"typescript": "~2.6.2",
"@angular-devkit/core": "^0.2.0",
"@angular-devkit/schematics": "^0.2.0",
"@schematics/angular": "^0.2.0",
"@angular-devkit/core": "~0.4.6",
"@angular-devkit/schematics": "~0.4.6",
"@schematics/angular": "~0.4.6",
"rxjs": "^5.5.6"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { stringUtils } from '../utils';
import { Schema as ApplicationOptions } from './schema';

export default function (options: ApplicationOptions) {
const appPath = options.name || '.';
const sourcedir = options.sourceDir || 'app';
const appPath = options.name;
const sourcedir = options.sourceDir;
const routing = options.routing && !options.minimal;

return chain([
Expand Down
6 changes: 6 additions & 0 deletions src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
"schema": "./ng-cli-config/schema.json"
},

"migrate-ns": {
"factory": "./migrate-ns",
"description": "Migrates {N} to a code sharing project structure",
"schema": "./migrate-ns/schema.json"
},

"class": {
"aliases": [ "cl" ],
"extends": "@schematics/angular:class"
Expand Down
29 changes: 17 additions & 12 deletions src/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,14 @@ export default function (options: ComponentOptions): Rule {

(tree: Tree) => {
extensions = getExtensions(tree, options);
return tree;
},

(tree: Tree, context: SchematicContext) => web(tree, options) ?
templateToWeb(templatePath)(tree) :
filter((path: Path) => !path.match(templatePath))(tree, context),

(tree: Tree) => ns(tree, options) ?
insertModuleId(componentPath)(tree) :
tree,
renameWebTemplate(templatePath)(tree) :
removeWebTemplate(templatePath)(tree, context),

(tree: Tree, context: SchematicContext) => ns(tree, options) ?
addFiles(options)(tree, context) :
performNsModifications(options, componentPath)(tree, context) :
tree,
]);
};
Expand All @@ -76,12 +71,24 @@ const validateOptions = (options: ComponentOptions) =>
}
};

const templateToWeb = (path: string) =>
const renameWebTemplate = (path: string) =>
(tree: Tree) => {
const webName = path.replace(".html", `${extensions.web}.html`);
tree.rename(path, webName);

return tree;
};

const removeWebTemplate = (templatePath: string | RegExp) =>
(tree: Tree, context: SchematicContext) =>
filter((path: Path) => !path.match(templatePath))(tree, context)

const performNsModifications = (options: ComponentOptions, componentPath: string) =>
(tree: Tree, context: SchematicContext) => {
insertModuleId(componentPath)(tree);
return addNativeScriptFiles(options)(tree, context);
}

const insertModuleId = (component: string) =>
(tree: Tree) => {
const componentSource = getSourceFile(tree, component);
Expand All @@ -94,11 +101,9 @@ const insertModuleId = (component: string) =>
recorder.insertRight(change.pos, change.toAdd)
);
tree.commitUpdate(recorder);

return tree;
};

const addFiles = (options: ComponentOptions) => {
const addNativeScriptFiles = (options: ComponentOptions) => {
const sourceDir = options.sourceDir;
if (!sourceDir) {
throw new SchematicsException(`sourceDir option is required.`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
<h1>
Welcome to {{ title }}!
</h1>
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
</div>
<h2>Here are some links to help you start: </h2>
<ul>
<li>
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
</li>
<li>
<h2><a target="_blank" rel="noopener" href="https://github.com/angular/angular-cli/wiki">CLI Documentation</a></h2>
</li>
<li>
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
</li>
</ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from '@angular/core';

@Component({
selector: '<%= indexAppRootTag %>',
templateUrl: './app.component.html'
})
export class <%= entryComponentName %> {
title = 'app';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { <%= entryComponentName %> } from '<%= entryComponentImportPath %>';


@NgModule({
declarations: [
<%= entryComponentName %>
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [<%= entryComponentName %>]
})
export class <%= entryModuleName %> { }
12 changes: 12 additions & 0 deletions src/migrate-ns/_files/__appRoot__/__main____webext__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { <%= entryModuleName %> } from '<%= entryModuleImportPath %>';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(<%= entryModuleName %>)
.catch(err => console.log(err));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const environment = {
production: true
};
8 changes: 8 additions & 0 deletions src/migrate-ns/_files/__appRoot__/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.

export const environment = {
production: false
};
Binary file added src/migrate-ns/_files/__appRoot__/favicon.ico
Binary file not shown.
14 changes: 14 additions & 0 deletions src/migrate-ns/_files/__appRoot__/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NgSample</title>
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<<%= indexAppRootTag %>></<%= indexAppRootTag %>>
</body>
</html>
66 changes: 66 additions & 0 deletions src/migrate-ns/_files/__appRoot__/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* This file includes polyfills needed by Angular and is loaded before the app.
* You can add your own extra polyfills to this file.
*
* This file is divided into 2 sections:
* 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers.
* 2. Application imports. Files imported after ZoneJS that should be loaded before your main
* file.
*
* The current setup is for so-called "evergreen" browsers; the last versions of browsers that
* automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera),
* Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile.
*
* Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html
*/

/***************************************************************************************************
* BROWSER POLYFILLS
*/

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
// import 'core-js/es6/symbol';
// import 'core-js/es6/object';
// import 'core-js/es6/function';
// import 'core-js/es6/parse-int';
// import 'core-js/es6/parse-float';
// import 'core-js/es6/number';
// import 'core-js/es6/math';
// import 'core-js/es6/string';
// import 'core-js/es6/date';
// import 'core-js/es6/array';
// import 'core-js/es6/regexp';
// import 'core-js/es6/map';
// import 'core-js/es6/weak-map';
// import 'core-js/es6/set';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
// import 'classlist.js'; // Run `npm install --save classlist.js`.

/** IE10 and IE11 requires the following for the Reflect API. */
// import 'core-js/es6/reflect';


/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es7/reflect';


/**
* Required to support Web Animations `@angular/platform-browser/animations`.
* Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation
**/
// import 'web-animations-js'; // Run `npm install --save web-animations-js`.



/***************************************************************************************************
* Zone JS is required by default for Angular itself.
*/
import 'zone.js/dist/zone'; // Included with Angular CLI.



/***************************************************************************************************
* APPLICATION IMPORTS
*/
1 change: 1 addition & 0 deletions src/migrate-ns/_files/__appRoot__/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* You can add global styles to this file, and also import other style files */
13 changes: 13 additions & 0 deletions src/migrate-ns/_files/__appRoot__/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
19 changes: 19 additions & 0 deletions src/migrate-ns/_files/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
60 changes: 60 additions & 0 deletions src/migrate-ns/_ngproject/__directory__/__dot__angular-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "<%= name %>"
},
"apps": [
{
"root": "<%= sourceDir %>",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"styles.css"
],
"scripts": [],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"project": "<%= sourceDir %>/tsconfig.app.json",
"exclude": "**/node_modules/**"
},
{
"project": "<%= sourceDir %>/tsconfig.spec.json",
"exclude": "**/node_modules/**"
},
{
"project": "e2e/tsconfig.e2e.json",
"exclude": "**/node_modules/**"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"component": {}
}
}
Loading