Code Monkey home page Code Monkey logo

schematics's Introduction

Build Status Coverage Status

Schematics

An elegant, powerful blueprints scaffold library, using RxJS and TypeScript.

Usage Examples

Basic example using strings

import {Compiler, MemorySource, Schematics, Variable} from 'schematics';
import {Observable} from 'rxjs/Observable';

export class MyExampleSchematics extends Schematics {
  @Variable() public userName: string = 'Roger';

  constructor(private _compiler: Compiler) {
    super();
  }

  build(): Observable<Entry> {
    return MemorySource.readFrom({
      '/test.txt': 'Roger <%- userName %>'
    }, this._compiler);
  }
}

Basic example using functions

import {Compiler, MemorySource, Schematics, Variable} from 'schematics';
import {Observable} from 'rxjs/Observable';

export class MyExampleFunctionSchematics extends Schematics {
  @Variable() public value: Number = 3;

  constructor(private _compiler: Compiler) {
    super();
  }

  build(): Observable<Entry> {
    return MemorySource.create({
      '/test2.txt': () => {
        if (typeof this.value == 'number') {
          return 'Showing a number: ' + this.value;
        } else {
          return 'This should not happen in TypeScript -_-';
        }
      }
    }, this._compiler);
  }
}

Using multiple schematics

import {Context, RegisterSchematic} from 'schematics';
import {MyExampleStringSchematics} from './example1';
import {MyExampleFunctionSchematics} from './example2';
import {Observable} from 'rxjs/Observable';

// Register this schematic with the global library.
@RegisterSchematic({ name: 'Multiple' })
class MyExample extends Schematic {
  constructor(private _s1: MyExampleStringSchematics,
              private _s2: MyExampleFunctionSchematics)  {}

  build(): Observable<Entry> {
    return this._s1.build().merge(this._s2.build());
  }

  transform(context: Context) {
    super.transform(context);
    this._s1.transform(context);
    this._s2.transform(context);
  }
}

Installing a Schematic

import {Library} from 'schematics';
// Include the module above which registers this with the main
// code.
import 'multiple';

// Declare a sink to get the output of the schematics installation.
// We're going to output this to '/tmp'. Paths are always normalized
// before being used in Schematics.
const sink = new FileSink('/tmp');

// Library.global is the global library.
Library.global.setSink(sink);  // This sets the default sink. It is possible
                               // to use multiple sinks.

// Create the context for the values to be used in the templates.
Library.global.setContext({
  userName: 'blesh',
  // Values that aren't used in schematics will, of course, be ignored.
  ignored1: 'value',
  ignored2: 1
  // Since we don't include 'value', MyExampleFunctionSchematics will use
  // its default of 3.
});

// Install it by using its name.  The install() method takes another
// argument to pass in a different sink, or a context. The two calls above
// simply set the defaults for those.
Library.global.install('Multiple');

schematics's People

Contributors

filipesilva avatar hansl avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.