mirror of
https://github.com/chenasraf/simple-scaffold.git
synced 2026-05-18 01:29:09 +00:00
Compare commits
76 Commits
v0.4.1
...
v1.0.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
385829aa27 | ||
|
|
33c357bccc | ||
|
|
b74b781a5b | ||
|
|
57410c8d74 | ||
|
|
f81cfd8ae1 | ||
|
|
414494734d | ||
|
|
89b588f64e | ||
|
|
cf22e2e62f | ||
|
|
246c139061 | ||
|
|
711d8f0333 | ||
|
|
8b22e96329 | ||
|
|
53c0842ab8 | ||
|
|
48631325c1 | ||
|
|
045ad0118a | ||
|
|
b4dca7a954 | ||
|
|
7c42808f63 | ||
|
|
fd42013e8b | ||
|
|
961a72fcdc | ||
|
|
d6d99cfdf2 | ||
|
|
ea4ecabe02 | ||
|
|
c7749a8d33 | ||
|
|
a59f29d71d | ||
|
|
bc224d93e1 | ||
|
|
cf923d8889 | ||
|
|
01e458ee0c | ||
|
|
93853712f5 | ||
|
|
474a3dcc1f | ||
|
|
27e84d1093 | ||
|
|
a6f25facc0 | ||
|
|
3ee66b24f5 | ||
|
|
0ce19a7e1a | ||
|
|
7273538d79 | ||
|
|
43b64965e3 | ||
|
|
4f81654e05 | ||
|
|
8fcc7a6dc6 | ||
|
|
b4b0de6f2f | ||
|
|
2d5626ca70 | ||
|
|
564e821419 | ||
|
|
a52f9a0b84 | ||
|
|
aeddd44778 | ||
|
|
7cdf5e461b | ||
|
|
c42a58c2f2 | ||
|
|
d0c0152717 | ||
|
|
208ee307c8 | ||
|
|
54834909b9 | ||
|
|
40b592072e | ||
|
|
3cb9a6fcd8 | ||
|
|
12974b5561 | ||
|
|
7f98d469a3 | ||
|
|
cd25b04886 | ||
|
|
5cd637f41f | ||
|
|
0a4467ae5f | ||
|
|
713a0ed44f | ||
|
|
edec2d1c26 | ||
|
|
2e12907412 | ||
|
|
5b7e0e30f1 | ||
|
|
09238300cd | ||
|
|
552614ca3f | ||
|
|
813f706cf0 | ||
|
|
1bc2221472 | ||
|
|
f07affa124 | ||
|
|
ce22a2c34c | ||
|
|
7c0c347002 | ||
|
|
977288ae5a | ||
|
|
4afafa5a4a | ||
|
|
7bee2a51c7 | ||
|
|
d4c049baaf | ||
|
|
06590c4b6e | ||
|
|
c4f2dfb04f | ||
|
|
a410b79195 | ||
|
|
71d544aff4 | ||
|
|
20389d7b33 | ||
|
|
d7a4362725 | ||
|
|
0a2d7c08f3 | ||
|
|
a92c471243 | ||
|
|
07b1c4b1f0 |
5
.editorconfig
Normal file
5
.editorconfig
Normal file
@@ -0,0 +1,5 @@
|
||||
[*]
|
||||
tab_width = 2
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
12
.github/FUNDING.yml
vendored
Normal file
12
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||
patreon: # Replace with a single Patreon username
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: casraf
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
50
.github/workflows/alpha.yml
vendored
Normal file
50
.github/workflows/alpha.yml
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
name: Alpha Releases
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [alpha]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
if: "!contains(github.event.head_commit.message, '[skip ci]')"
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "12.x"
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn build
|
||||
- run: cd ./dist && yarn pack --filename=../package.tgz
|
||||
if: "!contains(github.event.head_commit.message, '[skip publish]')"
|
||||
- uses: Klemensas/action-autotag@stable
|
||||
if: "!contains(github.event.head_commit.message, '[skip publish]')"
|
||||
id: update_tag
|
||||
with:
|
||||
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
|
||||
tag_prefix: "v"
|
||||
- name: Publish on NPM
|
||||
uses: JS-DevTools/npm-publish@v1
|
||||
if: "!contains(github.event.head_commit.message, '[skip publish]')"
|
||||
with:
|
||||
package: ./dist/package.json
|
||||
token: "${{ secrets.NPM_TOKEN }}"
|
||||
- name: Create Release
|
||||
if: steps.update_tag.outputs.tagname && !contains(github.event.head_commit.message, '[skip publish]')
|
||||
uses: actions/create-release@v1
|
||||
id: create_release
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ steps.update_tag.outputs.tagname }}
|
||||
release_name: Release ${{ steps.update_tag.outputs.tagname }}
|
||||
- name: Upload Release Asset
|
||||
if: steps.update_tag.outputs.tagname && !contains(github.event.head_commit.message, '[skip publish]')
|
||||
id: upload-release-asset
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./package.tgz
|
||||
asset_name: package.tgz
|
||||
asset_content_type: application/tgz
|
||||
49
.github/workflows/main.yml
vendored
Normal file
49
.github/workflows/main.yml
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
name: Releases
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
if: "!contains(github.event.head_commit.message, '[skip ci]')"
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "12.x"
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn build
|
||||
- run: cd ./dist && yarn pack --filename=../package.tgz
|
||||
if: "!contains(github.event.head_commit.message, '[skip publish]')"
|
||||
- uses: Klemensas/action-autotag@stable
|
||||
if: "!contains(github.event.head_commit.message, '[skip publish]')"
|
||||
id: update_tag
|
||||
with:
|
||||
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
|
||||
tag_prefix: "v"
|
||||
- name: Publish on NPM
|
||||
uses: JS-DevTools/npm-publish@v1
|
||||
with:
|
||||
package: ./dist/package.json
|
||||
token: "${{ secrets.NPM_TOKEN }}"
|
||||
- name: Create Release
|
||||
if: steps.update_tag.outputs.tagname && !contains(github.event.head_commit.message, '[skip publish]')
|
||||
uses: actions/create-release@v1
|
||||
id: create_release
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ steps.update_tag.outputs.tagname }}
|
||||
release_name: Release ${{ steps.update_tag.outputs.tagname }}
|
||||
- name: Upload Release Asset
|
||||
if: steps.update_tag.outputs.tagname && !contains(github.event.head_commit.message, '[skip publish]')
|
||||
id: upload-release-asset
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./package.tgz
|
||||
asset_name: package.tgz
|
||||
asset_content_type: application/tgz
|
||||
17
.github/workflows/pull_requests.yml
vendored
Normal file
17
.github/workflows/pull_requests.yml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
name: Pull Requests
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [master, alpha, beta]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
if: "!contains(github.event.head_commit.message, '[skip ci]')"
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: "12.x"
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn build
|
||||
- run: yarn test
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -58,4 +58,6 @@ typings/
|
||||
.env
|
||||
|
||||
examples/test-output/**/*
|
||||
!examples/test-output/.gitkeep
|
||||
dist/
|
||||
.DS_Store
|
||||
tmp/
|
||||
|
||||
5
.prettierrc
Normal file
5
.prettierrc
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"semi": false,
|
||||
"printWidth": 120,
|
||||
"tabWidth": 2
|
||||
}
|
||||
11
.vscode/settings.json
vendored
11
.vscode/settings.json
vendored
@@ -1,3 +1,12 @@
|
||||
{
|
||||
"typescript.tsdk": "./node_modules/typescript/lib"
|
||||
"typescript.tsdk": "./node_modules/typescript/lib",
|
||||
"npm.packageManager": "yarn",
|
||||
"cSpell.words": [
|
||||
"massarg",
|
||||
"nodir",
|
||||
"nobrace",
|
||||
"noext",
|
||||
"nocomment",
|
||||
"nonegate"
|
||||
]
|
||||
}
|
||||
|
||||
56
.vscode/tasks.json
vendored
56
.vscode/tasks.json
vendored
@@ -1,7 +1,53 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"command": "webpack",
|
||||
"isShellCommand": true,
|
||||
"args": [],
|
||||
"showOutput": "always"
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"script": "build",
|
||||
"label": "build",
|
||||
"type": "npm",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
{
|
||||
"script": "dev",
|
||||
"label": "dev",
|
||||
"type": "npm",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
{
|
||||
"script": "start",
|
||||
"label": "start",
|
||||
"type": "npm",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
{
|
||||
"script": "test",
|
||||
"label": "test",
|
||||
"type": "npm",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
{
|
||||
"command": "yarn test --watchAll",
|
||||
"label": "yarn test --watchAll",
|
||||
"type": "shell",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
{
|
||||
"script": "cmd",
|
||||
"label": "cmd",
|
||||
"type": "npm",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
{
|
||||
"script": "build-test",
|
||||
"label": "build-test",
|
||||
"type": "npm",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
{
|
||||
"script": "build-cmd",
|
||||
"label": "build-cmd",
|
||||
"type": "npm",
|
||||
"problemMatcher": [],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
233
README.md
233
README.md
@@ -1,7 +1,9 @@
|
||||
# simple-scaffold
|
||||
|
||||
Simple Scaffold allows you to create your structured files based on templates.
|
||||
|
||||
## Install
|
||||
|
||||
You can either use it as a command line tool or import into your own code and run from there.
|
||||
|
||||
```bash
|
||||
@@ -9,89 +11,171 @@ You can either use it as a command line tool or import into your own code and ru
|
||||
npm install [-g] simple-scaffold
|
||||
# yarn
|
||||
yarn [global] add simple-scaffold
|
||||
# run without installing
|
||||
npx simple-scaffold <...args>
|
||||
```
|
||||
|
||||
## 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
|
||||
|
||||
```plaintext
|
||||
Usage: simple-scaffold [options]
|
||||
|
||||
Create structured files based on templates.
|
||||
|
||||
Options:
|
||||
|
||||
--help|-h Display help information
|
||||
|
||||
--name|-n Name to be passed to the generated files. {{name}} and
|
||||
{{Name}} inside contents and file names will be replaced
|
||||
accordingly.
|
||||
|
||||
--output|-o Path to output to. If --create-sub-folder is enabled, the
|
||||
subfolder will be created inside this path.
|
||||
|
||||
--templates|-t Template files to use as input. You may provide multiple
|
||||
files, each of which can be a relative or absolute path, or a glob
|
||||
pattern for multiple file matching easily. (default:
|
||||
)
|
||||
|
||||
--overwrite|-w Enable to override output files, even if they already exist.
|
||||
(default: false)
|
||||
|
||||
--data|-d Add custom data to the templates. By default, only your app
|
||||
name is included.
|
||||
|
||||
--create-sub-folder|-s Create subfolder with the input name (default:
|
||||
false)
|
||||
|
||||
--quiet|-q Suppress output logs (Same as --verbose 0)
|
||||
(default: false)
|
||||
|
||||
--verbose|-v Determine amount of logs to display. The values are: 0
|
||||
(none) | 1 (debug) | 2 (info) | 3 (warn) | 4 (error). The
|
||||
provided level will display messages of the same level or higher.
|
||||
(default: 2)
|
||||
|
||||
--dry-run|-dr Don't emit files. This is good for testing your scaffolds and
|
||||
making sure they don't fail, without having to write actual file
|
||||
contents or create directories. (default:
|
||||
false)
|
||||
```
|
||||
|
||||
You can add this as a script in your `package.json`:
|
||||
You can also add this as a script in your `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
...
|
||||
"scripts": {
|
||||
"scaffold": "node node_modules/simple-scaffold/dist/cmd.js --template scaffolds/component/**/* --output src/components --locals myProp=\"propname\",myVal=123"
|
||||
...
|
||||
"scaffold": "yarn simple-scaffold --templates scaffolds/component/**/* --output src/components --data '{\"myProp\": \"propName\", \"myVal\": \"123\"}'"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Scaffolding
|
||||
Scaffolding will replace {{vars}} in both the file name and its contents and put the transformed files
|
||||
in `<output>/<{{Name}}`.
|
||||
## Use in Node.js
|
||||
|
||||
Your context will be pre-populated with the following:
|
||||
- `{{Name}}`: CapitalizedName of the component
|
||||
- `{{name}}`: camelCasedName of the component
|
||||
|
||||
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)
|
||||
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)
|
||||
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[,key=value[,...]]` (optional)
|
||||
Pass a KV map to the template for parsing.
|
||||
|
||||
### 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.
|
||||
The config takes similar arguments to the command line:
|
||||
|
||||
```javascript
|
||||
const SimpleScaffold = require('simple-scaffold').default
|
||||
const SimpleScaffold = require("simple-scaffold").default
|
||||
|
||||
const scaffold = new SimpleScaffold({
|
||||
name: 'component',
|
||||
templates: [path.join(__dirname, 'scaffolds', 'component')],
|
||||
output: path.join(__dirname, 'src', 'components'),
|
||||
const scaffold = SimpleScaffold({
|
||||
name: "component",
|
||||
templates: [path.join(__dirname, "scaffolds", "component")],
|
||||
output: path.join(__dirname, "src", "components"),
|
||||
createSubFolder: true,
|
||||
locals: {
|
||||
property: 'value',
|
||||
}
|
||||
}).run()
|
||||
property: "value",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## Example Scaffold Input
|
||||
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:
|
||||
|
||||
### Input Directory structure
|
||||
```javascript
|
||||
config.output = (fullPath, baseDir, baseName) => {
|
||||
console.log({ fullPath, baseDir, baseName })
|
||||
return path.resolve(baseDir, baseName)
|
||||
}
|
||||
```
|
||||
|
||||
## Preparing files
|
||||
|
||||
### Template files
|
||||
|
||||
Put your template files anywhere, and fill them with tokens for replacement.
|
||||
|
||||
### Variable/token replacement
|
||||
|
||||
Scaffolding will replace `{{ varName }}` in both the file name and its contents and put the
|
||||
transformed files in the output directory.
|
||||
|
||||
The data available for the template parser is the data you pass to the `data` config option (or
|
||||
`--data` argument in CLI).
|
||||
|
||||
Your `data` will be pre-populated with the following:
|
||||
|
||||
- `{{Name}}`: PascalCase of the component name
|
||||
- `{{name}}`: raw name of the component
|
||||
|
||||
> Simple-Scaffold uses [Handlebars.js](https://handlebarsjs.com/) for outputting the file contents,
|
||||
> see their documentation for more information on syntax.
|
||||
> Any `data` you add in the config will be available for use with their names wrapped in
|
||||
> `{{` and `}}`.
|
||||
|
||||
#### Helpers
|
||||
|
||||
Simple-Scaffold provides some built-in text transformation filters usable by handleBars.
|
||||
|
||||
For example, you may use `{{ snakeCase name }}` inside a template file or filename, and it will
|
||||
replace `My Name` with `my_name` when producing the final value.
|
||||
|
||||
Here are the built-in helpers available for use:
|
||||
|
||||
| Helper name | Example code | Example output |
|
||||
| ----------- | ----------------------- | -------------- |
|
||||
| camelCase | `{{ camelCase name }}` | myName |
|
||||
| snakeCase | `{{ snakeCase name }}` | my_name |
|
||||
| startCase | `{{ startCase name }}` | My Name |
|
||||
| kebabCase | `{{ kebabCase name }}` | my-name |
|
||||
| hyphenCase | `{{ hyphenCase name }}` | my-name |
|
||||
| pascalCase | `{{ pascalCase name }}` | MyName |
|
||||
|
||||
**Note:** These helpers are available for any data property, not exclusive to `name`.
|
||||
|
||||
## Examples
|
||||
|
||||
### Command Example
|
||||
|
||||
```bash
|
||||
simple-scaffold MyComponent \
|
||||
-t project/scaffold/**/* \
|
||||
-o src/components \
|
||||
-d '{"className": "myClassName"}'
|
||||
MyComponent
|
||||
```
|
||||
|
||||
### Example Scaffold Input
|
||||
|
||||
#### Input Directory structure
|
||||
|
||||
```plaintext
|
||||
- project
|
||||
- scaffold
|
||||
- {{Name}}.js
|
||||
- src
|
||||
- components
|
||||
- ...
|
||||
- scaffold
|
||||
- {{Name}}.js
|
||||
- src
|
||||
- components
|
||||
- ...
|
||||
```
|
||||
|
||||
#### project/scaffold/{{Name}}.js
|
||||
#### Contents of `project/scaffold/{{Name}}.js`
|
||||
|
||||
```js
|
||||
const React = require('react')
|
||||
|
||||
@@ -102,32 +186,37 @@ module.exports = class {{Name}} extends React.Component {
|
||||
}
|
||||
```
|
||||
|
||||
### Run Example
|
||||
```bash
|
||||
simple-scaffold MyComponent \
|
||||
--template project/scaffold/**/* \
|
||||
--output src/components \
|
||||
--locals 'className=my-component`
|
||||
```
|
||||
### Example Scaffold Output
|
||||
|
||||
## Example Scaffold Output
|
||||
#### Directory structure
|
||||
```
|
||||
### Output directory structure
|
||||
|
||||
```plaintext
|
||||
- project
|
||||
- src
|
||||
- components
|
||||
- MyComponent
|
||||
- MyComponent.js
|
||||
- ...
|
||||
- src
|
||||
- components
|
||||
- MyComponent
|
||||
- MyComponent.js
|
||||
- ...
|
||||
```
|
||||
|
||||
#### project/scaffold/MyComponent/MyComponent.js
|
||||
With `createSubFolder = false`:
|
||||
|
||||
```plaintext
|
||||
- project
|
||||
- src
|
||||
- components
|
||||
- MyComponent.js
|
||||
- ...
|
||||
```
|
||||
|
||||
#### Contents of `project/scaffold/MyComponent/MyComponent.js`
|
||||
|
||||
```js
|
||||
const React = require('react')
|
||||
const React = require("react")
|
||||
|
||||
module.exports = class MyComponent extends React.Component {
|
||||
render() {
|
||||
<div className="my-component">MyComponent Component</div>
|
||||
<div className="myClassName">MyComponent Component</div>
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3
babel.config.js
Normal file
3
babel.config.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
plugins: ["@babel/plugin-proposal-nullish-coalescing-operator"],
|
||||
}
|
||||
52
cmd.ts
52
cmd.ts
@@ -1,52 +0,0 @@
|
||||
import SimpleScaffold from './scaffold'
|
||||
import * as fs from 'fs'
|
||||
import IScaffold from './index'
|
||||
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 }
|
||||
|
||||
function localsParser(content: string) {
|
||||
const [key, value] = content.split('=')
|
||||
return { [key]: value }
|
||||
}
|
||||
|
||||
function filePathParser(content: string) {
|
||||
if (content.startsWith('/')) {
|
||||
return content
|
||||
}
|
||||
return [process.cwd(), content].join(path.sep)
|
||||
}
|
||||
|
||||
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: Boolean },
|
||||
{ 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: 'Options', optionList: defs }
|
||||
]
|
||||
|
||||
args.locals = args.locals.reduce((all: object, cur: object) => ({ ...all, ...cur }), {} as IScaffold.Config['locals'])
|
||||
console.info('Config:', args)
|
||||
|
||||
if (args.help || !args.name) {
|
||||
console.log(cliUsage(help))
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
new SimpleScaffold({
|
||||
name: args.name,
|
||||
templates: args.templates,
|
||||
output: args.output,
|
||||
locals: args.locals,
|
||||
createSubfolder: args.createSubFolder,
|
||||
}).run()
|
||||
289
dist/cmd.js
vendored
289
dist/cmd.js
vendored
@@ -1,289 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 6);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("path");
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fs = __webpack_require__(2);
|
||||
var path = __webpack_require__(0);
|
||||
var glob = __webpack_require__(3);
|
||||
var handlebars = __webpack_require__(4);
|
||||
var SimpleScaffold = /** @class */ (function () {
|
||||
function SimpleScaffold(config) {
|
||||
this.locals = {};
|
||||
var DefaultConfig = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
};
|
||||
this.config = __assign({}, DefaultConfig, config);
|
||||
var DefaultLocals = {
|
||||
Name: this.config.name[0].toUpperCase() + this.config.name.slice(1),
|
||||
name: this.config.name[0].toLowerCase() + this.config.name.slice(1)
|
||||
};
|
||||
this.locals = __assign({}, DefaultLocals, config.locals);
|
||||
}
|
||||
SimpleScaffold.prototype.parseLocals = function (text) {
|
||||
var template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
});
|
||||
return template(this.locals);
|
||||
};
|
||||
SimpleScaffold.prototype.fileList = function (input) {
|
||||
var output = [];
|
||||
for (var _i = 0, input_1 = input; _i < input_1.length; _i++) {
|
||||
var checkPath = input_1[_i];
|
||||
var files = glob.sync(checkPath, { dot: true })
|
||||
.map(function (g) { return g[0] == '/' ? g : path.join(process.cwd(), g); });
|
||||
var idx = checkPath.indexOf('*');
|
||||
var cleanCheckPath = checkPath;
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1);
|
||||
}
|
||||
for (var _a = 0, files_1 = files; _a < files_1.length; _a++) {
|
||||
var file = files_1[_a];
|
||||
output.push({ base: cleanCheckPath, file: file });
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
SimpleScaffold.prototype.getFileContents = function (filePath) {
|
||||
console.log(fs.readFileSync(filePath));
|
||||
return fs.readFileSync(filePath).toString();
|
||||
};
|
||||
SimpleScaffold.prototype.getOutputPath = function (file, basePath) {
|
||||
var out;
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file, basePath);
|
||||
}
|
||||
else {
|
||||
var outputDir = this.config.output + (this.config.createSubfolder ? "/" + this.config.name + "/" : '/');
|
||||
var idx = file.indexOf(basePath);
|
||||
var relativeFilePath = file;
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1);
|
||||
}
|
||||
out = outputDir + relativeFilePath;
|
||||
}
|
||||
return this.parseLocals(out);
|
||||
};
|
||||
SimpleScaffold.prototype.writeFile = function (filePath, fileContents) {
|
||||
if (!fs.existsSync(path.dirname(filePath))) {
|
||||
fs.mkdirSync(path.dirname(filePath));
|
||||
}
|
||||
console.info('Writing file:', filePath);
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
SimpleScaffold.prototype.run = function () {
|
||||
console.log("Generating scaffold: " + this.config.name + "...");
|
||||
var templates = this.fileList(this.config.templates);
|
||||
var fileConf, count = 0;
|
||||
for (var _i = 0, templates_1 = templates; _i < templates_1.length; _i++) {
|
||||
fileConf = templates_1[_i];
|
||||
count++;
|
||||
var file = fileConf.file, base = fileConf.base;
|
||||
var outputPath = this.getOutputPath(file, base);
|
||||
var contents = this.getFileContents(file);
|
||||
var outputContents = this.parseLocals(contents);
|
||||
this.writeFile(outputPath, outputContents);
|
||||
console.info('Parsing:', { file: file, base: base, outputPath: outputPath, outputContents: outputContents.replace("\n", "\\n") });
|
||||
}
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!');
|
||||
}
|
||||
console.log('Done');
|
||||
};
|
||||
return SimpleScaffold;
|
||||
}());
|
||||
exports.default = SimpleScaffold;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("fs");
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("glob");
|
||||
|
||||
/***/ }),
|
||||
/* 4 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("handlebars");
|
||||
|
||||
/***/ }),
|
||||
/* 5 */,
|
||||
/* 6 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var scaffold_1 = __webpack_require__(1);
|
||||
var cliArgs = __webpack_require__(7);
|
||||
var cliUsage = __webpack_require__(8);
|
||||
var path = __webpack_require__(0);
|
||||
function localsParser(content) {
|
||||
var _a = content.split('='), key = _a[0], value = _a[1];
|
||||
return _b = {}, _b[key] = value, _b;
|
||||
var _b;
|
||||
}
|
||||
function filePathParser(content) {
|
||||
if (content.startsWith('/')) {
|
||||
return content;
|
||||
}
|
||||
return [process.cwd(), content].join(path.sep);
|
||||
}
|
||||
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: Boolean },
|
||||
{ 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: 'Options', optionList: defs }
|
||||
];
|
||||
args.locals = args.locals.reduce(function (all, cur) { return (__assign({}, all, cur)); }, {});
|
||||
console.info('Config:', args);
|
||||
if (args.help || !args.name) {
|
||||
console.log(cliUsage(help));
|
||||
process.exit(0);
|
||||
}
|
||||
new scaffold_1.default({
|
||||
name: args.name,
|
||||
templates: args.templates,
|
||||
output: args.output,
|
||||
locals: args.locals,
|
||||
createSubfolder: args.createSubFolder,
|
||||
}).run();
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 7 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("command-line-args");
|
||||
|
||||
/***/ }),
|
||||
/* 8 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("command-line-usage");
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
||||
});
|
||||
//# sourceMappingURL=cmd.js.map
|
||||
1
dist/cmd.js.map
vendored
1
dist/cmd.js.map
vendored
File diff suppressed because one or more lines are too long
13
dist/dist/scaffold.d.ts
vendored
13
dist/dist/scaffold.d.ts
vendored
@@ -1,13 +0,0 @@
|
||||
import IScaffold from './index';
|
||||
declare class SimpleScaffold {
|
||||
config: IScaffold.Config;
|
||||
locals: IScaffold.Config['locals'];
|
||||
constructor(config: IScaffold.Config);
|
||||
private parseLocals(text);
|
||||
private fileList(input);
|
||||
private getFileContents(filePath);
|
||||
private getOutputPath(file, basePath);
|
||||
private writeFile(filePath, fileContents);
|
||||
run(): void;
|
||||
}
|
||||
export default SimpleScaffold;
|
||||
0
dist/dist/test.d.ts
vendored
0
dist/dist/test.d.ts
vendored
96
dist/main.js
vendored
96
dist/main.js
vendored
@@ -1,96 +0,0 @@
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 7);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ({
|
||||
|
||||
/***/ 7:
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
(function webpackMissingModule() { throw new Error("Cannot find module \"add\""); }());
|
||||
module.exports = __webpack_require__(8);
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 8:
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("jest");
|
||||
|
||||
/***/ })
|
||||
|
||||
/******/ });
|
||||
});
|
||||
//# sourceMappingURL=main.js.map
|
||||
1
dist/main.js.map
vendored
1
dist/main.js.map
vendored
@@ -1 +0,0 @@
|
||||
{"version":3,"sources":["webpack/universalModuleDefinition","webpack/bootstrap c994c2c400fa1fc61abe","external \"jest\""],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;ACVA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAK;AACL;AACA;;AAEA;AACA;AACA;AACA,mCAA2B,0BAA0B,EAAE;AACvD,yCAAiC,eAAe;AAChD;AACA;AACA;;AAEA;AACA,8DAAsD,+DAA+D;;AAErH;AACA;;AAEA;AACA;;;;;;;;;;;;;;;;;AC7DA,iC","file":"main.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"library\"] = factory();\n\telse\n\t\troot[\"library\"] = factory();\n})(this, function() {\nreturn \n\n\n// WEBPACK FOOTER //\n// webpack/universalModuleDefinition"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 7);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap c994c2c400fa1fc61abe","module.exports = require(\"jest\");\n\n\n//////////////////\n// WEBPACK FOOTER\n// external \"jest\"\n// module id = 8\n// module chunks = 3"],"sourceRoot":""}
|
||||
217
dist/scaffold.js
vendored
217
dist/scaffold.js
vendored
@@ -1,217 +0,0 @@
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 1);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("path");
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fs = __webpack_require__(2);
|
||||
var path = __webpack_require__(0);
|
||||
var glob = __webpack_require__(3);
|
||||
var handlebars = __webpack_require__(4);
|
||||
var SimpleScaffold = /** @class */ (function () {
|
||||
function SimpleScaffold(config) {
|
||||
this.locals = {};
|
||||
var DefaultConfig = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
};
|
||||
this.config = __assign({}, DefaultConfig, config);
|
||||
var DefaultLocals = {
|
||||
Name: this.config.name[0].toUpperCase() + this.config.name.slice(1),
|
||||
name: this.config.name[0].toLowerCase() + this.config.name.slice(1)
|
||||
};
|
||||
this.locals = __assign({}, DefaultLocals, config.locals);
|
||||
}
|
||||
SimpleScaffold.prototype.parseLocals = function (text) {
|
||||
var template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
});
|
||||
return template(this.locals);
|
||||
};
|
||||
SimpleScaffold.prototype.fileList = function (input) {
|
||||
var output = [];
|
||||
for (var _i = 0, input_1 = input; _i < input_1.length; _i++) {
|
||||
var checkPath = input_1[_i];
|
||||
var files = glob.sync(checkPath, { dot: true })
|
||||
.map(function (g) { return g[0] == '/' ? g : path.join(process.cwd(), g); });
|
||||
var idx = checkPath.indexOf('*');
|
||||
var cleanCheckPath = checkPath;
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1);
|
||||
}
|
||||
for (var _a = 0, files_1 = files; _a < files_1.length; _a++) {
|
||||
var file = files_1[_a];
|
||||
output.push({ base: cleanCheckPath, file: file });
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
SimpleScaffold.prototype.getFileContents = function (filePath) {
|
||||
console.log(fs.readFileSync(filePath));
|
||||
return fs.readFileSync(filePath).toString();
|
||||
};
|
||||
SimpleScaffold.prototype.getOutputPath = function (file, basePath) {
|
||||
var out;
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file, basePath);
|
||||
}
|
||||
else {
|
||||
var outputDir = this.config.output + (this.config.createSubfolder ? "/" + this.config.name + "/" : '/');
|
||||
var idx = file.indexOf(basePath);
|
||||
var relativeFilePath = file;
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1);
|
||||
}
|
||||
out = outputDir + relativeFilePath;
|
||||
}
|
||||
return this.parseLocals(out);
|
||||
};
|
||||
SimpleScaffold.prototype.writeFile = function (filePath, fileContents) {
|
||||
if (!fs.existsSync(path.dirname(filePath))) {
|
||||
fs.mkdirSync(path.dirname(filePath));
|
||||
}
|
||||
console.info('Writing file:', filePath);
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
SimpleScaffold.prototype.run = function () {
|
||||
console.log("Generating scaffold: " + this.config.name + "...");
|
||||
var templates = this.fileList(this.config.templates);
|
||||
var fileConf, count = 0;
|
||||
for (var _i = 0, templates_1 = templates; _i < templates_1.length; _i++) {
|
||||
fileConf = templates_1[_i];
|
||||
count++;
|
||||
var file = fileConf.file, base = fileConf.base;
|
||||
var outputPath = this.getOutputPath(file, base);
|
||||
var contents = this.getFileContents(file);
|
||||
var outputContents = this.parseLocals(contents);
|
||||
this.writeFile(outputPath, outputContents);
|
||||
console.info('Parsing:', { file: file, base: base, outputPath: outputPath, outputContents: outputContents.replace("\n", "\\n") });
|
||||
}
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!');
|
||||
}
|
||||
console.log('Done');
|
||||
};
|
||||
return SimpleScaffold;
|
||||
}());
|
||||
exports.default = SimpleScaffold;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("fs");
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("glob");
|
||||
|
||||
/***/ }),
|
||||
/* 4 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("handlebars");
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
||||
});
|
||||
//# sourceMappingURL=scaffold.js.map
|
||||
1
dist/scaffold.js.map
vendored
1
dist/scaffold.js.map
vendored
File diff suppressed because one or more lines are too long
246
dist/test.js
vendored
246
dist/test.js
vendored
@@ -1,246 +0,0 @@
|
||||
(function webpackUniversalModuleDefinition(root, factory) {
|
||||
if(typeof exports === 'object' && typeof module === 'object')
|
||||
module.exports = factory();
|
||||
else if(typeof define === 'function' && define.amd)
|
||||
define([], factory);
|
||||
else if(typeof exports === 'object')
|
||||
exports["library"] = factory();
|
||||
else
|
||||
root["library"] = factory();
|
||||
})(this, function() {
|
||||
return /******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId]) {
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
/******/ }
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 5);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("path");
|
||||
|
||||
/***/ }),
|
||||
/* 1 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var fs = __webpack_require__(2);
|
||||
var path = __webpack_require__(0);
|
||||
var glob = __webpack_require__(3);
|
||||
var handlebars = __webpack_require__(4);
|
||||
var SimpleScaffold = /** @class */ (function () {
|
||||
function SimpleScaffold(config) {
|
||||
this.locals = {};
|
||||
var DefaultConfig = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
};
|
||||
this.config = __assign({}, DefaultConfig, config);
|
||||
var DefaultLocals = {
|
||||
Name: this.config.name[0].toUpperCase() + this.config.name.slice(1),
|
||||
name: this.config.name[0].toLowerCase() + this.config.name.slice(1)
|
||||
};
|
||||
this.locals = __assign({}, DefaultLocals, config.locals);
|
||||
}
|
||||
SimpleScaffold.prototype.parseLocals = function (text) {
|
||||
var template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
});
|
||||
return template(this.locals);
|
||||
};
|
||||
SimpleScaffold.prototype.fileList = function (input) {
|
||||
var output = [];
|
||||
for (var _i = 0, input_1 = input; _i < input_1.length; _i++) {
|
||||
var checkPath = input_1[_i];
|
||||
var files = glob.sync(checkPath, { dot: true })
|
||||
.map(function (g) { return g[0] == '/' ? g : path.join(process.cwd(), g); });
|
||||
var idx = checkPath.indexOf('*');
|
||||
var cleanCheckPath = checkPath;
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1);
|
||||
}
|
||||
for (var _a = 0, files_1 = files; _a < files_1.length; _a++) {
|
||||
var file = files_1[_a];
|
||||
output.push({ base: cleanCheckPath, file: file });
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
SimpleScaffold.prototype.getFileContents = function (filePath) {
|
||||
console.log(fs.readFileSync(filePath));
|
||||
return fs.readFileSync(filePath).toString();
|
||||
};
|
||||
SimpleScaffold.prototype.getOutputPath = function (file, basePath) {
|
||||
var out;
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file, basePath);
|
||||
}
|
||||
else {
|
||||
var outputDir = this.config.output + (this.config.createSubfolder ? "/" + this.config.name + "/" : '/');
|
||||
var idx = file.indexOf(basePath);
|
||||
var relativeFilePath = file;
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1);
|
||||
}
|
||||
out = outputDir + relativeFilePath;
|
||||
}
|
||||
return this.parseLocals(out);
|
||||
};
|
||||
SimpleScaffold.prototype.writeFile = function (filePath, fileContents) {
|
||||
if (!fs.existsSync(path.dirname(filePath))) {
|
||||
fs.mkdirSync(path.dirname(filePath));
|
||||
}
|
||||
console.info('Writing file:', filePath);
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, function (err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
SimpleScaffold.prototype.run = function () {
|
||||
console.log("Generating scaffold: " + this.config.name + "...");
|
||||
var templates = this.fileList(this.config.templates);
|
||||
var fileConf, count = 0;
|
||||
for (var _i = 0, templates_1 = templates; _i < templates_1.length; _i++) {
|
||||
fileConf = templates_1[_i];
|
||||
count++;
|
||||
var file = fileConf.file, base = fileConf.base;
|
||||
var outputPath = this.getOutputPath(file, base);
|
||||
var contents = this.getFileContents(file);
|
||||
var outputContents = this.parseLocals(contents);
|
||||
this.writeFile(outputPath, outputContents);
|
||||
console.info('Parsing:', { file: file, base: base, outputPath: outputPath, outputContents: outputContents.replace("\n", "\\n") });
|
||||
}
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!');
|
||||
}
|
||||
console.log('Done');
|
||||
};
|
||||
return SimpleScaffold;
|
||||
}());
|
||||
exports.default = SimpleScaffold;
|
||||
|
||||
|
||||
/***/ }),
|
||||
/* 2 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("fs");
|
||||
|
||||
/***/ }),
|
||||
/* 3 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("glob");
|
||||
|
||||
/***/ }),
|
||||
/* 4 */
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
module.exports = require("handlebars");
|
||||
|
||||
/***/ }),
|
||||
/* 5 */
|
||||
/***/ (function(module, exports, __webpack_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var scaffold_1 = __webpack_require__(1);
|
||||
var path = __webpack_require__(0);
|
||||
var templateDir = path.join(process.cwd(), 'examples');
|
||||
new scaffold_1.default({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output/no-create-subpath',
|
||||
createSubfolder: false,
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run();
|
||||
new scaffold_1.default({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output',
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run();
|
||||
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
||||
});
|
||||
//# sourceMappingURL=test.js.map
|
||||
1
dist/test.js.map
vendored
1
dist/test.js.map
vendored
File diff suppressed because one or more lines are too long
1
examples/test-input/Component/inner/inner-{{name}}.txt
Normal file
1
examples/test-input/Component/inner/inner-{{name}}.txt
Normal file
@@ -0,0 +1 @@
|
||||
{{name}}
|
||||
@@ -1,18 +1,16 @@
|
||||
import * as React from 'react'
|
||||
import * as css from './{{Name}}.css'
|
||||
import * as React from "react"
|
||||
import * as css from "./{{Name}}.css"
|
||||
|
||||
class {{Name}} extends React.Component<any> {
|
||||
private {{property}}
|
||||
private {{ property }}
|
||||
|
||||
constructor(props: any) {
|
||||
super(props)
|
||||
this.{{property}} = {{value}}
|
||||
this.{{ property }} = {{ value }}
|
||||
}
|
||||
|
||||
public render() {
|
||||
return (
|
||||
<div className={ css.{{Name}} } />
|
||||
)
|
||||
return <div className={ css.{{Name}} } />
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
21
index.d.ts
vendored
21
index.d.ts
vendored
@@ -1,21 +0,0 @@
|
||||
declare namespace IScaffold {
|
||||
|
||||
export interface Config {
|
||||
name?: string
|
||||
templates: string[]
|
||||
output: string | ((path: string, base: string) => string)
|
||||
locals?: Locals
|
||||
createSubfolder?: boolean
|
||||
}
|
||||
|
||||
export interface Locals {
|
||||
[k: string]: string
|
||||
}
|
||||
|
||||
export interface FileRepr {
|
||||
base: string
|
||||
file: string
|
||||
}
|
||||
}
|
||||
|
||||
export default IScaffold
|
||||
196
jest.config.ts
Normal file
196
jest.config.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* For a detailed explanation regarding each configuration property and type check, visit:
|
||||
* https://jestjs.io/docs/configuration
|
||||
*/
|
||||
|
||||
export default {
|
||||
// All imported modules in your tests should be mocked automatically
|
||||
// automock: false,
|
||||
|
||||
// Stop running tests after `n` failures
|
||||
// bail: 0,
|
||||
|
||||
// The directory where Jest should store its cached dependency information
|
||||
// cacheDirectory: "/private/var/folders/q9/0mns8fgd00b4t5j5lq2wh2yh0000gn/T/jest_dx",
|
||||
|
||||
// Automatically clear mock calls and instances between every test
|
||||
clearMocks: true,
|
||||
|
||||
// Indicates whether the coverage information should be collected while executing the test
|
||||
collectCoverage: true,
|
||||
|
||||
// An array of glob patterns indicating a set of files for which coverage information should be collected
|
||||
// collectCoverageFrom: undefined,
|
||||
|
||||
// The directory where Jest should output its coverage files
|
||||
coverageDirectory: "coverage",
|
||||
|
||||
// An array of regexp pattern strings used to skip coverage collection
|
||||
// coveragePathIgnorePatterns: [
|
||||
// "/node_modules/"
|
||||
// ],
|
||||
|
||||
// Indicates which provider should be used to instrument code for coverage
|
||||
coverageProvider: "v8",
|
||||
|
||||
// A list of reporter names that Jest uses when writing coverage reports
|
||||
// coverageReporters: [
|
||||
// "json",
|
||||
// "text",
|
||||
// "lcov",
|
||||
// "clover"
|
||||
// ],
|
||||
|
||||
// An object that configures minimum threshold enforcement for coverage results
|
||||
// coverageThreshold: undefined,
|
||||
|
||||
// A path to a custom dependency extractor
|
||||
// dependencyExtractor: undefined,
|
||||
|
||||
// Make calling deprecated APIs throw helpful error messages
|
||||
// errorOnDeprecated: false,
|
||||
|
||||
// Force coverage collection from ignored files using an array of glob patterns
|
||||
// forceCoverageMatch: [],
|
||||
|
||||
// A path to a module which exports an async function that is triggered once before all test suites
|
||||
// globalSetup: undefined,
|
||||
|
||||
// A path to a module which exports an async function that is triggered once after all test suites
|
||||
// globalTeardown: undefined,
|
||||
|
||||
// A set of global variables that need to be available in all test environments
|
||||
// globals: {},
|
||||
|
||||
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
|
||||
// maxWorkers: "50%",
|
||||
|
||||
// An array of directory names to be searched recursively up from the requiring module's location
|
||||
// moduleDirectories: [
|
||||
// "node_modules"
|
||||
// ],
|
||||
|
||||
// An array of file extensions your modules use
|
||||
// moduleFileExtensions: [
|
||||
// "js",
|
||||
// "jsx",
|
||||
// "ts",
|
||||
// "tsx",
|
||||
// "json",
|
||||
// "node"
|
||||
// ],
|
||||
|
||||
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
|
||||
// moduleNameMapper: {},
|
||||
|
||||
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
|
||||
modulePathIgnorePatterns: ["<rootDir>/dist"],
|
||||
|
||||
// Activates notifications for test results
|
||||
// notify: false,
|
||||
|
||||
// An enum that specifies notification mode. Requires { notify: true }
|
||||
// notifyMode: "failure-change",
|
||||
|
||||
// A preset that is used as a base for Jest's configuration
|
||||
preset: "ts-jest",
|
||||
|
||||
// Run tests from one or more projects
|
||||
// projects: undefined,
|
||||
|
||||
// Use this configuration option to add custom reporters to Jest
|
||||
// reporters: undefined,
|
||||
|
||||
// Automatically reset mock state between every test
|
||||
// resetMocks: false,
|
||||
|
||||
// Reset the module registry before running each individual test
|
||||
// resetModules: false,
|
||||
|
||||
// A path to a custom resolver
|
||||
// resolver: undefined,
|
||||
|
||||
// Automatically restore mock state between every test
|
||||
// restoreMocks: false,
|
||||
|
||||
// The root directory that Jest should scan for tests and modules within
|
||||
// rootDir: undefined,
|
||||
|
||||
// A list of paths to directories that Jest should use to search for files in
|
||||
// roots: [
|
||||
// "<rootDir>"
|
||||
// ],
|
||||
|
||||
// Allows you to use a custom runner instead of Jest's default test runner
|
||||
// runner: "jest-runner",
|
||||
|
||||
// The paths to modules that run some code to configure or set up the testing environment before each test
|
||||
// setupFiles: [],
|
||||
|
||||
// A list of paths to modules that run some code to configure or set up the testing framework before each test
|
||||
// setupFilesAfterEnv: [],
|
||||
|
||||
// The number of seconds after which a test is considered as slow and reported as such in the results.
|
||||
// slowTestThreshold: 5,
|
||||
|
||||
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
|
||||
// snapshotSerializers: [],
|
||||
|
||||
// The test environment that will be used for testing
|
||||
testEnvironment: "node",
|
||||
|
||||
// Options that will be passed to the testEnvironment
|
||||
// testEnvironmentOptions: {},
|
||||
|
||||
// Adds a location field to test results
|
||||
// testLocationInResults: false,
|
||||
|
||||
// The glob patterns Jest uses to detect test files
|
||||
// testMatch: [
|
||||
// "**/__tests__/**/*.[jt]s?(x)",
|
||||
// "**/?(*.)+(spec|test).[tj]s?(x)"
|
||||
// ],
|
||||
|
||||
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
|
||||
// testPathIgnorePatterns: [
|
||||
// "/node_modules/"
|
||||
// ],
|
||||
|
||||
// The regexp pattern or array of patterns that Jest uses to detect test files
|
||||
// testRegex: [],
|
||||
|
||||
// This option allows the use of a custom results processor
|
||||
// testResultsProcessor: undefined,
|
||||
|
||||
// This option allows use of a custom test runner
|
||||
// testRunner: "jest-circus/runner",
|
||||
|
||||
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
|
||||
// testURL: "http://localhost",
|
||||
|
||||
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
|
||||
// timers: "real",
|
||||
|
||||
// A map from regular expressions to paths to transformers
|
||||
// transform: {},
|
||||
|
||||
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
|
||||
// transformIgnorePatterns: [
|
||||
// "/node_modules/",
|
||||
// "\\.pnp\\.[^\\/]+$"
|
||||
// ],
|
||||
|
||||
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
|
||||
// unmockedModulePathPatterns: undefined,
|
||||
|
||||
// Indicates whether each individual test should be reported during the run
|
||||
// verbose: undefined,
|
||||
|
||||
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
|
||||
// watchPathIgnorePatterns: [],
|
||||
|
||||
// Whether to use watchman for file crawling
|
||||
// watchman: true,
|
||||
|
||||
// extensionsToTreatAsEsm: [".ts"],
|
||||
}
|
||||
55
package.json
55
package.json
@@ -1,45 +1,42 @@
|
||||
{
|
||||
"name": "simple-scaffold",
|
||||
"version": "0.4.1",
|
||||
"version": "1.0.0-alpha.10",
|
||||
"description": "Create files based on templates",
|
||||
"repository": "https://github.com/chenasraf/simple-scaffold.git",
|
||||
"author": "Chen Asraf <inbox@casraf.com>",
|
||||
"license": "MIT",
|
||||
"main": "dist/scaffold.js",
|
||||
"bin": "dist/cmd.js",
|
||||
"types": "index.d.ts",
|
||||
"main": "index.js",
|
||||
"bin": "cmd.js",
|
||||
"scripts": {
|
||||
"build": "NODE_ENV=${NODE_ENV:-production} webpack -p && chmod -R +x ./dist",
|
||||
"dev": "webpack --watch",
|
||||
"clean": "rm -rf dist/",
|
||||
"build": "yarn clean && tsc && chmod -R +x ./dist && cp ./package.json ./dist/ && cp ./README.md ./dist/",
|
||||
"dev": "tsc --watch",
|
||||
"start": "node dist/scaffold.js",
|
||||
"test": "jest",
|
||||
"cmd": "dist/cmd.js",
|
||||
"test": "jest --verbose",
|
||||
"cmd": "node --trace-warnings dist/cmd.js",
|
||||
"build-test": "yarn build && yarn test",
|
||||
"build-cmd": "yarn build && yarn cmd"
|
||||
},
|
||||
"dependencies": {
|
||||
"command-line-args": "^5.0.2",
|
||||
"command-line-usage": "^5.0.5",
|
||||
"glob": "^7.1.2",
|
||||
"handlebars": "^4.0.11",
|
||||
"ts-loader": "^3.1.1",
|
||||
"typescript": "^2.6.1",
|
||||
"webpack": "^3.8.1",
|
||||
"webpack-dev-server": "^2.9.4",
|
||||
"webpack-node-externals": "^1.6.0"
|
||||
"args": "^5.0.1",
|
||||
"chalk": "^4.1.2",
|
||||
"glob": "^7.1.3",
|
||||
"handlebars": "^4.7.7",
|
||||
"lodash": "^4.17.21",
|
||||
"massarg": "^1.0.3",
|
||||
"util.promisify": "^1.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/command-line-args": "^5.0.0",
|
||||
"@types/command-line-usage": "^5.0.1",
|
||||
"@types/glob": "^5.0.34",
|
||||
"@types/handlebars": "^4.0.36",
|
||||
"@types/node": "^8.0.50",
|
||||
"jest": "^22.0.4"
|
||||
},
|
||||
"jest": {
|
||||
"testPathIgnorePatterns": [
|
||||
"node_modules",
|
||||
"dist"
|
||||
]
|
||||
"@types/args": "^3.0.1",
|
||||
"@types/glob": "^7.1.1",
|
||||
"@types/jest": "^26.0.24",
|
||||
"@types/lodash": "^4.14.171",
|
||||
"@types/mock-fs": "^4.13.1",
|
||||
"@types/node": "^14.14.22",
|
||||
"jest": "^27.0.6",
|
||||
"mock-fs": "^5.0.0",
|
||||
"ts-jest": "^27.0.3",
|
||||
"ts-node": "^10.1.0",
|
||||
"typescript": "^4.3.5"
|
||||
}
|
||||
}
|
||||
|
||||
112
scaffold.ts
112
scaffold.ts
@@ -1,112 +0,0 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import IScaffold from './index'
|
||||
import * as glob from 'glob'
|
||||
import * as handlebars from 'handlebars'
|
||||
|
||||
class SimpleScaffold {
|
||||
public config: IScaffold.Config
|
||||
public locals: IScaffold.Config['locals'] = {} as any
|
||||
|
||||
constructor(config: IScaffold.Config) {
|
||||
const DefaultConfig: IScaffold.Config = {
|
||||
name: 'scaffold',
|
||||
templates: [],
|
||||
output: process.cwd(),
|
||||
createSubfolder: true,
|
||||
}
|
||||
|
||||
this.config = { ...DefaultConfig, ...config }
|
||||
|
||||
const DefaultLocals = {
|
||||
Name: this.config.name![0].toUpperCase() + this.config.name!.slice(1),
|
||||
name: this.config.name![0].toLowerCase() + this.config.name!.slice(1)
|
||||
}
|
||||
|
||||
this.locals = { ...DefaultLocals, ...config.locals }
|
||||
}
|
||||
|
||||
private parseLocals(text: string): string {
|
||||
const template = handlebars.compile(text, {
|
||||
noEscape: true
|
||||
})
|
||||
return template(this.locals)
|
||||
}
|
||||
|
||||
private fileList(input: string[]): IScaffold.FileRepr[] {
|
||||
const output: IScaffold.FileRepr[] = []
|
||||
for (const checkPath of input) {
|
||||
const files = glob.sync(checkPath, { dot: true })
|
||||
.map(g => g[0] == '/' ? g : path.join(process.cwd(), g))
|
||||
const idx = checkPath.indexOf('*')
|
||||
let cleanCheckPath = checkPath
|
||||
if (idx >= 0) {
|
||||
cleanCheckPath = checkPath.slice(0, idx - 1)
|
||||
}
|
||||
for (const file of files) {
|
||||
output.push({ base: cleanCheckPath, file })
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
private getFileContents(filePath: string): string {
|
||||
console.log(fs.readFileSync(filePath))
|
||||
return fs.readFileSync(filePath).toString()
|
||||
}
|
||||
|
||||
private getOutputPath(file: string, basePath: string): string {
|
||||
let out: string
|
||||
|
||||
if (typeof this.config.output === 'function') {
|
||||
out = this.config.output(file, basePath)
|
||||
} else {
|
||||
const outputDir = this.config.output + (this.config.createSubfolder ? `/${this.config.name}/` : '/')
|
||||
const idx = file.indexOf(basePath)
|
||||
let relativeFilePath = file
|
||||
if (idx >= 0) {
|
||||
relativeFilePath = file.slice(idx + basePath.length + 1)
|
||||
}
|
||||
out = outputDir + relativeFilePath
|
||||
}
|
||||
|
||||
return this.parseLocals(out)
|
||||
}
|
||||
|
||||
private writeFile(filePath: string, fileContents: string): void {
|
||||
if (!fs.existsSync(path.dirname(filePath))) {
|
||||
fs.mkdirSync(path.dirname(filePath))
|
||||
}
|
||||
console.info('Writing file:', filePath)
|
||||
fs.writeFile(filePath, fileContents, { encoding: 'utf-8' }, (err) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public run(): void {
|
||||
console.log(`Generating scaffold: ${this.config.name}...`)
|
||||
const templates = this.fileList(this.config.templates)
|
||||
|
||||
let fileConf, count = 0
|
||||
for (fileConf of templates) {
|
||||
count++
|
||||
const { file, base } = fileConf
|
||||
const outputPath = this.getOutputPath(file, base)
|
||||
const contents = this.getFileContents(file)
|
||||
const outputContents = this.parseLocals(contents)
|
||||
|
||||
this.writeFile(outputPath, outputContents)
|
||||
console.info('Parsing:', { file, base, outputPath, outputContents: outputContents.replace("\n", "\\n") })
|
||||
}
|
||||
|
||||
if (!count) {
|
||||
throw new Error('No files to scaffold!')
|
||||
}
|
||||
|
||||
console.log('Done')
|
||||
}
|
||||
}
|
||||
|
||||
export default SimpleScaffold
|
||||
2
src/cmd.ts
Normal file
2
src/cmd.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import { parseCliArgs } from "./cmd_util"
|
||||
parseCliArgs()
|
||||
89
src/cmd_util.ts
Normal file
89
src/cmd_util.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import massarg from "massarg"
|
||||
import chalk from "chalk"
|
||||
import { LogLevel, ScaffoldCmdConfig } from "./types"
|
||||
import { Scaffold } from "./scaffold"
|
||||
|
||||
export function parseCliArgs(args = process.argv.slice(2)) {
|
||||
return (
|
||||
massarg<ScaffoldCmdConfig & { help: boolean; extras: string[] }>()
|
||||
.main(Scaffold)
|
||||
.option({
|
||||
name: "name",
|
||||
aliases: ["n"],
|
||||
isDefault: true,
|
||||
description:
|
||||
"Name to be passed to the generated files. {{name}} and {{Name}} inside contents and file names will be replaced accordingly.",
|
||||
})
|
||||
.option({
|
||||
name: "output",
|
||||
aliases: ["o"],
|
||||
description:
|
||||
"Path to output to. If --create-sub-folder is enabled, the subfolder will be created inside this path.",
|
||||
})
|
||||
.option({
|
||||
name: "templates",
|
||||
aliases: ["t"],
|
||||
description:
|
||||
"Template files to use as input. You may provide multiple files, each of which can be a relative or absolute path, " +
|
||||
"or a glob pattern for multiple file matching easily.",
|
||||
defaultValue: [],
|
||||
array: true,
|
||||
})
|
||||
.option({
|
||||
aliases: ["w"],
|
||||
name: "overwrite",
|
||||
description: "Enable to override output files, even if they already exist.",
|
||||
defaultValue: false,
|
||||
boolean: true,
|
||||
})
|
||||
.option({
|
||||
aliases: ["d"],
|
||||
name: "data",
|
||||
description: "Add custom data to the templates. By default, only your app name is included.",
|
||||
parse: (v) => JSON.parse(v),
|
||||
})
|
||||
.option({
|
||||
aliases: ["s"],
|
||||
name: "create-sub-folder",
|
||||
description: "Create subfolder with the input name",
|
||||
defaultValue: false,
|
||||
boolean: true,
|
||||
})
|
||||
.option({
|
||||
aliases: ["q"],
|
||||
name: "quiet",
|
||||
description: "Suppress output logs (Same as --verbose 0)",
|
||||
defaultValue: false,
|
||||
boolean: true,
|
||||
})
|
||||
.option({
|
||||
aliases: ["v"],
|
||||
name: "verbose",
|
||||
description: `Determine amount of logs to display. The values are: ${chalk.bold`0 (none) | 1 (debug) | 2 (info) | 3 (warn) | 4 (error)`}. The provided level will display messages of the same level or higher.`,
|
||||
defaultValue: LogLevel.Info,
|
||||
parse: Number,
|
||||
})
|
||||
.option({
|
||||
aliases: ["dr"],
|
||||
name: "dry-run",
|
||||
description:
|
||||
"Don't emit files. This is good for testing your scaffolds and making sure they " +
|
||||
"don't fail, without having to write actual file contents or create directories.",
|
||||
defaultValue: false,
|
||||
boolean: true,
|
||||
})
|
||||
// .example({
|
||||
// input: `yarn cmd -t examples/test-input/Component -o examples/test-output -d '{"property":"myProp","value":"10"}'`,
|
||||
// description: "Usage",
|
||||
// output: "",
|
||||
// })
|
||||
.help({
|
||||
binName: "simple-scaffold",
|
||||
useGlobalColumns: true,
|
||||
usageExample: "[options]",
|
||||
header: "Create structured files based on templates.",
|
||||
footer: `Copyright © Chen Asraf 2021\nNPM: ${chalk.underline`https://npmjs.com/package/massarg`}\nGitHub: ${chalk.underline`https://github.com/chenasraf/massarg`}`,
|
||||
})
|
||||
.parse(args)
|
||||
)
|
||||
}
|
||||
0
dist/dist/cmd.d.ts → src/filters.ts
Executable file → Normal file
0
dist/dist/cmd.d.ts → src/filters.ts
Executable file → Normal file
4
src/index.ts
Normal file
4
src/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from "./scaffold"
|
||||
export * from "./types"
|
||||
import Scaffold from "./scaffold"
|
||||
export default Scaffold
|
||||
159
src/scaffold.ts
Normal file
159
src/scaffold.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { glob } from "glob"
|
||||
import path from "path"
|
||||
import { promisify } from "util"
|
||||
import { promises as fsPromises } from "fs"
|
||||
const { readFile, writeFile } = fsPromises
|
||||
|
||||
import {
|
||||
createDirIfNotExists,
|
||||
getOptionValueForFile,
|
||||
handleErr,
|
||||
handlebarsParse,
|
||||
log,
|
||||
pathExists,
|
||||
pascalCase,
|
||||
isDir,
|
||||
removeGlob,
|
||||
} from "./utils"
|
||||
import { LogLevel, ScaffoldConfig } from "./types"
|
||||
|
||||
export async function Scaffold(config: ScaffoldConfig) {
|
||||
const options = { ...config }
|
||||
try {
|
||||
const data = { name: options.name, Name: pascalCase(options.name), ...options.data }
|
||||
log(options, LogLevel.Debug, "Full config:", {
|
||||
name: options.name,
|
||||
templates: options.templates,
|
||||
output: options.output,
|
||||
createSubfolder: options.createSubFolder,
|
||||
data: options.data,
|
||||
overwrite: options.overwrite,
|
||||
quiet: options.quiet,
|
||||
verbose: `${options.verbose} (${Object.keys(LogLevel).find(
|
||||
(k) => (LogLevel[k as any] as unknown as number) === options.verbose!
|
||||
)})`,
|
||||
})
|
||||
log(options, LogLevel.Info, "Data:", data)
|
||||
for (let template of config.templates) {
|
||||
try {
|
||||
const _isGlob = template.includes("*")
|
||||
if (!_isGlob && !(await pathExists(template))) {
|
||||
const err: NodeJS.ErrnoException = new Error(`ENOENT, no such file or directory ${template}`)
|
||||
err.code = "ENOENT"
|
||||
err.path = "non-existing-input"
|
||||
err.errno = -2
|
||||
throw err
|
||||
}
|
||||
const _nonGlobTemplate = _isGlob ? removeGlob(template) : template
|
||||
log(options, LogLevel.Debug, "before isDir", "isGlob:", _isGlob, template)
|
||||
const _isDir = _isGlob ? true : await isDir(template)
|
||||
log(options, LogLevel.Debug, "after isDir", _isDir)
|
||||
const _shouldAddGlob = !_isGlob && _isDir
|
||||
const origTemplate = template
|
||||
if (_shouldAddGlob) {
|
||||
template = template + "/**/*"
|
||||
}
|
||||
log(options, LogLevel.Debug, "before glob")
|
||||
const files = await promisify(glob)(template, {
|
||||
dot: true,
|
||||
debug: false,
|
||||
nodir: options.verbose === LogLevel.Debug,
|
||||
nobrace: true,
|
||||
noext: true,
|
||||
nocomment: true,
|
||||
nonegate: true,
|
||||
})
|
||||
log(options, LogLevel.Debug, "after glob")
|
||||
for (const inputFilePath of files) {
|
||||
if (!(await isDir(inputFilePath))) {
|
||||
const basePath = path
|
||||
.resolve(process.cwd(), path.dirname(removeGlob(inputFilePath).replace(_nonGlobTemplate, "")).slice(1))
|
||||
.replace(process.cwd() + "/", "")
|
||||
.replace(process.cwd(), "")
|
||||
log(
|
||||
options,
|
||||
LogLevel.Debug,
|
||||
`\nprocess.cwd(): ${process.cwd()}`,
|
||||
`\norigTemplate: ${origTemplate}`,
|
||||
`\nremoveGlob(inputFilePath).replace(_nonGlobTemplate, "").slice(1): ${removeGlob(inputFilePath)
|
||||
.replace(_nonGlobTemplate, "")
|
||||
.slice(1)}`,
|
||||
`\ntemplate: ${template}`,
|
||||
`\ninputFilePath: ${inputFilePath}`,
|
||||
`\nnonGlobTemplate: ${_nonGlobTemplate}`,
|
||||
`\nbase path: ${basePath}`,
|
||||
`\nisDir: ${_isDir}`,
|
||||
`\nisGlob: ${_isGlob}`,
|
||||
`\n`
|
||||
)
|
||||
await handleTemplateFile(inputFilePath, basePath, options, data)
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
handleErr(e)
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
log(options, LogLevel.Error, e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async function handleTemplateFile(
|
||||
templatePath: string,
|
||||
basePath: string,
|
||||
options: ScaffoldConfig,
|
||||
data: Record<string, string>
|
||||
): Promise<void> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const inputPath = path.resolve(process.cwd(), templatePath)
|
||||
const outputPathOpt = getOptionValueForFile(inputPath, data, options.output)
|
||||
const outputDir = path.resolve(
|
||||
process.cwd(),
|
||||
...([outputPathOpt, basePath, options.createSubFolder ? options.name : undefined].filter(Boolean) as string[])
|
||||
)
|
||||
const outputPath = handlebarsParse(path.join(outputDir, path.basename(inputPath)), data)
|
||||
log(
|
||||
options,
|
||||
LogLevel.Debug,
|
||||
`\nParsing ${templatePath}`,
|
||||
`\nBase path: ${basePath}`,
|
||||
`\nFull input path: ${inputPath}`,
|
||||
`\nOutput Path Opt: ${outputPathOpt}`,
|
||||
`\nFull output dir: ${outputDir}`,
|
||||
`\nFull output path: ${outputPath}`,
|
||||
`\n`
|
||||
)
|
||||
const overwrite = getOptionValueForFile(inputPath, data, options.overwrite ?? false)
|
||||
const exists = await pathExists(outputPath)
|
||||
|
||||
await createDirIfNotExists(path.dirname(outputPath), options)
|
||||
|
||||
log(options, LogLevel.Info, `Writing to ${outputPath}`)
|
||||
if (!exists || overwrite) {
|
||||
if (exists && overwrite) {
|
||||
log(options, LogLevel.Info, `File ${outputPath} exists, overwriting`)
|
||||
}
|
||||
const templateBuffer = await readFile(inputPath)
|
||||
const outputContents = handlebarsParse(templateBuffer, data)
|
||||
|
||||
if (!options.dryRun) {
|
||||
await writeFile(outputPath, outputContents)
|
||||
log(options, LogLevel.Info, "Done.")
|
||||
} else {
|
||||
log(options, LogLevel.Info, "Content output:")
|
||||
log(options, LogLevel.Info, outputContents)
|
||||
}
|
||||
} else if (exists) {
|
||||
log(options, LogLevel.Info, `File ${outputPath} already exists, skipping`)
|
||||
}
|
||||
resolve()
|
||||
} catch (e: any) {
|
||||
handleErr(e)
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default Scaffold
|
||||
36
src/types.ts
Normal file
36
src/types.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
export enum LogLevel {
|
||||
None = 0,
|
||||
Debug = 1,
|
||||
Info = 2,
|
||||
Warning = 3,
|
||||
Error = 4,
|
||||
}
|
||||
|
||||
export type FileResponseFn<T> = (fullPath: string, basedir: string, basename: string) => T
|
||||
|
||||
export type FileResponse<T> = T | FileResponseFn<T>
|
||||
|
||||
export interface ScaffoldConfig {
|
||||
/** The name supplied for the output templates */
|
||||
name: string
|
||||
/** Template input files/dirs/glob patterns to use as template input. These will be copied to the output directory. */
|
||||
templates: string[]
|
||||
/** Output directory to put scaffolded files in. */
|
||||
output: FileResponse<string>
|
||||
createSubFolder?: boolean
|
||||
data?: Record<string, string>
|
||||
overwrite?: FileResponse<boolean>
|
||||
quiet?: boolean
|
||||
verbose?: LogLevel
|
||||
dryRun?: boolean
|
||||
}
|
||||
export interface ScaffoldCmdConfig {
|
||||
name: string
|
||||
templates: string[]
|
||||
output: string
|
||||
createSubFolder: boolean
|
||||
data?: Record<string, string>
|
||||
overwrite: boolean
|
||||
quiet: boolean
|
||||
dryRun: boolean
|
||||
}
|
||||
121
src/utils.ts
Normal file
121
src/utils.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import path from "path"
|
||||
import { F_OK } from "constants"
|
||||
import { FileResponse, FileResponseFn, LogLevel, ScaffoldConfig } from "./types"
|
||||
import camelCase from "lodash/camelCase"
|
||||
import snakeCase from "lodash/snakeCase"
|
||||
import kebabCase from "lodash/kebabCase"
|
||||
import startCase from "lodash/startCase"
|
||||
import Handlebars from "handlebars"
|
||||
import { promises as fsPromises } from "fs"
|
||||
import chalk from "chalk"
|
||||
const { stat, access, mkdir } = fsPromises
|
||||
|
||||
const helpers = {
|
||||
camelCase,
|
||||
snakeCase,
|
||||
startCase,
|
||||
kebabCase,
|
||||
hyphenCase: kebabCase,
|
||||
pascalCase,
|
||||
}
|
||||
|
||||
for (const helperName in helpers) {
|
||||
Handlebars.registerHelper(helperName, helpers[helperName as keyof typeof helpers])
|
||||
}
|
||||
|
||||
export function handleErr(err: NodeJS.ErrnoException | null) {
|
||||
if (err) throw err
|
||||
}
|
||||
|
||||
export function log(options: ScaffoldConfig, level: LogLevel, ...obj: any[]) {
|
||||
if (options.quiet || options.verbose === LogLevel.None || level <= (options.verbose ?? LogLevel.Info)) {
|
||||
return
|
||||
}
|
||||
const levelColor: Record<LogLevel, keyof typeof chalk> = {
|
||||
[LogLevel.None]: "reset",
|
||||
[LogLevel.Debug]: "blue",
|
||||
[LogLevel.Info]: "dim",
|
||||
[LogLevel.Warning]: "yellow",
|
||||
[LogLevel.Error]: "red",
|
||||
}
|
||||
const chalkFn: any = chalk[levelColor[level]]
|
||||
const key: "log" | "warn" | "error" = level === LogLevel.Error ? "error" : level === LogLevel.Warning ? "warn" : "log"
|
||||
const logFn: any = console[key]
|
||||
logFn(
|
||||
...obj.map((i) =>
|
||||
i instanceof Error
|
||||
? chalkFn(i, JSON.stringify(i, undefined, 1), i.stack)
|
||||
: typeof i === "object"
|
||||
? chalkFn(JSON.stringify(i, undefined, 1))
|
||||
: chalkFn(i)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export async function createDirIfNotExists(dir: string, options: ScaffoldConfig): Promise<void> {
|
||||
const parentDir = path.dirname(dir)
|
||||
|
||||
if (!(await pathExists(parentDir))) {
|
||||
await createDirIfNotExists(parentDir, options)
|
||||
}
|
||||
|
||||
if (!(await pathExists(dir))) {
|
||||
try {
|
||||
log(options, LogLevel.Debug, `Creating dir ${dir}`)
|
||||
await mkdir(dir)
|
||||
return
|
||||
} catch (e: any) {
|
||||
if (e.code !== "EEXIST") {
|
||||
throw e
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getOptionValueForFile<T>(
|
||||
filePath: string,
|
||||
data: Record<string, string>,
|
||||
fn: FileResponse<T>,
|
||||
defaultValue?: T
|
||||
): T {
|
||||
if (typeof fn !== "function") {
|
||||
return defaultValue ?? (fn as T)
|
||||
}
|
||||
return (fn as FileResponseFn<T>)(
|
||||
filePath,
|
||||
path.dirname(handlebarsParse(filePath, data)),
|
||||
path.basename(handlebarsParse(filePath, data))
|
||||
)
|
||||
}
|
||||
|
||||
export function handlebarsParse(templateBuffer: Buffer | string, data: Record<string, string>) {
|
||||
const parser = Handlebars.compile(templateBuffer.toString(), { noEscape: true })
|
||||
const outputContents = parser(data)
|
||||
return outputContents
|
||||
}
|
||||
|
||||
export async function pathExists(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await access(filePath, F_OK)
|
||||
return true
|
||||
} catch (e: any) {
|
||||
if (e.code === "ENOENT") {
|
||||
return false
|
||||
}
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
export function pascalCase(s: string): string {
|
||||
return startCase(s).replace(/\s+/g, "")
|
||||
}
|
||||
|
||||
export async function isDir(path: string): Promise<boolean> {
|
||||
const tplStat = await stat(path)
|
||||
return tplStat.isDirectory()
|
||||
}
|
||||
|
||||
export function removeGlob(template: string) {
|
||||
return template.replace(/\*/g, "").replace(/\/\//g, "/")
|
||||
}
|
||||
23
test.ts
23
test.ts
@@ -1,23 +0,0 @@
|
||||
import SimpleScaffold from './scaffold'
|
||||
import * as path from 'path'
|
||||
|
||||
const templateDir = path.join(process.cwd(), 'examples')
|
||||
|
||||
new SimpleScaffold({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output/no-create-subpath',
|
||||
createSubfolder: false,
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run()
|
||||
|
||||
new SimpleScaffold({
|
||||
templates: [templateDir + '/test-input/Component/**/*'],
|
||||
output: templateDir + '/test-output',
|
||||
locals: {
|
||||
property: 'myProp',
|
||||
value: '"value"'
|
||||
}
|
||||
}).run()
|
||||
205
tests/scaffold.test.ts
Normal file
205
tests/scaffold.test.ts
Normal file
@@ -0,0 +1,205 @@
|
||||
import mockFs from "mock-fs"
|
||||
import FileSystem from "mock-fs/lib/filesystem"
|
||||
import Scaffold from "../src/scaffold"
|
||||
import { readdirSync, readFileSync } from "fs"
|
||||
import { Console } from "console"
|
||||
|
||||
const fileStructNormal = {
|
||||
input: {
|
||||
"{{name}}.txt": "Hello, my app is {{name}}",
|
||||
},
|
||||
output: {},
|
||||
}
|
||||
|
||||
const fileStructWithData = {
|
||||
input: {
|
||||
"{{name}}.txt": "Hello, my value is {{value}}",
|
||||
},
|
||||
output: {},
|
||||
}
|
||||
|
||||
const fileStructNested = {
|
||||
input: {
|
||||
"{{name}}-1.txt": "This should be in root",
|
||||
"{{Name}}": {
|
||||
"{{name}}-2.txt": "Hello, my value is {{value}}",
|
||||
moreNesting: {
|
||||
"{{name}}-3.txt": "Hi! My value is actually NOT {{value}}!",
|
||||
},
|
||||
},
|
||||
},
|
||||
output: {},
|
||||
}
|
||||
// let logsTemp: any = []
|
||||
// let logMock: any
|
||||
function withMock(fileStruct: FileSystem.DirectoryItems, testFn: jest.EmptyFunction): jest.EmptyFunction {
|
||||
return () => {
|
||||
beforeEach(() => {
|
||||
// console.log("Mocking:", fileStruct)
|
||||
console = new Console(process.stdout, process.stderr)
|
||||
|
||||
mockFs(fileStruct)
|
||||
// logMock = jest.spyOn(console, 'log').mockImplementation((...args) => {
|
||||
// logsTemp.push(args)
|
||||
// })
|
||||
})
|
||||
testFn()
|
||||
afterEach(() => {
|
||||
// console.log("Restoring mock")
|
||||
mockFs.restore()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
describe("Scaffold", () => {
|
||||
describe(
|
||||
"create subfolder",
|
||||
withMock(fileStructNormal, () => {
|
||||
test("should not create by default", async () => {
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input"],
|
||||
verbose: 0,
|
||||
})
|
||||
const data = readFileSync(process.cwd() + "/output/app_name.txt")
|
||||
expect(data.toString()).toBe("Hello, my app is app_name")
|
||||
})
|
||||
|
||||
test("should create with config", async () => {
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input"],
|
||||
createSubFolder: true,
|
||||
verbose: 0,
|
||||
})
|
||||
|
||||
const data = readFileSync(process.cwd() + "/output/app_name/app_name.txt")
|
||||
expect(data.toString()).toBe("Hello, my app is app_name")
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
describe(
|
||||
"overwrite",
|
||||
withMock(fileStructWithData, () => {
|
||||
test("should not overwrite by default", async () => {
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input"],
|
||||
data: { value: "1" },
|
||||
verbose: 0,
|
||||
})
|
||||
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input"],
|
||||
data: { value: "2" },
|
||||
verbose: 0,
|
||||
})
|
||||
|
||||
const data = readFileSync(process.cwd() + "/output/app_name.txt")
|
||||
expect(data.toString()).toBe("Hello, my value is 1")
|
||||
})
|
||||
|
||||
test("should overwrite with config", async () => {
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input"],
|
||||
data: { value: "1" },
|
||||
verbose: 0,
|
||||
})
|
||||
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input"],
|
||||
data: { value: "2" },
|
||||
overwrite: true,
|
||||
verbose: 0,
|
||||
})
|
||||
|
||||
const data = readFileSync(process.cwd() + "/output/app_name.txt")
|
||||
expect(data.toString()).toBe("Hello, my value is 2")
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
describe(
|
||||
"errors",
|
||||
withMock(fileStructNormal, () => {
|
||||
let consoleMock1: jest.SpyInstance
|
||||
beforeAll(() => {
|
||||
consoleMock1 = jest.spyOn(console, "error").mockImplementation(() => void 0)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
consoleMock1.mockRestore()
|
||||
})
|
||||
|
||||
test("should throw for bad input", async () => {
|
||||
await expect(
|
||||
Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["non-existing-input"],
|
||||
data: { value: "1" },
|
||||
verbose: 0,
|
||||
})
|
||||
).rejects.toThrow()
|
||||
|
||||
expect(() => readFileSync(process.cwd() + "/output/app_name.txt")).toThrow()
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
describe(
|
||||
"outputPath override",
|
||||
withMock(fileStructNormal, () => {
|
||||
test("should allow override function", async () => {
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: (fullPath, basedir, basename) => `custom-output/${basename.split(".")[0]}`,
|
||||
templates: ["input"],
|
||||
data: { value: "1" },
|
||||
verbose: 0,
|
||||
})
|
||||
const data = readFileSync(process.cwd() + "/custom-output/app_name/app_name.txt")
|
||||
expect(data.toString()).toBe("Hello, my app is app_name")
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
describe(
|
||||
"output structure",
|
||||
withMock(fileStructNested, () => {
|
||||
test("should maintain input structure on output", async () => {
|
||||
await Scaffold({
|
||||
name: "app_name",
|
||||
output: "output",
|
||||
templates: ["input"],
|
||||
data: { value: "1" },
|
||||
verbose: 0,
|
||||
})
|
||||
|
||||
const rootDir = readdirSync(process.cwd() + "/output")
|
||||
const dir = readdirSync(process.cwd() + "/output/AppName")
|
||||
const nestedDir = readdirSync(process.cwd() + "/output/AppName/moreNesting")
|
||||
expect(rootDir).toHaveProperty("length")
|
||||
expect(dir).toHaveProperty("length")
|
||||
expect(nestedDir).toHaveProperty("length")
|
||||
|
||||
const rootFile = readFileSync(process.cwd() + "/output/app_name-1.txt")
|
||||
const oneDeepFile = readFileSync(process.cwd() + "/output/AppName/app_name-2.txt")
|
||||
const twoDeepFile = readFileSync(process.cwd() + "/output/AppName/moreNesting/app_name-3.txt")
|
||||
expect(rootFile.toString()).toEqual("This should be in root")
|
||||
expect(oneDeepFile.toString()).toEqual("Hello, my value is 1")
|
||||
expect(twoDeepFile.toString()).toEqual("Hi! My value is actually NOT 1!")
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -1,14 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"target": "ES2019",
|
||||
"module": "commonjs",
|
||||
"lib": ["es2017", "es6"],
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"lib": [
|
||||
"ES2019",
|
||||
],
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
"strict": true,
|
||||
"sourceMap": true
|
||||
"sourceMap": true,
|
||||
"removeComments": false,
|
||||
},
|
||||
"include": [
|
||||
"src/index.ts",
|
||||
"src/cmd.ts",
|
||||
],
|
||||
"exclude": [
|
||||
"examples"
|
||||
"tests/*"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
const path = require('path')
|
||||
const webpack = require('webpack')
|
||||
const nodeExternals = require('webpack-node-externals')
|
||||
|
||||
module.exports = {
|
||||
devtool: process.env.NODE_ENV === 'develop' ? 'inline-source-map' : 'source-map',
|
||||
target: 'node',
|
||||
entry: {
|
||||
scaffold: './scaffold.ts',
|
||||
test: './test.ts',
|
||||
cmd: './cmd.ts',
|
||||
},
|
||||
output: {
|
||||
filename: '[name].js',
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
|
||||
library: 'library',
|
||||
libraryTarget: 'umd',
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts']
|
||||
},
|
||||
externals: [nodeExternals()],
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
loader: 'ts-loader',
|
||||
exclude: ['./examples', 'node_modules']
|
||||
}
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
new webpack.DefinePlugin({
|
||||
'__dirname': '__dirname'
|
||||
}),
|
||||
new webpack.BannerPlugin({
|
||||
banner: '#!/usr/bin/env node',
|
||||
raw: true,
|
||||
include: /cmd\.js/,
|
||||
})
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user