npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rainbow-js-codegeneration

v1.0.7

Published

JavaScript based code-generation for Rainbow serialization format of Sitecore items

Downloads

57

Readme

NPM

Build Status Dependencies Status

Node.js version NPM version

Watch on GitHub Star on GitHub PRs Welcome

Rainbow JS CodeGeneration

Greenkeeper badge

Installation

npm i rainbow-js-codegeneration --saveDev

API


The module exposes one method generate that accepts options object as a parameter

import { generator, IOptions } from 'rainbow-js-codegeneration';
//...
const options: IOptions = {
  cwd: path,
  pattern,
};

const result = await generator(options);
//...

generate(options)

This method search for files based on pattern provided, read them and construct templates objects. Once processing is done it complies Handlebars template and generates C# code.

options:

  • cwd: string (required) - defines a root directory for a glob search;
  • pattern: string|string[] (required) - defines a pattern for the glob search;
  • templatePath: string (default: standard template for GlassMapper embedded) - path to a Handlebars template file that will process templates info.;
  • Using: string[] - a list of using that should be rendered in the header of the file;
  • ToClass: function(name:string) (default: PascalCase class name) - overrides a logic that generates class names;
  • ToInterface: function(name:string) (default: class name prepended with 'I')- overrides a logic that generates interface names;
  • ToNamespace: function(path:string) (default: section of a path excluding /sitecore/templates and template name) - overrides a logic that generates namespace names;
  • ToProperty: function(name:string) (default: PascalCase property name)- overrides a logic that generates property names from field names;
  • ToPropertyType: function(type:string, id:string) (default: simple mapping to available types in GlassMapper) - extends logic related to mapping of a fields to C# tpe of a property. If return undefined will fall back to default.

context

The method will compile provided or embedded template and pass two objects there templates and options

Sample of a template file:

#pragma warning disable 1591
#pragma warning disable 0108
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by Rainbow JS CodeGeneration.
//     (https://github.com/asmagin/rainbow-js-codegeneration)
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Glass.Mapper.Sc.Configuration.Attributes;
using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Fields;
using Sitecore.Globalization;
using Sitecore.Data;
using Sitecore.Data.Items;

{{#each options.Using}}using {{this}};\n{{/each}}
{{#each templates}}

namespace {{this.AsNamespace}}
{

  /// <summary>
  /// {{this.AsInterface}} Interface
  /// <para>Path: {{this.Path}}</para>
  /// <para>ID: {{this.ID}}</para>
  /// </summary>
  [SitecoreType(TemplateId="{{this.ID}}")]
  public partial interface {{this.AsInterface}}: {{#each this.BaseTemplates}}{{this.AsInterface}}, {{/each}}IGlassBase
  {
    {{#each this.Fields}}

    /// <summary>
    /// The {{this.PropertyName}} field.
    /// <para>Field Type: {{this.Type}}</para>
    /// <para>Field ID: {{this.ID}}</para>
    /// </summary>
    [SitecoreField("{{this.Name}}")]
    {{{this.AsPropertyType}}} {{this.AsProperty}} {get; set;}
    {{/each}}

  }
}
{{/each}}

Template Helpers

guid-b: will convert text to UPPER CASE and wrap in { }. Could be used to emulate output of C# method .ToString("B")

{{guid-b this.ID}}
{{! will return "{00000000-ABCD-0000-0000-000000000000}"}}

guid-d: will convert text to UPPER CASE. Could be used to emulate output of C# method .ToString("D")

{{guid-b this.ID}}
{{! will return "00000000-ABCD-0000-0000-000000000000"}}

Gulp integration

generationPlugin()

The library provides a Gulp plugin. The plugin will generate code file for each configuration file detected. See example below:

// other imports
var codeGen = require('rainbow-js-codegeneration').generationPlugin;

// code generation task
gulp.task("Generate-Code", function (callback) {
  gulp.src('**/codegeneration.config.js', { base: "./" })
    .pipe(codeGen())
    .pipe(rename(function (path) {
      path.basename = "Templates.Generated";
      path.extname = ".cs"
    }))
    .pipe(gulp.dest('./'))
    .on("end", function () { // will make sure that you wait for generation to finish
      callback();
    });
});

Configuration

Configuration of the library could be provided via JS module. See example below:

var path = require("path");

module.exports = {
  cwd: path.join(__dirname, '..'),
  pattern: '**/serialization/*.Templates/**/*.yml',
  Using: [ 'System.CodeDom.Compiler' ],
  templatePath: path.join(__dirname, 'codegeneration.tmpl'),
}

Available scripts

  • clean - remove coverage data, Jest cache and transpiled files,
  • build - transpile TypeScript to ES6,
  • watch - interactive watch mode to automatically transpile source files,
  • lint - lint source files and tests,
  • test - run tests,
  • test:watch - interactive watch mode to automatically re-run tests

License

Licensed under the MIT. See the LICENSE file for details.