forked from SpoonX/boards-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d35798d
Showing
9 changed files
with
1,860 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
dist | ||
rlib-cov | ||
*.seed | ||
*.log | ||
*.out | ||
*.pid | ||
npm-debug.log | ||
*~ | ||
*# | ||
.DS_STORE | ||
.netbeans | ||
nbproject | ||
node_modules | ||
.idea | ||
.node_history | ||
.nyc_output | ||
.vscode | ||
notes.md | ||
_book | ||
yarn.lock | ||
.data |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Contributing | ||
|
||
We'd love for you to contribute and to make this project even better! | ||
If this interests you, please begin by reading our [contributing guidelines](https://github.com/SpoonX/about/blob/master/CONTRIBUTING.md), which will provide you with all the information you need to get started. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 SpoonX | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Boards-cli | ||
|
||
_An opinionated, no-nonsense boilerplate tool based on [Boards](www.npmjs.com/package/boards) and [Procurator](https://www.npmjs.com/package/procurator)._ | ||
|
||
Simply tell me where you keep your templates, pass instructions through a tiny config file and I'll take care of the rest. | ||
|
||
Use me to: | ||
|
||
- Generate boilerplate files (using a _really_ simple [templating language](https://www.npmjs.com/package/procurator)). | ||
- Alter existing files (like adding routes, or exports). | ||
- That's it. I don't do a lot, but I'm very good at what I do. | ||
|
||
## Getting started | ||
|
||
Getting started with Boards-cli is only a few steps. | ||
|
||
### Installation | ||
|
||
First, install the boards cli. `npm i -g boards-cli` | ||
|
||
### Setting up | ||
|
||
1. Create a new directory called `templates` (you can change this later) in the root of your project. | ||
2. Create a template (`echo "Created {{name}}, hello {{who}}!" > templates/hello.hbs`) | ||
3. Create a file in your project's root called `boards.js` with the following contents _(change to fit your project)_: | ||
|
||
```js | ||
module.exports = { | ||
// Where will you kep your boilerplate templates? | ||
templateRoot: __dirname + '/templates', | ||
|
||
// Where is your application's source located? | ||
appRoot: __dirname + '/src', | ||
|
||
tasks: { | ||
hello: [{task: 'generate', template: 'hello.hbs', target: '{{pascalCased}}.js'}] | ||
} | ||
}; | ||
``` | ||
|
||
4. Now you can run `boards hello someName who:you`. | ||
5. All done! You just generated a new file in src/SomeName.js | ||
|
||
## The command | ||
|
||
Running a command is simple, and allows for flexibility in terms of parameters. | ||
|
||
> `boards <task> <name> [parameters]` | ||
* The name of the **task** _(property `hello` in your config's tasks)_ is the first argument. This is defined by you in the config. Use this to specify which task to run. | ||
* The **name** argument is second, and specifies the name of the _thing_ you want to generate. This is one of the default parameters available in your templates, and target name. | ||
* The **parameters** is the third and last argument. This will be passed in to the templates and can be used in the target name. The format used here is [ezon](https://www.npmjs.com/package/ezon), a super simple format. Just JSON with the (optional) possibility to leave out quotes, curly braces and such. | ||
|
||
Some examples: | ||
|
||
* `boards redux user` | ||
* `boards class test 'className:TestClass,location:my/path'` | ||
|
||
## Tasks | ||
|
||
The cli only has 2 tasks available. Refreshing, isn't it? | ||
The cool thing is that you can batch them. This means a single task can perform multiple actions. | ||
|
||
### Generate | ||
|
||
This allows you to generate a new file in your project. | ||
|
||
```js | ||
{ | ||
task: 'generate', | ||
|
||
// Path to template (relative from `templateRoot`) | ||
template: 'reducers.hbs', | ||
|
||
// Path to target file. Where will we be putting the new file? | ||
target: '{{pascalCased}}.reducers.js' | ||
} | ||
``` | ||
|
||
### Modify | ||
|
||
This allows you to modify an existing file in your project. Useful for routes of exports! | ||
|
||
```js | ||
{ | ||
task: 'modify', | ||
|
||
// Path to target file. Which file will we be modifying? Relative to appRoot. | ||
target: '{{pascalCased}}.reducers.js', | ||
|
||
patch: { | ||
pattern: /];\s*module/, | ||
prepend: ` '{{name}}',\n` | ||
} | ||
} | ||
``` | ||
|
||
_**Note:** Read more about the [modify step](https://github.com/SpoonX/boards#modify) in the Boards docs._ | ||
|
||
### Batching tasks | ||
|
||
Batching tasks looks like this (using the config we made in [Setting up](#setting-up)): | ||
|
||
```js | ||
module.exports = { | ||
// Where will you kep your boilerplate templates? | ||
templateRoot: __dirname + '/templates', | ||
|
||
// Where is your application's source located? | ||
appRoot: __dirname + '/src', | ||
|
||
tasks: { | ||
hello: [ | ||
{task: 'generate', template: 'hello.hbs', target: '{{pascalCased}}.js'}, | ||
{task: 'generate', template: 'hello2.hbs', target: '{{pascalCased}}2.js'}, | ||
{task: 'modify', target: 'redux/foo.js', patch: {pattern: /];\s*module/, prepend: ` '{{name}}',\n`}}, | ||
] | ||
} | ||
}; | ||
``` | ||
|
||
Now when running hello, two files will be created and one will be altered. | ||
|
||
## Templating | ||
|
||
Templating uses [Procurator](https://www.npmjs.com/package/procurator). This means you have some flexibility in terms of defaults and variables. Also, it's just extremely fast. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env node | ||
|
||
const ezon = require('ezon'); | ||
const {Runner} = require('./lib/Runner'); | ||
const config = require(process.cwd() + '/boards.js'); | ||
const task = process.argv[2]; | ||
const name = process.argv[3]; | ||
const pascalCased = name[0].toUpperCase() + name.substr(1); | ||
|
||
let runner = new Runner(config); | ||
|
||
runner.run(task, Object.assign(process.argv[4] ? ezon(process.argv[4]) : {}, {name, pascalCased})) | ||
.then(() => console.log('Task completed.')) | ||
.catch(error => console.log('Task failed.', error)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const {Boards} = require('boards'); | ||
const boards = new Boards({discovery: false}); | ||
|
||
class Runner { | ||
constructor(config) { | ||
this.boards = new Boards({discovery: false}); | ||
this.config = config; | ||
} | ||
|
||
run(task, parameters) { | ||
let instructions = this.config.tasks[task]; | ||
|
||
if (!instructions) { | ||
throw new Error(`Instructions for task "${task}" not found.`); | ||
} | ||
|
||
if (!Array.isArray(instructions)) { | ||
instructions = [instructions]; | ||
} | ||
|
||
return Promise.all(instructions.map(instruction => this.runTask(instruction, parameters))); | ||
} | ||
|
||
runTask(instruction, parameters) { | ||
if (typeof this[instruction.task] !== 'function') { | ||
throw new Error(`Invalid task "${instruction.task}" supplied`); | ||
} | ||
|
||
this[instruction.task](instruction, parameters); | ||
} | ||
|
||
modify(instruction, parameters) { | ||
return this.boards.generate('ModificationGenerator', Object.assign({}, parameters, { | ||
sourceDirectory: this.config.appRoot, | ||
targetDirectory: this.config.appRoot, | ||
sourceFile : this.getTarget(instruction.target, parameters), | ||
modify : {patch: instruction.patch} | ||
})); | ||
} | ||
|
||
generate(instruction, parameters) { | ||
return boards.generate('TemplateGenerator', Object.assign({}, parameters, { | ||
sourceFile : instruction.template, | ||
targetFile : this.getTarget(instruction.target, parameters), | ||
sourceDirectory: this.config.templateRoot, | ||
targetDirectory: `${this.config.appRoot}/redux` | ||
})); | ||
} | ||
|
||
getTarget(target, parameters) { | ||
if (typeof target === 'function') { | ||
return target(parameters); | ||
} | ||
|
||
return target | ||
.replace('{{pascalCased}}', parameters.pascalCased) | ||
.replace('{{name}}', parameters.name); | ||
} | ||
} | ||
|
||
module.exports.Runner = Runner; |
Oops, something went wrong.