1.0.0 • Published 9 years ago

pal-cli v1.0.0

Weekly downloads
2
License
MIT
Repository
-
Last release
9 years ago

Pal

Pal is a NodeJS command line tool for quickly generating Angular controllers, directives, factories and services from templates. It takes away the pain of manually creating them or remembering their syntax every time you want to use them.

Installation

Pal is available as a Node module on npm. Make sure you have Node and npm installed and run the following to install Pal and make it available globally on your system:

npm install -g pal-cli

Configuration

Pal uses a simple JSON configuration file .palconfig in the root directory of your project. You can use Pal without a .palconfig file, but if you want to override the default options, you have to create one. Use the pal make:config command to create a .palconfig file.

Default .palconfig

{
	"angular": {
		"controller_dir": "./angular/controllers",
		"service_dir": "./angular/services",
		"factory_dir": "./angular/factories",
		"directive_dir": "./angular/directives"
	},
	"template_dir": ""
}

Available commands

make:controller

Create a new Angular controller in the angular.controller_dir specified in .palconfig (or default to angular/controllers).

Syntax

pal make:controller <name> -d '[dependencies]' -m [module] [-a|-c]

Options:

  • -m or --module: Angular module on which to create the controller. Default: app
  • -d or --dependencies: A comma-separated list of dependencies to inject into the controller. Wrap in quotes to preserve special characters like $
  • -a or --array_syntax: Toggle Use array syntax for dependency injection
  • -c or --classy: Toggle Use Angular Classy syntax for controller syntax

Example

pal make:controller LoginCtrl -d '$scope,$http,$q,$timeout' -m myModule -a

Running the command above will create a new controller in the file LoginCtrl.js with the following contents:

/**
 * Login controller
 */
myModule.controller('LoginCtrl', ['$scope', '$http', '$q', '$timeout', function($scope, $http, $q, $timeout) {
	
}]);

make:directive

Create a new Angular directive in the angular.directive_dir specified in .palconfig (or default to angular/directives).

Syntax

pal make:directive <name> -d '[dependencies]' -m [module] -t [template url] -r [restrict type] [-a]

Options:

  • -m or --module: Angular module on which to create the directive. Default: app
  • -d or --dependencies: A comma-separated list of dependencies to inject into the directive. Wrap in quotes to preserve special characters like $
  • -t or --template: Template URL for the directive
  • -r or --restrict: Directive restrict type: A, E or AE. Default: E
  • -a or --array_syntax: Toggle Use array syntax for dependency injection

Example

pal make:directive svgIcons -d '$scope,$timeout' -m myModule -t 'templates/svg-icon.html' -r AE -a

Running the command above will create a new directive in the file svgIcons.js with the following contents:

/**
 * svg Icons (directive)
 */
myModule.directive('svgIcons', ['$scope', '$timeout', function($scope, $timeout) {
    return {
        restrict: 'AE',
        templateUrl: 'templates/svg-icon.html',
        link: function(scope, element, attr) {
            
        }
    };
}]);

make:factory

Create a new Angular factory in the angular.factory_dir specified in .palconfig (or default to angular/factories).

Syntax

pal make:factory <name> -d '[dependencies]' -m [module] [-a]

Options:

  • -m or --module: Angular module on which to create the factory. Default: app
  • -d or --dependencies: A comma-separated list of dependencies to inject into the factory. Wrap in quotes to preserve special characters like $
  • -a or --array_syntax: Toggle Use array syntax for dependency injection

Example

pal make:factory Users -d '$http,$q,AccountService' -m myModule -a

Running the command above will create a new factory in the file Users.js with the following contents:

/**
 * Users (factory)
 */
myModule.factory('Users', ['$http', '$q', 'AccountService', function($http, $q, AccountService) {
    return {

    };
}]);

make:service

Create a new Angular service in the angular.service_dir specified in .palconfig (or default to angular/services).

Syntax

pal make:service <name> -d '[dependencies]' -m [module] [-a]

Options:

  • -m or --module: Angular module on which to create the service. Default: app
  • -d or --dependencies: A comma-separated list of dependencies to inject into the service. Wrap in quotes to preserve special characters like $
  • -a or --array_syntax: Toggle Use array syntax for dependency injection

Example

pal make:service AccountService -d '$http,$q' -m myModule -a

Running the command above will create a new service in the file AccountService.js with the following contents:

/**
 * Account Service (service)
 */
myModule.service('AccountService', ['$http', '$q', function($http, $q) {
    
}]);

make:config

Create a .palconfig file with default options in the current directory

Syntax

pal make:config

Example

pal make:config

Running the command above will create a .palconfig file with the following contents:

{
	"angular": {
		"controller_dir": "./angular/controllers",
		"service_dir": "./angular/services",
		"factory_dir": "./angular/factories",
		"directive_dir": "./angular/directives"
	},
	"template_dir": ""
}

make:templates

Create sample templates for use with Pal in the template_dir specified in .palconfig (or default to pal_templates/).

Syntax

pal make:templates [directory]

Example

pal make:templates custom_dir

Running the command above will copy the default template files into the folder custom_dir. It will also set the template_dir option in .palconfig to custom_dir. A .palconfig file will be created if it doesn't already exist.

Default template files:

  • controller.js
  • controller_array.js
  • controller_classy.js
  • directive.js
  • directive_array.js
  • factory.js
  • factory_array.js
  • service.js
  • service_array.js

Using custom templates

Pal allows you to specify custom templates from which the controllers, directives, factories and services will be generated. The following command will copy the default templates into your project root in a pal_templates folder. It will also set the template_dir in .palconfig to pal_templates.

pal make:templates pal_templates

Keywords

You can customize the templates however you see fit, placing the following keywords anywhere within the files:

  • $NAME$: Name of the controller, directive, factory or service
  • $FRIENDLY_NAME$: Human-friendly name of the controller, directive, factory or service (for use in comments)
  • $MODULE_NAME$: Name of the module on which to create the controller, directive, factory or service
  • $DEPENDENCIES$: Comma-separated list of dependencies to inject into the controller, directive, factory or service
  • $DEPENDENCIES_STRING$: Comma-separated list of dependencies with each wrapped in single quotes
  • $TEMPLATE_PATH$: Directive only Path of the directive's template file
  • $RESTRICT_TYPE$: Directive only Directive restrict type: A, E or AE

Template files:

  • controller.js: Used for command make:controller
  • controller_array.js: Used for commands make:controller -a and make:controller --array_syntax
  • controller_classy.js: Used for commands make:controller -c and make:controller --classy
  • directive.js: Used for command make:directive
  • directive_array.js: Used for commands make:directive -a and make:directive --array_syntax
  • factory.js: Used for command make:factory
  • factory_array.js: Used for commands make:factory -a and make:factory --array_syntax
  • service.js: Used for command make:service
  • service_array.js: Used for commands make:service -a and make:service --array_syntax

When the templates are compiled, a file is selected (falls back to default template if file not found), the keywords are replaced and the generated file is placed within the directory specified in .palconfig.

License

MIT