Improved docs

This commit is contained in:
Chen Asraf
2019-02-27 16:14:56 +02:00
parent 71d544aff4
commit a410b79195
6 changed files with 133 additions and 55 deletions

View File

@@ -12,14 +12,26 @@ yarn [global] add simple-scaffold
```
## Use as a command line tool
The first non-token argument (that has no `--` prefix) will be used as the scaffold name.
The rest is ignored, of course except for the available arguments below.
```bash
simple-scaffold MyComponent --template scaffolds/component/**/* \
--output src/components \
--locals myProp="propname",myVal=123
```
### Command Line Options
Scaffold Generator
Generate scaffolds for your project based on file templates.
Usage: simple-scaffold scaffold-name [options]
-n, --name string Component output name
-t, --templates File[] A glob pattern of template files to load.
A template file may be of any type and extension, and supports Handlebars as
a parsing engine for the file names and contents, so you may customize both
with variables from your configuration.
-o, --output File[] The output directory to put the new files in. They will attempt to maintain
their regular structure as they are found, if possible.
-l, --locals Key=Value[] A key-value map for the template to use in parsing.
-S, --create-sub-folder Boolean Whether to create a subdirectory with {{Name}} in the output directory.
default=true
-h, --help Display this help message
You can add this as a script in your `package.json`:
@@ -33,7 +45,7 @@ You can add this as a script in your `package.json`:
## Scaffolding
Scaffolding will replace {{vars}} in both the file name and its contents and put the transformed files
in `<output>/<{{Name}}`.
in `<output>/<{{Name}}>`, as per the Handlebars formatting rules.
Your context will be pre-populated with the following:
- `{{Name}}`: CapitalizedName of the component
@@ -42,30 +54,6 @@ Your context will be pre-populated with the following:
Any `locals` you add in the config will populate with their names wrapped in `{{` and `}}`.
They are all stringified, so be sure to parse them accordingly by creating a script, if necessary.
### Command line options
##### `--template glob [--template glob2 [...]]` (required) (alias: -t)
A glob pattern of template files to load.
A template file may be of any type and extension, and supports [Handlebars](https://handlebarsjs.com) as a parsing engine for the file names and contents, so you may customize both with variables from your configuration.
You can load more than one template list by simple adding more `--template` arguments.
##### `--output path` (optional) (alias -o)
The output directory to put the new files in. They will attempt to maintain their regular structure as they are found, if possible.
Your new scaffold will be placed under a directory with the scaffold name from the argumemts.
You may also pass a function to transform the output path for each file individually.
This function takes 2 arguments: filename, and base glob path
##### `--locals key=value [--locals key=value [,...]]` (optional) (alias: -l)
Pass a KV map to the template for parsing.
##### `--create-sub-folder [true|false]` (optional) (alias -S) (default: true)
Whether to create a subfolder for the output with all the files inside, or simply dump them directly in the output folder.
#####
### Use in Node.js
You can also build the scaffold yourself, if you want to create more complex arguments or scaffold groups.
Simply pass a config object to the constructor, and invoke `run()` when you are ready to start.
@@ -85,6 +73,11 @@ const scaffold = new SimpleScaffold({
}).run()
```
The exception in the config is that `output`, when used in Node directly, may also be passed a function for each input file to output into a dynamic path:
```javascript
config.output = (filename, basePath) => [basePath, filename].join(path.sep)
```
## Example Scaffold Input
### Input Directory structure
@@ -111,9 +104,9 @@ module.exports = class {{Name}} extends React.Component {
### Run Example
```bash
simple-scaffold MyComponent \
--template project/scaffold/**/* \
--output src/components \
--locals 'className=my-component`
-t project/scaffold/**/* \
-o src/components \
-l className=my-component
```
## Example Scaffold Output

61
cmd.ts
View File

@@ -5,7 +5,7 @@ import * as cliArgs from 'command-line-args'
import * as cliUsage from 'command-line-usage'
import * as path from 'path'
type Def = cliArgs.OptionDefinition & { description?: string }
type Def = cliArgs.OptionDefinition & { description?: string, typeLabel?: string }
function localsParser(content: string) {
const [key, value] = content.split('=')
@@ -19,19 +19,62 @@ function filePathParser(content: string) {
return [process.cwd(), content].join(path.sep)
}
function booleanParser(text: string) {
return text && text.trim().length ? ['true', '1', 'on'].includes(text.trim()) : true
}
const defs: Def[] = [
{ name: 'name', alias: 'n', type: String, description: 'Component output name', defaultOption: true },
{ name: 'templates', alias: 't', type: filePathParser, multiple: true },
{ name: 'output', alias: 'o', type: filePathParser, multiple: true },
{ name: 'locals', alias: 'l', multiple: true, type: localsParser },
{ name: 'create-sub-folder', alias: 'S', type: (text: string) => text && text.trim().length ? ['true', '1', 'on'].includes(text.trim()) : true },
{ name: 'help', alias: 'h', type: Boolean, description: 'Display this help message' },
{
name: 'name',
alias: 'n',
type: String,
description: 'Component output name',
defaultOption: true,
},
{
name: 'templates',
alias: 't',
type: filePathParser,
typeLabel: '{underline File}[]',
description: `A glob pattern of template files to load.\nA template file may be of any type and extension, and supports Handlebars as a parsing engine for the file names and contents, so you may customize both with variables from your configuration.`,
multiple: true,
},
{
name: 'output',
alias: 'o',
type: filePathParser,
typeLabel: '{underline File}[]',
description: `The output directory to put the new files in. They will attempt to maintain their regular structure as they are found, if possible.`,
multiple: true,
},
{
name: 'locals',
alias: 'l',
description: `A key-value map for the template to use in parsing.`,
multiple: true,
typeLabel: '{underline Key=Value}[]',
type: localsParser,
},
{
name: 'create-sub-folder',
alias: 'S',
typeLabel: '{underline Boolean}',
description: 'Whether to create a subdirectory with \\{\\{Name\\}\\} in the {underline output} directory. {bold default=true}',
type: booleanParser,
defaultValue: true,
},
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Display this help message',
},
]
const args = cliArgs(defs, { camelCase: true })
const help = [
{ header: 'Scaffold Generator', content: 'Generate scaffolds for your project based on file templates.' },
{ header: 'Scaffold Generator', content: `Generate scaffolds for your project based on file templates.\nUsage: {bold simple-scaffold} {underline scaffold-name} {underline [options]}` },
{ header: 'Options', optionList: defs }
]
@@ -39,13 +82,13 @@ args.locals = (args.locals || []).reduce((all: object, cur: object) => ({ ...all
if (args.createSubFolder === null) {
args.createSubFolder = true
}
console.info('Config:', args)
if (args.help || !args.name) {
console.log(cliUsage(help))
process.exit(0)
}
console.info('Config:', args)
new SimpleScaffold({
name: args.name,
templates: args.templates,

58
dist/cmd.js vendored
View File

@@ -243,28 +243,70 @@ function filePathParser(content) {
}
return [process.cwd(), content].join(path.sep);
}
function booleanParser(text) {
return text && text.trim().length ? ['true', '1', 'on'].includes(text.trim()) : true;
}
var defs = [
{ name: 'name', alias: 'n', type: String, description: 'Component output name', defaultOption: true },
{ name: 'templates', alias: 't', type: filePathParser, multiple: true },
{ name: 'output', alias: 'o', type: filePathParser, multiple: true },
{ name: 'locals', alias: 'l', multiple: true, type: localsParser },
{ name: 'create-sub-folder', alias: 'S', type: function (text) { return text && text.trim().length ? ['true', '1', 'on'].includes(text.trim()) : true; } },
{ name: 'help', alias: 'h', type: Boolean, description: 'Display this help message' },
{
name: 'name',
alias: 'n',
type: String,
description: 'Component output name',
defaultOption: true,
},
{
name: 'templates',
alias: 't',
type: filePathParser,
typeLabel: '{underline File}[]',
description: "A glob pattern of template files to load.\nA template file may be of any type and extension, and supports Handlebars as a parsing engine for the file names and contents, so you may customize both with variables from your configuration.",
multiple: true,
},
{
name: 'output',
alias: 'o',
type: filePathParser,
typeLabel: '{underline File}[]',
description: "The output directory to put the new files in. They will attempt to maintain their regular structure as they are found, if possible.",
multiple: true,
},
{
name: 'locals',
alias: 'l',
description: "A key-value map for the template to use in parsing.",
multiple: true,
typeLabel: '{underline Key=Value}[]',
type: localsParser,
},
{
name: 'create-sub-folder',
alias: 'S',
typeLabel: '{underline Boolean}',
description: 'Whether to create a subdirectory with \\{\\{Name\\}\\} in the {underline output} directory. {bold default=true}',
type: booleanParser,
defaultValue: true,
},
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Display this help message',
},
];
var args = cliArgs(defs, { camelCase: true });
var help = [
{ header: 'Scaffold Generator', content: 'Generate scaffolds for your project based on file templates.' },
{ header: 'Scaffold Generator', content: "Generate scaffolds for your project based on file templates.\nUsage: {bold simple-scaffold} {underline scaffold-name} {underline [options]}" },
{ header: 'Options', optionList: defs }
];
args.locals = (args.locals || []).reduce(function (all, cur) { return (__assign({}, all, cur)); }, {});
if (args.createSubFolder === null) {
args.createSubFolder = true;
}
console.info('Config:', args);
if (args.help || !args.name) {
console.log(cliUsage(help));
process.exit(0);
}
console.info('Config:', args);
new scaffold_1.default({
name: args.name,
templates: args.templates,

2
dist/cmd.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
dist/test.js.map vendored

File diff suppressed because one or more lines are too long