Skip to content

Commit

Permalink
move code from spasdk/tasks repo and rework
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkPark committed Jun 8, 2018
0 parents commit 9def7d0
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @author Stanislav Kalashnik <[email protected]>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/

'use strict';

// public
module.exports = {
// base rules
extends: require.resolve('@cjssdk/eslint-config/.eslintrc.js')
};
13 changes: 13 additions & 0 deletions .github/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Contributing
============

## Issues ##

Try to be the most specific possible. Give your OS, Node.js, application, database and packages info.
Give an example that will help other people to replicate your problem.


## Pull Requests ##

They are always welcomed but with respect to the adopted [code style](https://github.com/DarkPark/jscs) and included [ESLint](http://eslint.org/) rules.
Your patches must pass all the tests. If adding a new feature, don't forget about adding it to documentation (readme.md) and add tests for it.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.eslintrc.js
.github
.idea
.travis.yml
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sudo: false
language: node_js
node_js:
- "10"
- "8"
- "6"
cache:
directories:
- node_modules
66 changes: 66 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @author Stanislav Kalashnik <[email protected]>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/

'use strict';

var name = 'pug',
tools = require('@runner/tools'),
log = require('@runner/logger').wrap(name);


function build ( config, done ) {
var pug = require('pug'),
render, data;

try {
// prepare function and data
render = pug.compileFile(config.source, config.options || {});
data = render(config.variables || {});

// save generated result
tools.write([{name: config.target, data: data}], log, done);
} catch ( error ) {
log.fail(error.toString());
}
}


function generator ( config, options ) {
var tasks = {};

// sanitize and extend defaults
options = Object.assign(generator.options, options || {});

tasks[options.prefix + 'config' + options.suffix] = function () {
log.inspect(config, log);
};

tasks[options.prefix + 'build' + options.suffix] = function ( done ) {
build(config, done);
};

tasks[options.prefix + 'clear' + options.suffix] = function ( done ) {
tools.unlink([config.target], log, done);
};

return tasks;
}


// defaults
generator.options = {
prefix: name + ':',
suffix: ''
};


// export main actions
generator.methods = {
build: build
};


// public
module.exports = generator;
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@runner/generator-pug",
"version": "1.0.0",
"description": "Tasks generator for Pug.",
"author": {
"name": "Stanislav Kalashnik",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/runner/generator-pug"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"lint": "eslint .",
"test": "npm run lint"
},
"dependencies": {
"@runner/logger": "^1.0.0",
"pug": "^2.0.3"
},
"devDependencies": {
"@cjssdk/eslint-config": "^1.1.2",
"eslint": "^4.19.1"
},
"keywords": [
"task",
"runner",
"generator",
"pug"
],
"license": "GPL-3.0"
}
106 changes: 106 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
Tasks generator for Pug
=======================

[![build status](https://img.shields.io/travis/runner/generator-pug.svg?style=flat-square)](https://travis-ci.org/runner/generator-pug)
[![npm version](https://img.shields.io/npm/v/@runner/generator-pug.svg?style=flat-square)](https://www.npmjs.com/package/@runner/generator-pug)
[![dependencies status](https://img.shields.io/david/runner/generator-pug.svg?style=flat-square)](https://david-dm.org/runner/generator-pug)
[![devDependencies status](https://img.shields.io/david/dev/runner/generator-pug.svg?style=flat-square)](https://david-dm.org/runner/generator-pug?type=dev)
[![Gitter](https://img.shields.io/badge/gitter-join%20chat-blue.svg?style=flat-square)](https://gitter.im/DarkPark/runner)
[![RunKit](https://img.shields.io/badge/RunKit-try-yellow.svg?style=flat-square)](https://npm.runkit.com/@runner/generator-pug)


## Installation ##

```bash
npm install @runner/generator-pug
```


## Usage ##

Add to the scope:

```js
var generator = require('@runner/generator-pug');
```

Generate tasks according to the given config:

```js
var tasks = generator({
source: 'src/pug/main.pug',
target: 'build/develop/index.html',
variables: {
develop: true,
package: require('../package')
}
});
```

Add generated tasks to the `runner` instance:

```js
var runner = require('@runner/core');

Object.assign(runner.tasks, tasks);
```

The following tasks will become available:

Task name | Description
--------------|-------------
`pug:config` | prints the current configuration used for generated tasks
`pug:build` | performs pug compilation
`pug:clear` | removes compiled file

Generator accepts two arguments: base configuration and additional options.


### Base configuration ###

It's an object with the following properties:

Name | Description
-----------|-------------
source | main entry point passed as `path` to [pug.compileFile](https://pugjs.org/api/reference.html#pugcompilefilepath-options)
target | generated HTML file name
options | pug compiler [options](https://pugjs.org/api/reference.html#options)
variables | vars available in pug templates


### Additional options ###

It's an object with the following properties:

Name | Description
--------|-------------
prefix | an affix placed before a task name (default is `pug:`)
suffix | a string added at the end of a task name (empty by default)

So it's possible to change generated tasks names:

```js
Object.assign(runner.tasks,
generator(config, {
prefix: 'html:',
suffix: ':develop'
})
);
```

It will add the following tasks:

* `html:config:develop`
* `html:build:develop`
* `html:clear:develop`


## Contribution ##

If you have any problems or suggestions please open an [issue](https://github.com/runner/generator-pug/issues)
according to the contribution [rules](.github/contributing.md).


## License ##

`@runner/generator-pug` is released under the [GPL-3.0 License](http://opensource.org/licenses/GPL-3.0).

0 comments on commit 9def7d0

Please sign in to comment.