Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
adc95809ba | ||
| 98b326c843 | |||
| ddc115a037 | |||
| 19e7b0f0c3 | |||
|
|
f883571daa | ||
|
|
be3068a533 | ||
|
|
8acc660dea | ||
|
|
df6c351cb0 | ||
|
|
5f810e2116 | ||
| d579c09c11 | |||
| 3765398ab9 | |||
|
|
9be8a8b71b | ||
| 5cb8c3c081 | |||
| 3b52c255f0 | |||
| 80cf2076b0 | |||
| 4fd710b763 | |||
| 4e7ac34db9 | |||
| e48b832e0b | |||
| 06ffa656ae | |||
| 919fd54ebb | |||
|
|
dbc3283d5a | ||
|
|
0e567ec56f | ||
|
|
0ec671fe83 | ||
| 1b70897f98 | |||
| 43c9e8748f | |||
| 65b14bc707 | |||
| f583af662c | |||
| 600cc78186 | |||
| b4aea804cb | |||
| 795635dc61 | |||
| 6a026ce1a1 | |||
| 268e4d973c | |||
| a403d9df9b | |||
| 298beff355 | |||
| 8f1d58f2c2 | |||
| b10d69d2fe |
3
.github/workflows/docs.yml
vendored
@@ -1,5 +1,8 @@
|
||||
name: Documentation
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, pre, develop]
|
||||
|
||||
31
CHANGELOG.md
@@ -1,5 +1,36 @@
|
||||
# Change Log
|
||||
|
||||
## [2.2.1](https://github.com/chenasraf/simple-scaffold/compare/v2.2.0...v2.2.1) (2024-04-21)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* beforeWrite from config files ([98b326c](https://github.com/chenasraf/simple-scaffold/commit/98b326c84346162f379af46bc5aefb69df8be515))
|
||||
* use console.info for handlebars parse warning ([19e7b0f](https://github.com/chenasraf/simple-scaffold/commit/19e7b0f0c35c1b79a98781bdec9f54354123d8e0))
|
||||
|
||||
# [2.2.0](https://github.com/chenasraf/simple-scaffold/compare/v2.1.0...v2.2.0) (2024-02-23)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* `list` command ([d579c09](https://github.com/chenasraf/simple-scaffold/commit/d579c09c11f2149fe7bb4515297c1287fa67083e))
|
||||
* add `--before-write` cli option ([#89](https://github.com/chenasraf/simple-scaffold/issues/89)) ([5f810e2](https://github.com/chenasraf/simple-scaffold/commit/5f810e21160816bc683cc0f375de318ff874871c))
|
||||
|
||||
# [2.1.0](https://github.com/chenasraf/simple-scaffold/compare/v2.0.2...v2.1.0) (2024-02-12)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* support directory in --config flag ([e48b832](https://github.com/chenasraf/simple-scaffold/commit/e48b832e0b72a084d33fa2cbcca332e8209a734f))
|
||||
* support providing name in config ([4e7ac34](https://github.com/chenasraf/simple-scaffold/commit/4e7ac34db9bf67d012bbd1c06c1a26bc5ac93559))
|
||||
|
||||
## [2.0.2](https://github.com/chenasraf/simple-scaffold/compare/v2.0.1...v2.0.2) (2024-02-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* try to await scaffold before finally ([1b70897](https://github.com/chenasraf/simple-scaffold/commit/1b70897f9840e6365ff800490fbb813b9840177d))
|
||||
|
||||
## [2.0.1](https://github.com/chenasraf/simple-scaffold/compare/v2.0.0...v2.0.1) (2024-02-02)
|
||||
|
||||
|
||||
|
||||
182
README.md
@@ -1,4 +1,6 @@
|
||||
<h1 align="center">Simple Scaffold</h1>
|
||||
<p align="center">
|
||||
<img src="https://chenasraf.github.io//simple-scaffold/img/logo-lg.png" alt="Logo" />
|
||||
</p>
|
||||
|
||||
<h2 align="center">
|
||||
|
||||
@@ -31,54 +33,76 @@ lifting for you and start building your projects faster and more efficiently tod
|
||||
|
||||
---
|
||||
|
||||
## Quick Start
|
||||
## Documentation
|
||||
|
||||
### Local Templates
|
||||
See full documentation [here](https://chenasraf.github.io/simple-scaffold).
|
||||
|
||||
The fastest way to get started is to use `npx` to immediately start a scaffold process.
|
||||
- [Command Line Interface (CLI) usage](https://chenasraf.github.io/simple-scaffold/docs/usage/cli)
|
||||
- [Node.js usage](https://chenasraf.github.io/simple-scaffold/docs/usage/node)
|
||||
- [Templates](https://chenasraf.github.io/simple-scaffold/docs/usage/templates)
|
||||
- [Configuration Files](https://chenasraf.github.io/simple-scaffold/docs/usage/configuration_files)
|
||||
- [Migration](https://chenasraf.github.io/simple-scaffold/docs/usage/migration)
|
||||
|
||||
Prepare any templates you want to use - for example, in the directory `templates/component`; and use
|
||||
that in the CLI args. Here is a simple example file:
|
||||
## Getting Started
|
||||
|
||||
Simple Scaffold will maintain any file and directory structure you try to generate.
|
||||
### Cheat Sheet
|
||||
|
||||
`templates/component/{{ pascalName name }}.tsx`
|
||||
A quick rundown of common usage scenarios:
|
||||
|
||||
```tsx
|
||||
// Created: {{ now 'yyyy-MM-dd' }}
|
||||
import React from 'react'
|
||||
- Remote template config file on GitHub:
|
||||
|
||||
export default {{pascalCase name}}: React.FC = (props) => {
|
||||
return (
|
||||
<div className="{{camelCase name}}">{{pascalCase name}} Component</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
```sh
|
||||
npx simple-scaffold -g username/repository -c scaffold.js -k component NewComponentName
|
||||
```
|
||||
|
||||
To generate the template output, run:
|
||||
- Local template config file:
|
||||
|
||||
```sh
|
||||
npx simple-scaffold -c scaffold.js -k component NewComponentName
|
||||
```
|
||||
|
||||
- Local one-time usage:
|
||||
|
||||
```sh
|
||||
npx simple-scaffold -t templates/component -o src/components NewComponentName
|
||||
```
|
||||
|
||||
### Remote Configurations
|
||||
|
||||
The fastest way to get started is to is to re-use someone else's (or your own) work using a template
|
||||
repository.
|
||||
|
||||
A remote config can be loaded in one of these ways:
|
||||
|
||||
- For templates hosted on GitHub, the syntax is `-g user/repository_name`
|
||||
- For other Git platforms like GitLab, use `-g https://example.com/user/repository_name.git`
|
||||
|
||||
These remote configurations support multiple scaffold groups, which can be specified using the
|
||||
`--key` or `-k` argument:
|
||||
|
||||
```sh
|
||||
# generate single component
|
||||
$ npx simple-scaffold@latest \
|
||||
-t templates/component -o src/components PageWrapper
|
||||
$ npx simple-scaffold \
|
||||
-g chenasraf/simple-scaffold \
|
||||
-k component \
|
||||
PageWrapper
|
||||
|
||||
# equivalent to:
|
||||
$ npx simple-scaffold \
|
||||
-g https://github.com/chenasraf/simple-scaffold.git \
|
||||
-c scaffold.config.js \
|
||||
-k component \
|
||||
PageWrapper
|
||||
```
|
||||
|
||||
This will immediately create the following file: `src/components/PageWrapper.tsx`
|
||||
By default, the template name is set to `default` when the `--key` option is not provided.
|
||||
|
||||
```tsx
|
||||
// Created: 2077-01-01
|
||||
import React from 'react'
|
||||
|
||||
export default PageWrapper: React.FC = (props) => {
|
||||
return (
|
||||
<div className="pageWrapper">PageWrapper Component</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
See information about each option and flag using the `--help` flag, or read the
|
||||
[CLI documentation](https://chenasraf.github.io/simple-scaffold/docs/usage/cli). For information
|
||||
about how configuration files work, [see below](#configuration-files).
|
||||
|
||||
### Configuration Files
|
||||
|
||||
You can also use a config file to more easily maintain all your scaffold definitions.
|
||||
You can use a config file to more easily maintain all your scaffold definitions.
|
||||
|
||||
`scaffold.config.js`
|
||||
|
||||
@@ -99,66 +123,61 @@ module.exports = {
|
||||
Then call your scaffold like this:
|
||||
|
||||
```sh
|
||||
$ npx simple-scaffold@latest -c scaffold.config.js PageWrapper
|
||||
$ npx simple-scaffold -c scaffold.config.js PageWrapper
|
||||
```
|
||||
|
||||
This will allow you to avoid needing to remember which configs are needed or to store them in a
|
||||
1-liner in `packqge.json` which can get pretty long and messy, which is harder to maintain.
|
||||
one-liner in `package.json` which can get pretty long and messy, and harder to maintain.
|
||||
|
||||
Also, this allows you to define more complex scaffolds with logic without having to use the Node.js
|
||||
API directly. (Of course you always have the option to still do so if you wish)
|
||||
|
||||
See more at the [CLI documentation](https://chenasraf.github.io/simple-scaffold/docs/usage/cli) and
|
||||
[Configuration Files](https://chenasraf.github.io/simple-scaffold/docs/usage/configuration_files).
|
||||
More information can be found at the
|
||||
[Configuration Files documentation](https://chenasraf.github.io/simple-scaffold/docs/usage/configuration_files).
|
||||
|
||||
### Remote Configurations
|
||||
### Templates Structure
|
||||
|
||||
Another quick way to start is to re-use someone else's (or your own) work using a template
|
||||
repository.
|
||||
Templates are **any file** in the a directory given to `--templates`.
|
||||
|
||||
A remote config can be loaded in one of these ways:
|
||||
Simple Scaffold will maintain any file and directory structure you try to generate, while replacing
|
||||
any tokens such as `{{ name }}` or other custom-data using
|
||||
[Handlebars.js](https://handlebarsjs.com/).
|
||||
|
||||
- If it's on GitHub, you can use `-g user/repository_name`
|
||||
- If it's on another git server (such as GitLab), you can use
|
||||
`-g https://example.com/user/repository_name.git`
|
||||
`templates/component/{{ pascalName name }}.tsx`
|
||||
|
||||
Configurations can hold multiple scaffold groups. Each group can be accessed using its key by
|
||||
supplying the `--key` or `-k` argument, like so:
|
||||
```tsx
|
||||
// Created: {{ now 'yyyy-MM-dd' }}
|
||||
import React from 'react'
|
||||
|
||||
```sh
|
||||
-g user/repository_name -c scaffold.js -k key_name`.
|
||||
export default {{pascalCase name}}: React.FC = (props) => {
|
||||
return (
|
||||
<div className="{{camelCase name}}">{{pascalCase name}} Component</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
Here is an example for loading the example component templates in this very repository:
|
||||
To generate the template output once without saving a configuration file, run:
|
||||
|
||||
```sh
|
||||
$ npx simple-scaffold@latest \
|
||||
-g chenasraf/simple-scaffold \
|
||||
-k component \
|
||||
PageWrapper
|
||||
|
||||
# equivalent to:
|
||||
$ npx simple-scaffold@latest \
|
||||
-g https://github.com/chenasraf/simple-scaffold.git \
|
||||
-c scaffold.config.js \
|
||||
-k component \
|
||||
# generate single component
|
||||
$ npx simple-scaffold \
|
||||
-t templates/component \
|
||||
-o src/components \
|
||||
PageWrapper
|
||||
```
|
||||
|
||||
When template name (`-k component`) is omitted, `default` is used.
|
||||
This will immediately create the following file: `src/components/PageWrapper.tsx`
|
||||
|
||||
See more at the [CLI documentation](https://chenasraf.github.io/simple-scaffold/docs/usage/cli) and
|
||||
[Configuration Files](https://chenasraf.github.io/simple-scaffold/docs/usage/configuration_files).
|
||||
```tsx
|
||||
// Created: 2077-01-01
|
||||
import React from 'react'
|
||||
|
||||
## Documentation
|
||||
|
||||
See full documentation [here](https://chenasraf.github.io/simple-scaffold).
|
||||
|
||||
- [Command Line Interface (CLI) usage](https://chenasraf.github.io/simple-scaffold/docs/usage/cli)
|
||||
- [Node.js usage](https://chenasraf.github.io/simple-scaffold/docs/usage/node)
|
||||
- [Templates](https://chenasraf.github.io/simple-scaffold/docs/usage/templates)
|
||||
- [Configuration Files](https://chenasraf.github.io/simple-scaffold/docs/usage/configuration_files)
|
||||
- [Migration](https://chenasraf.github.io/simple-scaffold/docs/usage/migration)
|
||||
export default PageWrapper: React.FC = (props) => {
|
||||
return (
|
||||
<div className="pageWrapper">PageWrapper Component</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
@@ -189,22 +208,9 @@ If you are a developer and want to contribute code, here are some starting tips:
|
||||
|
||||
Some tips on getting around the code:
|
||||
|
||||
- Use `pnpm dev` for development - it runs TypeScript compile in watch mode, allowing you to make
|
||||
changes and immediately be able to try them using `pnpm cmd`.
|
||||
- Use `pnpm build` to build the output once
|
||||
- Use `pnpm test` to run tests
|
||||
- Use `pnpm cmd` to use the CLI feature of Simple Scaffold from within the root directory, enabling
|
||||
you to test different behaviors. See `pnpm cmd -h` for more information.
|
||||
|
||||
> This requires an updated build, and does not trigger one itself. From here you have several
|
||||
> options:
|
||||
>
|
||||
> - Run `pnpm dev` to watch for file changes and build automatically
|
||||
> - Run `pnpm build` before running this to trigger a one-time build
|
||||
> - Run `pnpm build-cmd` which triggers a build right before running `pnpm cmd` automatically with
|
||||
> the rest of the given arguments.
|
||||
|
||||
- Use `pnpm build-docs` to build the documentation once
|
||||
- Use `pnpm watch-docs` to start docs in watch mode
|
||||
- To see the documentation, currently you have to serve the directory yourself with a static web
|
||||
server (like node's built in serve, VS code's "Go Live" mode, etc)
|
||||
- Use `pnpm test` to run tests
|
||||
- Use `pnpm docs:build` to build the documentation once
|
||||
- Use `pnpm docs:watch` to start docs in watch mode
|
||||
- Use `pnpm build` to build the output
|
||||
|
||||
@@ -50,7 +50,6 @@ The contents of the file will be transformed in a similar fashion.
|
||||
|
||||
Your `data` will be pre-populated with the following:
|
||||
|
||||
- `{{Name}}`: PascalCase of the component name
|
||||
- `{{name}}`: raw name of the component as you entered it
|
||||
|
||||
> Simple-Scaffold uses [Handlebars.js](https://handlebarsjs.com/) for outputting the file contents.
|
||||
@@ -110,7 +109,15 @@ Further details:
|
||||
```
|
||||
|
||||
- **The now helper** (for current time) takes the same arguments, minus the first one (`date`) as it
|
||||
is implicitly the current date.
|
||||
is implicitly the current date:
|
||||
|
||||
```typescript
|
||||
(
|
||||
format: string,
|
||||
offsetAmount?: number,
|
||||
offsetType?: "years" | "months" | "weeks" | "days" | "hours" | "minutes" | "seconds"
|
||||
)
|
||||
```
|
||||
|
||||
### Custom Helpers
|
||||
|
||||
@@ -23,48 +23,11 @@ module.exports = {
|
||||
}
|
||||
```
|
||||
|
||||
The configuration contents are identical to the
|
||||
[Node.js configuration structure](https://chenasraf.github.io/simple-scaffold/docs/usage/node):
|
||||
|
||||
```ts
|
||||
interface ScaffoldConfig {
|
||||
name: string
|
||||
templates: string[]
|
||||
output: FileResponse<string>
|
||||
subdir?: boolean
|
||||
git?: string
|
||||
config?: string
|
||||
key?: string
|
||||
data?: Record<string, any>
|
||||
overwrite?: FileResponse<boolean>
|
||||
quiet?: boolean
|
||||
verbose?: LogLevel
|
||||
dryRun?: boolean
|
||||
helpers?: Record<string, Helper>
|
||||
subdirHelper?: DefaultHelpers | string
|
||||
beforeWrite?(
|
||||
content: Buffer,
|
||||
rawContent: Buffer,
|
||||
outputPath: string,
|
||||
): string | Buffer | undefined | Promise<string | Buffer | undefined>
|
||||
}
|
||||
```
|
||||
For the full configuration options, see [ScaffoldConfigFile](../api/modules#scaffoldconfigfile).
|
||||
|
||||
If you want to supply functions inside the configurations, you must use a `.js`/`.cjs`/`.mjs` file
|
||||
as JSON does not support non-primitives.
|
||||
|
||||
A `.js` file can be just like a `.json` file, make sure to export the final configuration:
|
||||
|
||||
```js
|
||||
/** @type {import('simple-scaffold').ScaffoldConfigFile} */
|
||||
module.exports = {
|
||||
component: {
|
||||
templates: ["templates/component"],
|
||||
output: "src/components",
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
Another feature of using a JS file is you can export a function which will be loaded with the CMD
|
||||
config provided to Simple Scaffold. The `extra` key contains any values not consumed by built-in
|
||||
flags, so you can pre-process your args before outputting a config:
|
||||
@@ -82,6 +45,12 @@ module.exports = (config) => {
|
||||
}
|
||||
```
|
||||
|
||||
If you want to provide templates that need no name (such as common config files which are easily
|
||||
portable between projects), you may provide the `name` property in the config object.
|
||||
|
||||
You will always be able to override it using `--name NewName`, but it will be given a value by
|
||||
default and therefore it will no longer be required in the CLI arguments.
|
||||
|
||||
## Using a config file
|
||||
|
||||
Once your config is created, you can use it by providing the file name to the `--config` (or `-c`
|
||||
@@ -116,7 +85,7 @@ And then:
|
||||
simple-scaffold -c scaffold.json MyComponentName
|
||||
```
|
||||
|
||||
- When the filename is omitted, the following files will be tried in order:
|
||||
- When the a directory is given, the following files in the given directory will be tried in order:
|
||||
|
||||
- `scaffold.config.*`
|
||||
- `scaffold.*`
|
||||
@@ -162,6 +131,12 @@ simple-scaffold -g git://gitlab.com/<username>/<project_name> [-c <filename>] [-
|
||||
simple-scaffold -g https://gitlab.com/<username>/<project_name>.git [-c <filename>] [-k <template_key>]
|
||||
```
|
||||
|
||||
When a config file path is omitted, the files given in the list above will be tried on the root
|
||||
directory of the git repository.
|
||||
|
||||
**Note:** The repository will be cloned to a temporary directory and removed after the scaffolding
|
||||
has been done.
|
||||
|
||||
## Use In Node.js
|
||||
|
||||
You can also start a scaffold from Node.js with a remote file or URL config.
|
||||
@@ -11,23 +11,57 @@ Usage: simple-scaffold [options]
|
||||
To see this and more information anytime, add the `-h` or `--help` flag to your call, e.g.
|
||||
`npx simple-scaffold@latest -h`.
|
||||
|
||||
| Command \| alias | |
|
||||
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `--name` \| `-n` | Name to be passed to the generated files. `{{name}}` and other data parameters inside contents and file names will be replaced accordingly. You may omit the `--name` or `-n` for this specific option. |
|
||||
| `--config`\|`-c` | Filename to load config from instead of passing arguments to CLI or using a Node.js script. See examples for syntax. This can also work in conjunction with `--git` or `--github` to point to remote files, and with `--key` to denote which key to select from the file., |
|
||||
| `--git`\|`-g` | Git URL to load config from instead of passing arguments to CLI or using a Node.js script. See examples for syntax. |
|
||||
| `--key` \| `-k` | Key to load inside the config file. This overwrites the config key provided after the colon in `--config` (e.g. `--config scaffold.cmd.js:component`) |
|
||||
| `--output` \| `-o` | Path to output to. If `--create-sub-folder` is enabled, the subfolder will be created inside this path. Default is current working directory. |
|
||||
| `--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. |
|
||||
| `--overwrite` \| `-w` | Enable to override output files, even if they already exist. |
|
||||
| `--data` \| `-d` | Add custom data to the templates. By default, only your app name is included. |
|
||||
| `--append-data` \| `-D` | Append additional custom data to the templates, which will overwrite `--data`, using an alternate syntax, which is easier to use with CLI: `-D key1=string -D key2:=raw` |
|
||||
| `--create-sub-folder` \| `-s` | Create subfolder with the input name |
|
||||
| `--sub-folder-name-helper` \| `-sh` | Default helper to apply to subfolder name when using `--create-sub-folder true`. |
|
||||
| `--quiet` \| `-q` | Suppress output logs (Same as `--log-level none`) |
|
||||
| `--log-level` \| `-l` | Determine amount of logs to display. The values are: `none \| debug \| info \| warn \| error`. The provided level will display messages of the same level or higher. |
|
||||
| `--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. |
|
||||
| `--help` \| `-h` | Show this help message |
|
||||
| Command \| alias | |
|
||||
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `--name` \| `-n` | Name to be passed to the generated files. `{{name}}` and other data parameters inside contents and file names will be replaced accordingly. You may omit the `--name` or `-n` for this specific option. |
|
||||
| `--config`\|`-c` | Filename or directory to load config from |
|
||||
| `--git`\|`-g` | Git URL or GitHub path to load a template from. |
|
||||
| `--key` \| `-k` | Key to load inside the config file. This overwrites the config key provided after the colon in `--config` (e.g. `--config scaffold.cmd.js:component`) |
|
||||
| `--output` \| `-o` | Path to output to. If `--create-sub-folder` is enabled, the subfolder will be created inside this path. Default is current working directory. |
|
||||
| `--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. |
|
||||
| `--overwrite` \| `-w` | Enable to override output files, even if they already exist. |
|
||||
| `--data` \| `-d` | Add custom data to the templates. By default, only your app name is included. |
|
||||
| `--append-data` \| `-D` | Append additional custom data to the templates, which will overwrite `--data`, using an alternate syntax, which is easier to use with CLI: `-D key1=string -D key2:=raw` |
|
||||
| `--create-sub-folder` \| `-s` | Create subfolder with the input name |
|
||||
| `--sub-folder-name-helper` \| `-sh` | Default helper to apply to subfolder name when using `--create-sub-folder true`. |
|
||||
| `--quiet` \| `-q` | Suppress output logs (Same as `--log-level none`) |
|
||||
| `--log-level` \| `-l` | Determine amount of logs to display. The values are: `none \| debug \| info \| warn \| error`. The provided level will display messages of the same level or higher. |
|
||||
| `--before-write` \| `-B` | Run a script before writing the files. This can be a command or a path to a file. A temporary file path will be passed to the given command and the command should return a string for the final output. |
|
||||
| `--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. |
|
||||
| `--help` \| `-h` | Show this help message |
|
||||
| `--version` \| `-v` | Display version. |
|
||||
|
||||
### Before Write option
|
||||
|
||||
This option allows you to preprocess a file before it is being written, such as running a formatter,
|
||||
linter or other commands.
|
||||
|
||||
To use this option, pass it the command you would like to run. The following tokens will be replaced
|
||||
in your string:
|
||||
|
||||
- `{{path}}` - the temporary file path for you to read from
|
||||
- `{{rawpath}}` - a different file path containing the raw file contents **before** they were
|
||||
handled by Handlebars.js.
|
||||
|
||||
If none of these tokens are found, the regular (non-raw) path will be appended to the end of the
|
||||
command.
|
||||
|
||||
```shell
|
||||
simple-scaffold -c . --before-write prettier
|
||||
# command: prettier /tmp/somefile
|
||||
|
||||
simple-scaffold -c . --before-write 'cat {{path}} | my-linter'
|
||||
# command: cat /tmp/somefile | my-linter
|
||||
```
|
||||
|
||||
The command should return the string to write to the file through standard output (stdout), and not
|
||||
re-write the tmp file as it is not used for writing. Returning an empty string (after trimming) will
|
||||
discard the result and write the original file contents.
|
||||
|
||||
See
|
||||
[beforeWrite](https://chenasraf.github.io/simple-scaffold/docs/api/interfaces/ScaffoldConfig#beforewrite)
|
||||
Node.js API for more details. Instead of returning `undefined` to keep the default behavior, you can
|
||||
output `''` for the same effect.
|
||||
|
||||
## Examples:
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
title: Node.js Usage
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
You can build the scaffold yourself, if you want to create more complex arguments, scaffold groups,
|
||||
etc - simply pass a config object to the Scaffold function when you are ready to start.
|
||||
|
||||
@@ -33,6 +35,19 @@ interface ScaffoldConfig {
|
||||
}
|
||||
```
|
||||
|
||||
### Before Write option
|
||||
|
||||
This option allows you to preprocess a file before it is being written, such as running a formatter,
|
||||
linter or other commands.
|
||||
|
||||
To use this option, you can run any async/blocking command, and return a string as the final output
|
||||
to be used as the file contents.
|
||||
|
||||
Returning `undefined` will keep the file contents as-is, after normal Handlebars.js procesing by
|
||||
Simple Scaffold.
|
||||
|
||||
## Example
|
||||
|
||||
This is an example of loading a complete scaffold via Node.js:
|
||||
|
||||
```typescript
|
||||
@@ -50,6 +65,8 @@ const config = {
|
||||
helpers: {
|
||||
twice: (text) => [text, text].join(" ")
|
||||
},
|
||||
// return a string to replace the final file contents after pre-processing, or `undefined`
|
||||
// to keep it as-is
|
||||
beforeWrite: (content, rawContent, outputPath) => content.toString().toUpperCase()
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import type * as Preset from "@docusaurus/preset-classic"
|
||||
const config: Config = {
|
||||
title: "Simple Scaffold",
|
||||
tagline: "Generate any file structure - from single components to entire app boilerplates, with a single command.",
|
||||
favicon: "img/favicon.ico",
|
||||
favicon: "img/favicon.svg",
|
||||
|
||||
// Set the production url of your site here
|
||||
url: "https://chenasraf.github.io",
|
||||
@@ -64,13 +64,7 @@ const config: Config = {
|
||||
sidebarPath: "./sidebars.ts",
|
||||
// Please change this to your repo.
|
||||
// Remove this to remove the "edit this page" links.
|
||||
editUrl: "https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/",
|
||||
},
|
||||
blog: {
|
||||
showReadingTime: true,
|
||||
// Please change this to your repo.
|
||||
// Remove this to remove the "edit this page" links.
|
||||
editUrl: "https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/",
|
||||
editUrl: "https://github.com/chenasraf/simple-scaffold/blob/master/docs",
|
||||
},
|
||||
theme: {
|
||||
customCss: "./src/css/custom.css",
|
||||
@@ -89,7 +83,7 @@ const config: Config = {
|
||||
title: "Simple Scaffold",
|
||||
logo: {
|
||||
alt: "Simple Scaffold",
|
||||
src: "img/logo.svg",
|
||||
src: "img/favicon.svg",
|
||||
},
|
||||
items: [
|
||||
{
|
||||
@@ -127,7 +121,7 @@ const config: Config = {
|
||||
position: "right",
|
||||
},
|
||||
{
|
||||
href: "https://github.com/facebook/docusaurus",
|
||||
href: "https://github.com/chenasraf/simple-scaffold",
|
||||
label: "GitHub",
|
||||
position: "right",
|
||||
},
|
||||
|
||||
74
docs/pnpm-lock.yaml
generated
@@ -13,10 +13,10 @@ dependencies:
|
||||
version: 3.1.1(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/preset-classic':
|
||||
specifier: 3.1.1
|
||||
version: 3.1.1(@algolia/client-search@4.22.1)(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2)
|
||||
version: 3.1.1(@algolia/client-search@4.22.1)(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2)
|
||||
'@mdx-js/react':
|
||||
specifier: ^3.0.0
|
||||
version: 3.0.0(@types/react@18.2.48)(react@18.2.0)
|
||||
version: 3.0.0(@types/react@18.2.69)(react@18.2.0)
|
||||
clsx:
|
||||
specifier: ^2.1.0
|
||||
version: 2.1.0
|
||||
@@ -1523,7 +1523,7 @@ packages:
|
||||
resolution: {integrity: sha512-SPiDHaWKQZpwR2siD0KQUwlStvIAnEyK6tAE2h2Wuoq8ue9skzhlyVQ1ddzOxX6khULnAALDiR/isSF3bnuciA==}
|
||||
dev: false
|
||||
|
||||
/@docsearch/react@3.5.2(@algolia/client-search@4.22.1)(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0):
|
||||
/@docsearch/react@3.5.2(@algolia/client-search@4.22.1)(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0):
|
||||
resolution: {integrity: sha512-9Ahcrs5z2jq/DcAvYtvlqEBHImbm4YJI8M9y0x6Tqg598P40HTEkX7hsMcIuThI+hTFxRGZ9hll0Wygm2yEjng==}
|
||||
peerDependencies:
|
||||
'@types/react': '>= 16.8.0 < 19.0.0'
|
||||
@@ -1543,7 +1543,7 @@ packages:
|
||||
'@algolia/autocomplete-core': 1.9.3(@algolia/client-search@4.22.1)(algoliasearch@4.22.1)(search-insights@2.13.0)
|
||||
'@algolia/autocomplete-preset-algolia': 1.9.3(@algolia/client-search@4.22.1)(algoliasearch@4.22.1)
|
||||
'@docsearch/css': 3.5.2
|
||||
'@types/react': 18.2.48
|
||||
'@types/react': 18.2.69
|
||||
algoliasearch: 4.22.1
|
||||
react: 18.2.0
|
||||
react-dom: 18.2.0(react@18.2.0)
|
||||
@@ -2028,7 +2028,7 @@ packages:
|
||||
- webpack-cli
|
||||
dev: false
|
||||
|
||||
/@docusaurus/preset-classic@3.1.1(@algolia/client-search@4.22.1)(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2):
|
||||
/@docusaurus/preset-classic@3.1.1(@algolia/client-search@4.22.1)(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2):
|
||||
resolution: {integrity: sha512-jG4ys/hWYf69iaN/xOmF+3kjs4Nnz1Ay3CjFLDtYa8KdxbmUhArA9HmP26ru5N0wbVWhY+6kmpYhTJpez5wTyg==}
|
||||
engines: {node: '>=18.0'}
|
||||
peerDependencies:
|
||||
@@ -2044,9 +2044,9 @@ packages:
|
||||
'@docusaurus/plugin-google-gtag': 3.1.1(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/plugin-google-tag-manager': 3.1.1(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/plugin-sitemap': 3.1.1(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/theme-classic': 3.1.1(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/theme-classic': 3.1.1(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/theme-common': 3.1.1(@docusaurus/types@3.1.1)(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/theme-search-algolia': 3.1.1(@algolia/client-search@4.22.1)(@docusaurus/types@3.1.1)(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2)
|
||||
'@docusaurus/theme-search-algolia': 3.1.1(@algolia/client-search@4.22.1)(@docusaurus/types@3.1.1)(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2)
|
||||
'@docusaurus/types': 3.1.1(react-dom@18.2.0)(react@18.2.0)
|
||||
react: 18.2.0
|
||||
react-dom: 18.2.0(react@18.2.0)
|
||||
@@ -2081,7 +2081,7 @@ packages:
|
||||
prop-types: 15.8.1
|
||||
react: 18.2.0
|
||||
|
||||
/@docusaurus/theme-classic@3.1.1(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2):
|
||||
/@docusaurus/theme-classic@3.1.1(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2):
|
||||
resolution: {integrity: sha512-GiPE/jbWM8Qv1A14lk6s9fhc0LhPEQ00eIczRO4QL2nAQJZXkjPG6zaVx+1cZxPFWbAsqSjKe2lqkwF3fGkQ7Q==}
|
||||
engines: {node: '>=18.0'}
|
||||
peerDependencies:
|
||||
@@ -2100,7 +2100,7 @@ packages:
|
||||
'@docusaurus/utils': 3.1.1(@docusaurus/types@3.1.1)
|
||||
'@docusaurus/utils-common': 3.1.1(@docusaurus/types@3.1.1)
|
||||
'@docusaurus/utils-validation': 3.1.1(@docusaurus/types@3.1.1)
|
||||
'@mdx-js/react': 3.0.0(@types/react@18.2.48)(react@18.2.0)
|
||||
'@mdx-js/react': 3.0.0(@types/react@18.2.69)(react@18.2.0)
|
||||
clsx: 2.1.0
|
||||
copy-text-to-clipboard: 3.2.0
|
||||
infima: 0.2.0-alpha.43
|
||||
@@ -2179,14 +2179,14 @@ packages:
|
||||
- webpack-cli
|
||||
dev: false
|
||||
|
||||
/@docusaurus/theme-search-algolia@3.1.1(@algolia/client-search@4.22.1)(@docusaurus/types@3.1.1)(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2):
|
||||
/@docusaurus/theme-search-algolia@3.1.1(@algolia/client-search@4.22.1)(@docusaurus/types@3.1.1)(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)(typescript@5.2.2):
|
||||
resolution: {integrity: sha512-tBH9VY5EpRctVdaAhT+b1BY8y5dyHVZGFXyCHgTrvcXQy5CV4q7serEX7U3SveNT9zksmchPyct6i1sFDC4Z5g==}
|
||||
engines: {node: '>=18.0'}
|
||||
peerDependencies:
|
||||
react: ^18.0.0
|
||||
react-dom: ^18.0.0
|
||||
dependencies:
|
||||
'@docsearch/react': 3.5.2(@algolia/client-search@4.22.1)(@types/react@18.2.48)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)
|
||||
'@docsearch/react': 3.5.2(@algolia/client-search@4.22.1)(@types/react@18.2.69)(react-dom@18.2.0)(react@18.2.0)(search-insights@2.13.0)
|
||||
'@docusaurus/core': 3.1.1(@docusaurus/types@3.1.1)(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
'@docusaurus/logger': 3.1.1
|
||||
'@docusaurus/plugin-content-docs': 3.1.1(react-dom@18.2.0)(react@18.2.0)(typescript@5.2.2)
|
||||
@@ -2420,14 +2420,14 @@ packages:
|
||||
transitivePeerDependencies:
|
||||
- supports-color
|
||||
|
||||
/@mdx-js/react@3.0.0(@types/react@18.2.48)(react@18.2.0):
|
||||
/@mdx-js/react@3.0.0(@types/react@18.2.69)(react@18.2.0):
|
||||
resolution: {integrity: sha512-nDctevR9KyYFyV+m+/+S4cpzCWHqj+iHDHq3QrsWezcC+B17uZdIWgCguESUkwFhM3n/56KxWVE3V6EokrmONQ==}
|
||||
peerDependencies:
|
||||
'@types/react': '>=16'
|
||||
react: '>=16'
|
||||
dependencies:
|
||||
'@types/mdx': 2.0.10
|
||||
'@types/react': 18.2.48
|
||||
'@types/react': 18.2.69
|
||||
react: 18.2.0
|
||||
dev: false
|
||||
|
||||
@@ -2852,6 +2852,10 @@ packages:
|
||||
/@types/prop-types@15.7.11:
|
||||
resolution: {integrity: sha512-ga8y9v9uyeiLdpKddhxYQkxNDrfvuPrlFb0N1qnZZByvcElJaXthF1UhvCh9TLWJBEHeNtdnbysW7Y6Uq8CVng==}
|
||||
|
||||
/@types/prop-types@15.7.12:
|
||||
resolution: {integrity: sha512-5zvhXYtRNRluoE/jAp4GVsSduVUzNWKkOZrCDBWYtE7biZywwdC2AcEzg+cSMLFRfVgeAFqpfNabiPjxFddV1Q==}
|
||||
dev: false
|
||||
|
||||
/@types/qs@6.9.11:
|
||||
resolution: {integrity: sha512-oGk0gmhnEJK4Yyk+oI7EfXsLayXatCWPHary1MtcmbAifkobT9cM9yutG/hZKIseOU0MqbIwQ/u2nn/Gb+ltuQ==}
|
||||
dev: false
|
||||
@@ -2887,6 +2891,14 @@ packages:
|
||||
'@types/scheduler': 0.16.8
|
||||
csstype: 3.1.3
|
||||
|
||||
/@types/react@18.2.69:
|
||||
resolution: {integrity: sha512-W1HOMUWY/1Yyw0ba5TkCV+oqynRjG7BnteBB+B7JmAK7iw3l2SW+VGOxL+akPweix6jk2NNJtyJKpn4TkpfK3Q==}
|
||||
dependencies:
|
||||
'@types/prop-types': 15.7.12
|
||||
'@types/scheduler': 0.16.8
|
||||
csstype: 3.1.3
|
||||
dev: false
|
||||
|
||||
/@types/retry@0.12.0:
|
||||
resolution: {integrity: sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==}
|
||||
dev: false
|
||||
@@ -3341,8 +3353,8 @@ packages:
|
||||
engines: {node: '>=8'}
|
||||
dev: false
|
||||
|
||||
/body-parser@1.20.1:
|
||||
resolution: {integrity: sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==}
|
||||
/body-parser@1.20.2:
|
||||
resolution: {integrity: sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==}
|
||||
engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16}
|
||||
dependencies:
|
||||
bytes: 3.1.2
|
||||
@@ -3354,7 +3366,7 @@ packages:
|
||||
iconv-lite: 0.4.24
|
||||
on-finished: 2.4.1
|
||||
qs: 6.11.0
|
||||
raw-body: 2.5.1
|
||||
raw-body: 2.5.2
|
||||
type-is: 1.6.18
|
||||
unpipe: 1.0.0
|
||||
transitivePeerDependencies:
|
||||
@@ -3776,8 +3788,8 @@ packages:
|
||||
resolution: {integrity: sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==}
|
||||
dev: false
|
||||
|
||||
/cookie@0.5.0:
|
||||
resolution: {integrity: sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==}
|
||||
/cookie@0.6.0:
|
||||
resolution: {integrity: sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==}
|
||||
engines: {node: '>= 0.6'}
|
||||
dev: false
|
||||
|
||||
@@ -4508,16 +4520,16 @@ packages:
|
||||
strip-final-newline: 2.0.0
|
||||
dev: false
|
||||
|
||||
/express@4.18.2:
|
||||
resolution: {integrity: sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==}
|
||||
/express@4.19.2:
|
||||
resolution: {integrity: sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==}
|
||||
engines: {node: '>= 0.10.0'}
|
||||
dependencies:
|
||||
accepts: 1.3.8
|
||||
array-flatten: 1.1.1
|
||||
body-parser: 1.20.1
|
||||
body-parser: 1.20.2
|
||||
content-disposition: 0.5.4
|
||||
content-type: 1.0.5
|
||||
cookie: 0.5.0
|
||||
cookie: 0.6.0
|
||||
cookie-signature: 1.0.6
|
||||
debug: 2.6.9
|
||||
depd: 2.0.0
|
||||
@@ -4679,8 +4691,8 @@ packages:
|
||||
resolution: {integrity: sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==}
|
||||
hasBin: true
|
||||
|
||||
/follow-redirects@1.15.5:
|
||||
resolution: {integrity: sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==}
|
||||
/follow-redirects@1.15.6:
|
||||
resolution: {integrity: sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==}
|
||||
engines: {node: '>=4.0'}
|
||||
peerDependencies:
|
||||
debug: '*'
|
||||
@@ -5268,7 +5280,7 @@ packages:
|
||||
engines: {node: '>=8.0.0'}
|
||||
dependencies:
|
||||
eventemitter3: 4.0.7
|
||||
follow-redirects: 1.15.5
|
||||
follow-redirects: 1.15.6
|
||||
requires-port: 1.0.0
|
||||
transitivePeerDependencies:
|
||||
- debug
|
||||
@@ -7419,8 +7431,8 @@ packages:
|
||||
engines: {node: '>= 0.6'}
|
||||
dev: false
|
||||
|
||||
/raw-body@2.5.1:
|
||||
resolution: {integrity: sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==}
|
||||
/raw-body@2.5.2:
|
||||
resolution: {integrity: sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==}
|
||||
engines: {node: '>= 0.8'}
|
||||
dependencies:
|
||||
bytes: 3.1.2
|
||||
@@ -8783,8 +8795,8 @@ packages:
|
||||
- utf-8-validate
|
||||
dev: false
|
||||
|
||||
/webpack-dev-middleware@5.3.3(webpack@5.90.0):
|
||||
resolution: {integrity: sha512-hj5CYrY0bZLB+eTO+x/j67Pkrquiy7kWepMHmUMoPsmcUaeEnQJqFzHJOyxgWlq746/wUuA64p9ta34Kyb01pA==}
|
||||
/webpack-dev-middleware@5.3.4(webpack@5.90.0):
|
||||
resolution: {integrity: sha512-BVdTqhhs+0IfoeAf7EoH5WE+exCmqGerHfDM0IL096Px60Tq2Mn9MAbnaGUe6HiMa41KMCYF19gyzZmBcq/o4Q==}
|
||||
engines: {node: '>= 12.13.0'}
|
||||
peerDependencies:
|
||||
webpack: ^4.0.0 || ^5.0.0
|
||||
@@ -8824,7 +8836,7 @@ packages:
|
||||
compression: 1.7.4
|
||||
connect-history-api-fallback: 2.0.0
|
||||
default-gateway: 6.0.3
|
||||
express: 4.18.2
|
||||
express: 4.19.2
|
||||
graceful-fs: 4.2.11
|
||||
html-entities: 2.4.0
|
||||
http-proxy-middleware: 2.0.6(@types/express@4.17.21)
|
||||
@@ -8839,7 +8851,7 @@ packages:
|
||||
sockjs: 0.3.24
|
||||
spdy: 4.0.2
|
||||
webpack: 5.90.0
|
||||
webpack-dev-middleware: 5.3.3(webpack@5.90.0)
|
||||
webpack-dev-middleware: 5.3.4(webpack@5.90.0)
|
||||
ws: 8.16.0
|
||||
transitivePeerDependencies:
|
||||
- bufferutil
|
||||
|
||||
@@ -22,3 +22,13 @@
|
||||
justify-content: center;
|
||||
gap: 2rem;
|
||||
}
|
||||
|
||||
.heroImage {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.logo {
|
||||
width: 100%;
|
||||
max-width: 300px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
@@ -12,10 +12,12 @@ function HomepageHeader() {
|
||||
return (
|
||||
<header className={clsx("hero hero--primary", styles.heroBanner)}>
|
||||
<div className="container">
|
||||
<img className={styles.logo} src="img/logo-lg.svg" alt="Simple Scaffold" />
|
||||
<Heading as="h1" className="hero__title">
|
||||
{siteConfig.title}
|
||||
</Heading>
|
||||
<p className="hero__subtitle">{siteConfig.tagline}</p>
|
||||
<img className={styles.heroImage} src="img/intro.gif" alt="Simple-Scaffold doing its thing" />
|
||||
<div className={styles.buttons}>
|
||||
<Link className="button button--secondary button--lg" to="/docs/api">
|
||||
API
|
||||
|
||||
BIN
docs/static/img/favicon.png
vendored
Normal file
|
After Width: | Height: | Size: 14 KiB |
20
docs/static/img/favicon.svg
vendored
Normal file
|
After Width: | Height: | Size: 265 KiB |
BIN
docs/static/img/logo-lg.png
vendored
Normal file
|
After Width: | Height: | Size: 22 KiB |
20
docs/static/img/logo-lg.svg
vendored
Normal file
|
After Width: | Height: | Size: 281 KiB |
BIN
docs/static/img/logo.png
vendored
Normal file
|
After Width: | Height: | Size: 24 KiB |
21
docs/static/img/logo.svg
vendored
|
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 432 KiB |
@@ -1,7 +1,7 @@
|
||||
import * as React from "react"
|
||||
import * as css from "./{{pascalCae name}}.css"
|
||||
import * as css from "./{{pascalCase name}}.css"
|
||||
|
||||
class {{pascalCae name}} extends React.Component<any> {
|
||||
class {{pascalCase name}} extends React.Component<any> {
|
||||
private {{ property }}
|
||||
|
||||
constructor(props: any) {
|
||||
@@ -10,8 +10,8 @@ class {{pascalCae name}} extends React.Component<any> {
|
||||
}
|
||||
|
||||
public render() {
|
||||
return <div className={ css.{{pascalCae name}} } />
|
||||
return <div className={ css.{{pascalCase name}} } />
|
||||
}
|
||||
}
|
||||
|
||||
export default {pascalCae nName}}
|
||||
export default {{pascalCase name}}
|
||||
|
||||
27
package.json
@@ -1,14 +1,19 @@
|
||||
{
|
||||
"name": "simple-scaffold",
|
||||
"version": "2.0.1",
|
||||
"version": "2.2.1",
|
||||
"description": "Generate any file structure - from single components to entire app boilerplates, with a single command.",
|
||||
"homepage": "https: //chenasraf.github.io/simple-scaffold",
|
||||
"repository": "https://github.com/chenasraf/simple-scaffold.git",
|
||||
"author": "Chen Asraf <contact@casraf.dev>",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/chenasraf/simple-scaffold.git"
|
||||
},
|
||||
"author": "Chen Asraf <contact@casraf.dev> (https://casraf.dev)",
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"bin": "cmd.js",
|
||||
"packageManager": "pnpm@8.6.2",
|
||||
"bin": {
|
||||
"simple-scaffold": "cmd.js"
|
||||
},
|
||||
"packageManager": "pnpm@9.0.4",
|
||||
"keywords": [
|
||||
"javascript",
|
||||
"cli",
|
||||
@@ -34,8 +39,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"chalk": "^4.1.2",
|
||||
"date-fns": "^3.3.1",
|
||||
"glob": "^10.3.10",
|
||||
"date-fns": "^3.6.0",
|
||||
"glob": "^10.3.12",
|
||||
"handlebars": "^4.7.8",
|
||||
"massarg": "2.0.0"
|
||||
},
|
||||
@@ -44,18 +49,18 @@
|
||||
"@semantic-release/exec": "^6.0.3",
|
||||
"@semantic-release/git": "^10.0.1",
|
||||
"@semantic-release/release-notes-generator": "^12.1.0",
|
||||
"@types/jest": "^29.5.11",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/mock-fs": "^4.13.4",
|
||||
"@types/node": "^20.11.14",
|
||||
"@types/node": "^20.12.7",
|
||||
"@types/semantic-release": "^20.0.6",
|
||||
"conventional-changelog": "^5.1.0",
|
||||
"conventional-changelog-cli": "^4.1.0",
|
||||
"jest": "^29.7.0",
|
||||
"mock-fs": "^5.2.0",
|
||||
"semantic-release": "^23.0.0",
|
||||
"semantic-release": "^23.0.8",
|
||||
"semantic-release-conventional-commits": "^3.0.0",
|
||||
"ts-jest": "^29.1.2",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "^5.4.5"
|
||||
}
|
||||
}
|
||||
|
||||
7078
pnpm-lock.yaml
generated
@@ -13,37 +13,44 @@ module.exports = {
|
||||
"@semantic-release/npm",
|
||||
{
|
||||
// only update the pkg version on root, don't publish
|
||||
// this is to keep package.json version in sync with the release
|
||||
npmPublish: false,
|
||||
},
|
||||
],
|
||||
// [
|
||||
// '@semantic-release/npm',
|
||||
// {
|
||||
// // only update the pkg version on doc, don't publish
|
||||
// npmPublish: false,
|
||||
// pkgRoot: 'doc',
|
||||
// },
|
||||
// ]
|
||||
[
|
||||
"@semantic-release/npm",
|
||||
{
|
||||
// publish from dist dir instead of root
|
||||
pkgRoot: "dist",
|
||||
},
|
||||
],
|
||||
[
|
||||
"@semantic-release/exec",
|
||||
{
|
||||
publish: "cd ./dist && pnpm pack --pack-destination=../",
|
||||
// pack the dist folder, during publish step (after version was bumped)
|
||||
publishCmd: 'echo "Packing..."; cd ./dist && pnpm pack --pack-destination=../; echo "Done"',
|
||||
},
|
||||
],
|
||||
[
|
||||
"@semantic-release/npm",
|
||||
{
|
||||
// publish from dist dir instead of root
|
||||
// this is the actual uild output
|
||||
pkgRoot: "dist",
|
||||
},
|
||||
],
|
||||
[
|
||||
// Release to GitHub
|
||||
"@semantic-release/github",
|
||||
{
|
||||
assets: ["*.tgz"],
|
||||
},
|
||||
],
|
||||
branch === "master"
|
||||
? [
|
||||
// Update CHANGELOG.md only on master
|
||||
"@semantic-release/changelog",
|
||||
{
|
||||
changelogFile: "CHANGELOG.md",
|
||||
changelogTitle: "# Change Log",
|
||||
},
|
||||
]
|
||||
: undefined,
|
||||
[
|
||||
// Commit the package.json and CHANGELOG.md files to git (if modified)
|
||||
"@semantic-release/git",
|
||||
{
|
||||
assets: ["package.json", "CHANGELOG.md"].filter(Boolean),
|
||||
@@ -56,16 +63,5 @@ module.exports = {
|
||||
// verifyReleaseCmd: 'echo ${nextRelease.version} > .VERSION',
|
||||
// },
|
||||
// ],
|
||||
],
|
||||
}
|
||||
|
||||
if (branch === "master") {
|
||||
const gitIdx = module.exports.plugins.findIndex((plugin) => plugin[0] === "@semantic-release/git")
|
||||
module.exports.plugins.splice(gitIdx, 0, [
|
||||
"@semantic-release/changelog",
|
||||
{
|
||||
changelogFile: "CHANGELOG.md",
|
||||
changelogTitle: "# Change Log",
|
||||
},
|
||||
])
|
||||
].filter(Boolean),
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/** @type {import('simple-scaffold').ScaffoldConfigFile} */
|
||||
// @ts-check
|
||||
/** @type {import('./dist').ScaffoldConfigFile} */
|
||||
module.exports = (conf) => {
|
||||
console.log("Config:", conf)
|
||||
return {
|
||||
@@ -12,5 +13,10 @@ module.exports = (conf) => {
|
||||
output: "examples/test-output/component",
|
||||
data: { property: "myProp", value: "10" },
|
||||
},
|
||||
configs: {
|
||||
templates: ["examples/test-input/**/.*"],
|
||||
output: "examples/test-output/configs",
|
||||
name: "---",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
132
src/cmd.ts
@@ -1,31 +1,45 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import os from "node:os"
|
||||
import { massarg } from "massarg"
|
||||
import chalk from "chalk"
|
||||
import { LogLevel, ScaffoldCmdConfig } from "./types"
|
||||
import { Scaffold } from "./scaffold"
|
||||
import path from "node:path"
|
||||
import fs from "node:fs/promises"
|
||||
import { parseAppendData, parseConfigFile } from "./config"
|
||||
import { massarg } from "massarg"
|
||||
import chalk from "chalk"
|
||||
import { ListCommandCliOptions, LogLevel, ScaffoldCmdConfig } from "./types"
|
||||
import { Scaffold } from "./scaffold"
|
||||
import { getConfigFile, parseAppendData, parseConfigFile } from "./config"
|
||||
import { log } from "./logger"
|
||||
import { MassargCommand } from "massarg/command"
|
||||
import { getUniqueTmpPath as generateUniqueTmpPath } from "./file"
|
||||
|
||||
export async function parseCliArgs(args = process.argv.slice(2)) {
|
||||
const isProjectRoot = Boolean(await fs.stat(path.join(__dirname, "package.json")).catch(() => false))
|
||||
const pkgFile = await fs.readFile(path.resolve(__dirname, isProjectRoot ? "." : "..", "package.json"))
|
||||
const pkg = JSON.parse(pkgFile.toString())
|
||||
const isConfigProvided =
|
||||
args.includes("--config") || args.includes("-c") || args.includes("--git") || args.includes("-g")
|
||||
const isVersionFlag = args.includes("--version") || args.includes("-v")
|
||||
const isConfigFileProvided = args.includes("--config") || args.includes("-c")
|
||||
const isGitProvided = args.includes("--git") || args.includes("-g")
|
||||
const isConfigProvided = isConfigFileProvided || isGitProvided || isVersionFlag
|
||||
|
||||
return massarg<ScaffoldCmdConfig>({
|
||||
name: pkg.name,
|
||||
description: pkg.description,
|
||||
})
|
||||
.main(async (config) => {
|
||||
const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`)
|
||||
const parsed = await parseConfigFile(config, tmpPath)
|
||||
if (config.version) {
|
||||
console.log(pkg.version)
|
||||
return
|
||||
}
|
||||
log(config, LogLevel.info, `Simple Scaffold v${pkg.version}`)
|
||||
const tmpPath = generateUniqueTmpPath()
|
||||
try {
|
||||
return Scaffold(parsed)
|
||||
log(config, LogLevel.debug, "Parsing config file...", config)
|
||||
const parsed = await parseConfigFile(config, tmpPath)
|
||||
await Scaffold(parsed)
|
||||
} catch (e) {
|
||||
const message = "message" in (e as any) ? (e as any).message : e?.toString()
|
||||
log(config, LogLevel.error, message)
|
||||
} finally {
|
||||
log(config, LogLevel.debug, "Cleaning up temporary files...", tmpPath)
|
||||
await fs.rm(tmpPath, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
@@ -34,24 +48,20 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
|
||||
aliases: ["n"],
|
||||
description:
|
||||
"Name to be passed to the generated files. `{{name}}` and other data parameters inside " +
|
||||
"contents and file names will be replaced accordingly. You may omit the `--name` or `-n` for this specific option.",
|
||||
"contents and file names will be replaced accordingly. You may omit the `--name` or `-n` " +
|
||||
"for this specific option.",
|
||||
isDefault: true,
|
||||
required: true,
|
||||
required: !isConfigProvided,
|
||||
})
|
||||
.option({
|
||||
name: "config",
|
||||
aliases: ["c"],
|
||||
description:
|
||||
"Filename to load config from instead of passing arguments to CLI or using a Node.js " +
|
||||
"script. See examples for syntax. This can also work in conjunction with `--git` or `--github` to point " +
|
||||
"to remote files, and with `--key` to denote which key to select from the file.",
|
||||
description: "Filename or directory to load config from",
|
||||
})
|
||||
.option({
|
||||
name: "git",
|
||||
aliases: ["g"],
|
||||
description:
|
||||
"Git URL to load config from instead of passing arguments to CLI or using a Node.js script. See " +
|
||||
"examples for syntax.",
|
||||
description: "Git URL or GitHub path to load a template from.",
|
||||
})
|
||||
.option({
|
||||
name: "key",
|
||||
@@ -126,7 +136,21 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
|
||||
"Determine amount of logs to display. The values are: " +
|
||||
`${chalk.bold`\`none | debug | info | warn | error\``}. ` +
|
||||
"The provided level will display messages of the same level or higher.",
|
||||
parse: (v) => v.toLowerCase(),
|
||||
parse: (v) => {
|
||||
const val = v.toLowerCase()
|
||||
if (!(val in LogLevel)) {
|
||||
throw new Error(`Invalid log level: ${val}, must be one of: ${Object.keys(LogLevel).join(", ")}`)
|
||||
}
|
||||
return val
|
||||
},
|
||||
})
|
||||
.option({
|
||||
name: "before-write",
|
||||
aliases: ["B"],
|
||||
description:
|
||||
"Run a script before writing the files. This can be a command or a path to a" +
|
||||
" file. A temporary file path will be passed to the given command and the command should " +
|
||||
"return a string for the final output.",
|
||||
})
|
||||
.flag({
|
||||
name: "dry-run",
|
||||
@@ -136,6 +160,72 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
|
||||
"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.",
|
||||
})
|
||||
.flag({
|
||||
name: "version",
|
||||
aliases: ["v"],
|
||||
description: "Display version.",
|
||||
})
|
||||
.command(
|
||||
new MassargCommand<ListCommandCliOptions>({
|
||||
name: "list",
|
||||
aliases: ["ls"],
|
||||
description: "List all available templates for a given config. See `list -h` for more information.",
|
||||
run: async (_config) => {
|
||||
const tmpPath = generateUniqueTmpPath()
|
||||
const config = {
|
||||
templates: [],
|
||||
name: "",
|
||||
version: false,
|
||||
output: "",
|
||||
subdir: false,
|
||||
overwrite: false,
|
||||
dryRun: false,
|
||||
..._config,
|
||||
config: _config.config ?? (!_config.git ? process.cwd() : undefined),
|
||||
}
|
||||
try {
|
||||
const file = await getConfigFile(config, tmpPath)
|
||||
console.log(chalk.underline`Available templates:\n`)
|
||||
console.log(Object.keys(file).join("\n"))
|
||||
} catch (e) {
|
||||
const message = "message" in (e as any) ? (e as any).message : e?.toString()
|
||||
log(config, LogLevel.error, message)
|
||||
} finally {
|
||||
log(config, LogLevel.debug, "Cleaning up temporary files...", tmpPath)
|
||||
await fs.rm(tmpPath, { recursive: true, force: true })
|
||||
}
|
||||
},
|
||||
})
|
||||
.option({
|
||||
name: "config",
|
||||
aliases: ["c"],
|
||||
description: "Filename or directory to load config from. Defaults to current working directory.",
|
||||
})
|
||||
.option({
|
||||
name: "git",
|
||||
aliases: ["g"],
|
||||
description: "Git URL or GitHub path to load a template from.",
|
||||
})
|
||||
.option({
|
||||
name: "log-level",
|
||||
aliases: ["l"],
|
||||
defaultValue: LogLevel.none,
|
||||
description:
|
||||
"Determine amount of logs to display. The values are: " +
|
||||
`${chalk.bold`\`none | debug | info | warn | error\``}. ` +
|
||||
"The provided level will display messages of the same level or higher.",
|
||||
parse: (v) => {
|
||||
const val = v.toLowerCase()
|
||||
if (!(val in LogLevel)) {
|
||||
throw new Error(`Invalid log level: ${val}, must be one of: ${Object.keys(LogLevel).join(", ")}`)
|
||||
}
|
||||
return val
|
||||
},
|
||||
})
|
||||
.help({
|
||||
bindOption: true,
|
||||
}),
|
||||
)
|
||||
.example({
|
||||
description: "Usage with config file",
|
||||
input: "simple-scaffold -c scaffold.cmd.js --key component",
|
||||
|
||||
181
src/config.ts
@@ -1,4 +1,5 @@
|
||||
import path from "node:path"
|
||||
import fs from "node:fs/promises"
|
||||
import {
|
||||
ConfigLoadConfig,
|
||||
FileResponse,
|
||||
@@ -9,11 +10,14 @@ import {
|
||||
ScaffoldCmdConfig,
|
||||
ScaffoldConfig,
|
||||
ScaffoldConfigFile,
|
||||
ScaffoldConfigMap,
|
||||
} from "./types"
|
||||
import { handlebarsParse } from "./parser"
|
||||
import { log } from "./logger"
|
||||
import { resolve, wrapNoopResolver } from "./utils"
|
||||
import { getGitConfig } from "./git"
|
||||
import { createDirIfNotExists, getUniqueTmpPath, isDir, pathExists } from "./file"
|
||||
import { exec, spawn } from "node:child_process"
|
||||
|
||||
/** @internal */
|
||||
export function getOptionValueForFile<T>(
|
||||
@@ -48,57 +52,73 @@ function isWrappedWithQuotes(string: string): boolean {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string): Promise<ScaffoldConfig> {
|
||||
let output: ScaffoldConfig = config
|
||||
|
||||
if (config.quiet) {
|
||||
config.logLevel = LogLevel.none
|
||||
}
|
||||
|
||||
export async function getConfigFile(config: ScaffoldCmdConfig, tmpPath: string): Promise<ScaffoldConfigMap> {
|
||||
if (config.git && !config.git.includes("://")) {
|
||||
log(config, LogLevel.info, `Loading config from GitHub ${config.git}`)
|
||||
config.git = githubPartToUrl(config.git)
|
||||
}
|
||||
|
||||
const shouldLoadConfig = config.config || config.git
|
||||
const isGit = Boolean(config.git)
|
||||
const configFilename = config.config
|
||||
const configPath = isGit ? config.git : configFilename
|
||||
|
||||
log(config, LogLevel.info, `Loading config from file ${configFilename}`)
|
||||
|
||||
const configPromise = await (isGit
|
||||
? getRemoteConfig({ git: configPath, config: configFilename, logLevel: config.logLevel, tmpPath })
|
||||
: getLocalConfig({ config: configFilename, logLevel: config.logLevel }))
|
||||
|
||||
// resolve the config
|
||||
let configImport = await resolve(configPromise, config)
|
||||
|
||||
// If the config is a function or promise, return the output
|
||||
if (typeof configImport.default === "function" || configImport.default instanceof Promise) {
|
||||
log(config, LogLevel.debug, "Config is a function or promise, resolving...")
|
||||
configImport = await resolve(configImport.default, config)
|
||||
}
|
||||
return configImport
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string): Promise<ScaffoldConfig> {
|
||||
let output: ScaffoldConfig = { ...config, beforeWrite: undefined }
|
||||
|
||||
if (config.quiet) {
|
||||
config.logLevel = LogLevel.none
|
||||
}
|
||||
|
||||
const shouldLoadConfig = Boolean(config.config || config.git)
|
||||
|
||||
if (shouldLoadConfig) {
|
||||
const isGit = Boolean(config.git)
|
||||
const key = config.key ?? "default"
|
||||
const configFilename = config.config
|
||||
const configPath = isGit ? config.git : configFilename
|
||||
|
||||
log(config, LogLevel.info, `Loading config from ${configFilename} with key ${key}`)
|
||||
|
||||
const configPromise = await (isGit
|
||||
? getRemoteConfig({ git: configPath, config: configFilename, logLevel: config.logLevel, tmpPath })
|
||||
: getLocalConfig({ config: configFilename, logLevel: config.logLevel }))
|
||||
|
||||
// resolve the config
|
||||
let configImport = await resolve(configPromise, config)
|
||||
|
||||
// If the config is a function or promise, return the output
|
||||
if (typeof configImport.default === "function" || configImport.default instanceof Promise) {
|
||||
configImport = await resolve(configImport.default, config)
|
||||
}
|
||||
const configImport = await getConfigFile(config, tmpPath)
|
||||
|
||||
if (!configImport[key]) {
|
||||
throw new Error(`Template "${key}" not found in ${configFilename}`)
|
||||
throw new Error(`Template "${key}" not found in ${config.config}`)
|
||||
}
|
||||
|
||||
const importedKey = configImport[key]
|
||||
const imported = configImport[key]
|
||||
log(config, LogLevel.debug, "Imported result", imported)
|
||||
output = {
|
||||
...config,
|
||||
...importedKey,
|
||||
...imported,
|
||||
beforeWrite: undefined,
|
||||
data: {
|
||||
...(importedKey as any).data,
|
||||
...(imported as any).data,
|
||||
...config.data,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
output.data = { ...output.data, ...config.appendData }
|
||||
delete config.appendData
|
||||
const cmdBeforeWrite = config.beforeWrite ? wrapBeforeWrite(config, config.beforeWrite) : undefined
|
||||
output.beforeWrite = cmdBeforeWrite ?? output.beforeWrite
|
||||
|
||||
if (!output.name) {
|
||||
throw new Error("simple-scaffold: Missing required option: name")
|
||||
}
|
||||
|
||||
log(output, LogLevel.debug, "Parsed config", output)
|
||||
return output
|
||||
}
|
||||
|
||||
@@ -115,8 +135,22 @@ export function githubPartToUrl(part: string): string {
|
||||
export async function getLocalConfig(config: ConfigLoadConfig & Partial<LogConfig>): Promise<ScaffoldConfigFile> {
|
||||
const { config: configFile, ...logConfig } = config as Required<typeof config>
|
||||
|
||||
log(logConfig, LogLevel.info, `Loading config from file ${configFile}`)
|
||||
const absolutePath = path.resolve(process.cwd(), configFile)
|
||||
|
||||
const _isDir = await isDir(absolutePath)
|
||||
|
||||
if (_isDir) {
|
||||
log(logConfig, LogLevel.debug, `Resolving config file from directory ${absolutePath}`)
|
||||
const file = await findConfigFile(absolutePath)
|
||||
const exists = await pathExists(file)
|
||||
if (!exists) {
|
||||
throw new Error(`Could not find config file in directory ${absolutePath}`)
|
||||
}
|
||||
log(logConfig, LogLevel.info, `Loading config from: ${path.resolve(absolutePath, file)}`)
|
||||
return wrapNoopResolver(import(path.resolve(absolutePath, file)))
|
||||
}
|
||||
|
||||
log(logConfig, LogLevel.info, `Loading config from: ${absolutePath}`)
|
||||
return wrapNoopResolver(import(absolutePath))
|
||||
}
|
||||
|
||||
@@ -138,3 +172,88 @@ export async function getRemoteConfig(
|
||||
|
||||
return getGitConfig(url, configFile, tmpPath, logConfig)
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export async function findConfigFile(root: string): Promise<string> {
|
||||
const allowed = ["mjs", "cjs", "js", "json"].reduce((acc, ext) => {
|
||||
acc.push(`scaffold.config.${ext}`)
|
||||
acc.push(`scaffold.${ext}`)
|
||||
return acc
|
||||
}, [] as string[])
|
||||
for (const file of allowed) {
|
||||
const exists = await pathExists(path.resolve(root, file))
|
||||
if (exists) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
throw new Error(`Could not find config file in git repo`)
|
||||
}
|
||||
|
||||
function wrapBeforeWrite(
|
||||
config: LogConfig & Pick<ScaffoldConfig, "dryRun">,
|
||||
beforeWrite: string,
|
||||
): ScaffoldConfig["beforeWrite"] {
|
||||
return async (content, rawContent, outputFile) => {
|
||||
const tmpPath = path.join(getUniqueTmpPath(), path.basename(outputFile))
|
||||
await createDirIfNotExists(path.dirname(tmpPath), config)
|
||||
const ext = path.extname(outputFile)
|
||||
const rawTmpPath = tmpPath.replace(ext, ".raw" + ext)
|
||||
try {
|
||||
log(config, LogLevel.debug, "Parsing beforeWrite command", beforeWrite)
|
||||
let cmd = await prepareBeforeWriteCmd({ beforeWrite, tmpPath, content, rawTmpPath, rawContent })
|
||||
const result = await new Promise<string | undefined>((resolve, reject) => {
|
||||
log(config, LogLevel.debug, "Running parsed beforeWrite command:", cmd)
|
||||
const proc = exec(cmd)
|
||||
proc.stdout!.on("data", (data) => {
|
||||
if (data.trim()) {
|
||||
resolve(data.toString())
|
||||
} else {
|
||||
resolve(undefined)
|
||||
}
|
||||
})
|
||||
proc.stderr!.on("data", (data) => {
|
||||
reject(data.toString())
|
||||
})
|
||||
})
|
||||
return result
|
||||
} catch (e) {
|
||||
log(config, LogLevel.debug, e)
|
||||
log(config, LogLevel.warning, "Error running beforeWrite command, returning original content")
|
||||
return undefined
|
||||
} finally {
|
||||
await fs.rm(tmpPath, { force: true })
|
||||
await fs.rm(rawTmpPath, { force: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function prepareBeforeWriteCmd({
|
||||
beforeWrite,
|
||||
tmpPath,
|
||||
content,
|
||||
rawTmpPath,
|
||||
rawContent,
|
||||
}: {
|
||||
beforeWrite: string
|
||||
tmpPath: string
|
||||
content: Buffer
|
||||
rawTmpPath: string
|
||||
rawContent: Buffer
|
||||
}): Promise<string> {
|
||||
let cmd: string = ""
|
||||
const pathReg = /\{\{\s*path\s*\}\}/gi
|
||||
const rawPathReg = /\{\{\s*rawpath\s*\}\}/gi
|
||||
if (pathReg.test(beforeWrite)) {
|
||||
await fs.writeFile(tmpPath, content)
|
||||
cmd = beforeWrite.replaceAll(pathReg, tmpPath)
|
||||
}
|
||||
if (rawPathReg.test(beforeWrite)) {
|
||||
await fs.writeFile(rawTmpPath, rawContent)
|
||||
cmd = beforeWrite.replaceAll(rawPathReg, rawTmpPath)
|
||||
}
|
||||
if (!cmd) {
|
||||
await fs.writeFile(tmpPath, content)
|
||||
cmd = [beforeWrite, tmpPath].join(" ")
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
18
src/file.ts
@@ -1,7 +1,8 @@
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { F_OK } from "node:constants"
|
||||
import { LogLevel, ScaffoldConfig } from "./types"
|
||||
import fs from "node:fs/promises"
|
||||
import { F_OK } from "node:constants"
|
||||
import { LogConfig, LogLevel, ScaffoldConfig } from "./types"
|
||||
import { glob, hasMagic } from "glob"
|
||||
import { log } from "./logger"
|
||||
import { getOptionValueForFile } from "./config"
|
||||
@@ -10,7 +11,10 @@ import { handleErr } from "./utils"
|
||||
|
||||
const { stat, access, mkdir, readFile, writeFile } = fs
|
||||
|
||||
export async function createDirIfNotExists(dir: string, config: ScaffoldConfig): Promise<void> {
|
||||
export async function createDirIfNotExists(
|
||||
dir: string,
|
||||
config: LogConfig & Pick<ScaffoldConfig, "dryRun">,
|
||||
): Promise<void> {
|
||||
if (config.dryRun) {
|
||||
log(config, LogLevel.info, `Dry Run. Not creating dir ${dir}`)
|
||||
return
|
||||
@@ -142,6 +146,7 @@ export async function copyFileTransformed(
|
||||
if (exists && overwrite) {
|
||||
log(config, LogLevel.info, `File ${outputPath} exists, overwriting`)
|
||||
}
|
||||
log(config, LogLevel.debug, `Processing file ${inputPath}`)
|
||||
const templateBuffer = await readFile(inputPath)
|
||||
const unprocessedOutputContents = handlebarsParse(config, templateBuffer)
|
||||
const finalOutputContents =
|
||||
@@ -149,7 +154,6 @@ export async function copyFileTransformed(
|
||||
|
||||
if (!config.dryRun) {
|
||||
await writeFile(outputPath, finalOutputContents)
|
||||
log(config, LogLevel.info, "Done.")
|
||||
} else {
|
||||
log(config, LogLevel.info, "Dry Run. Output should be:")
|
||||
log(config, LogLevel.info, finalOutputContents.toString())
|
||||
@@ -157,6 +161,7 @@ export async function copyFileTransformed(
|
||||
} else if (exists) {
|
||||
log(config, LogLevel.info, `File ${outputPath} already exists, skipping`)
|
||||
}
|
||||
log(config, LogLevel.info, "Done.")
|
||||
}
|
||||
|
||||
export function getOutputDir(config: ScaffoldConfig, outputPathOpt: string, basePath: string): string {
|
||||
@@ -209,3 +214,8 @@ export async function handleTemplateFile(
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function getUniqueTmpPath(): string {
|
||||
return path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
}
|
||||
|
||||
21
src/git.ts
@@ -1,9 +1,9 @@
|
||||
import path from "node:path"
|
||||
import fs from "node:fs/promises"
|
||||
import { log } from "./logger"
|
||||
import { AsyncResolver, LogConfig, LogLevel, ScaffoldCmdConfig, ScaffoldConfigMap } from "./types"
|
||||
import { spawn } from "node:child_process"
|
||||
import { resolve, wrapNoopResolver } from "./utils"
|
||||
import { findConfigFile } from "./config"
|
||||
|
||||
export async function getGitConfig(
|
||||
url: URL,
|
||||
@@ -60,22 +60,3 @@ export async function loadGitConfig({
|
||||
}
|
||||
return wrapNoopResolver(fixedConfig)
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export async function findConfigFile(root: string): Promise<string> {
|
||||
const allowed = ["mjs", "cjs", "js", "json"].reduce((acc, ext) => {
|
||||
acc.push(`scaffold.config.${ext}`)
|
||||
acc.push(`scaffold.${ext}`)
|
||||
return acc
|
||||
}, [] as string[])
|
||||
for (const file of allowed) {
|
||||
const exists = await fs
|
||||
.stat(path.resolve(root, file))
|
||||
.then(() => true)
|
||||
.catch(() => false)
|
||||
if (exists) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
throw new Error(`Could not find config file in git repo`)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./scaffold"
|
||||
export * from "./types"
|
||||
import Scaffold from "./scaffold"
|
||||
|
||||
export default Scaffold
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import util from "util"
|
||||
import { LogConfig, LogLevel, ScaffoldConfig } from "./types"
|
||||
import chalk from "chalk"
|
||||
|
||||
@@ -30,7 +31,7 @@ export function log(config: LogConfig, level: LogLevel, ...obj: any[]): void {
|
||||
i instanceof Error
|
||||
? chalkFn(i, JSON.stringify(i, undefined, 1), i.stack)
|
||||
: typeof i === "object"
|
||||
? chalkFn(JSON.stringify(i, undefined, 1))
|
||||
? util.inspect(i, { depth: null, colors: true })
|
||||
: chalkFn(i),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -121,7 +121,7 @@ export function handlebarsParse(
|
||||
return Buffer.from(outputContents)
|
||||
} catch (e) {
|
||||
log(config, LogLevel.debug, e)
|
||||
log(config, LogLevel.warning, "Couldn't parse file with handlebars, returning original content")
|
||||
log(config, LogLevel.info, "Couldn't parse file with handlebars, returning original content")
|
||||
return Buffer.from(templateBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +128,7 @@ Scaffold.fromConfig = async function (
|
||||
subdir: false,
|
||||
quiet: false,
|
||||
config: pathOrUrl,
|
||||
version: false,
|
||||
...config,
|
||||
}
|
||||
const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`)
|
||||
|
||||
40
src/types.ts
@@ -257,6 +257,18 @@ export type DefaultHelpers = CaseHelpers | DateHelpers
|
||||
*/
|
||||
export type Helper = HelperDelegate
|
||||
|
||||
/**
|
||||
* The amount of information to log when generating scaffold.
|
||||
* When not `none`, the selected level will be the lowest level included.
|
||||
*
|
||||
* For example, level `info` will include `info`, `warning` and `error`, but not `debug`; and `warning` will only
|
||||
* show `warning` and `error`, but not `info` or `debug`.
|
||||
*
|
||||
* @default `info`
|
||||
*
|
||||
* @category Logging (const)
|
||||
*/
|
||||
|
||||
export const LogLevel = {
|
||||
/** Silent output */
|
||||
none: "none",
|
||||
@@ -276,17 +288,14 @@ export const LogLevel = {
|
||||
|
||||
/**
|
||||
* The amount of information to log when generating scaffold.
|
||||
* When not `None`, the selected level will be the lowest level included.
|
||||
* When not `none`, the selected level will be the lowest level included.
|
||||
*
|
||||
* For example, level `Info` (2) will include `Info`, `Warning` and `Error`, but not `Debug`; and `Warning` will only
|
||||
* show `Warning` and `Error`.
|
||||
* For example, level `info` will include `info`, `warning` and `error`, but not `debug`; and `warning` will only
|
||||
* show `warning` and `error`, but not `info` or `debug`.
|
||||
*
|
||||
* You may use either the number or the name of the level.
|
||||
* For example, `2` or `info` are both valid.
|
||||
* @default `info`
|
||||
*
|
||||
* @default `2 (info)`
|
||||
*
|
||||
* @category Logging
|
||||
* @category Logging (type)
|
||||
*/
|
||||
export type LogLevel = (typeof LogLevel)[keyof typeof LogLevel]
|
||||
|
||||
@@ -324,7 +333,7 @@ export type FileResponse<T> = T | FileResponseHandler<T>
|
||||
* The Scaffold config for CLI
|
||||
* Contains less and more specific options than {@link ScaffoldConfig}
|
||||
*/
|
||||
export interface ScaffoldCmdConfig {
|
||||
export type ScaffoldCmdConfig = {
|
||||
/** The name of the scaffold template to use. */
|
||||
name: string
|
||||
/** The templates to use for generation */
|
||||
@@ -357,6 +366,10 @@ export interface ScaffoldCmdConfig {
|
||||
key?: string
|
||||
/** The git repository to use to fetch the config file */
|
||||
git?: string
|
||||
/** Display version */
|
||||
version: boolean
|
||||
/** Run a script before writing the files. This can be a command or a path to a file. The file contents will be passed to the given command. */
|
||||
beforeWrite?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -369,14 +382,19 @@ export interface ScaffoldCmdConfig {
|
||||
* When no template key is provided to the scaffold command, the "default" template is used.
|
||||
*
|
||||
* @see {@link ScaffoldConfig}
|
||||
*
|
||||
* @category Config
|
||||
*/
|
||||
export type ScaffoldConfigMap = Record<string, ScaffoldConfig>
|
||||
|
||||
/** The scaffold config file is either:
|
||||
/**
|
||||
* The scaffold config file is either:
|
||||
* - A {@link ScaffoldConfigMap} object
|
||||
* - A function that returns a {@link ScaffoldConfigMap} object
|
||||
* - A promise that resolves to a {@link ScaffoldConfigMap} object
|
||||
* - A function that returns a promise that resolves to a {@link ScaffoldConfigMap} object
|
||||
*
|
||||
* @category Config
|
||||
*/
|
||||
export type ScaffoldConfigFile = AsyncResolver<ScaffoldCmdConfig, ScaffoldConfigMap>
|
||||
|
||||
@@ -397,3 +415,5 @@ export type RemoteConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config
|
||||
|
||||
/** @internal */
|
||||
export type MinimalConfig = Pick<ScaffoldCmdConfig, "name" | "key">
|
||||
|
||||
export type ListCommandCliOptions = Pick<ScaffoldCmdConfig, "config" | "git" | "logLevel" | "quiet">
|
||||
|
||||
@@ -6,7 +6,7 @@ import * as config from "../src/config"
|
||||
import { resolve } from "../src/utils"
|
||||
// @ts-ignore
|
||||
import * as configFile from "../scaffold.config"
|
||||
import { findConfigFile } from "../src/git"
|
||||
import { findConfigFile } from "../src/config"
|
||||
|
||||
jest.mock("../src/git", () => {
|
||||
return {
|
||||
@@ -30,6 +30,7 @@ const blankCliConf: ScaffoldCmdConfig = {
|
||||
subdir: false,
|
||||
dryRun: false,
|
||||
quiet: false,
|
||||
version: false,
|
||||
}
|
||||
|
||||
const blankConfig: ScaffoldCmdConfig = {
|
||||
@@ -71,16 +72,18 @@ describe("config", () => {
|
||||
await parseConfigFile(
|
||||
{
|
||||
...blankCliConf,
|
||||
name: "-",
|
||||
},
|
||||
`/tmp/scaffold-config-${Date.now()}`,
|
||||
),
|
||||
).toEqual(blankCliConf)
|
||||
).toEqual({ ...blankCliConf, name: "-" })
|
||||
})
|
||||
describe("appendData", () => {
|
||||
test("appends", async () => {
|
||||
const result = await parseConfigFile(
|
||||
{
|
||||
...blankCliConf,
|
||||
name: "-",
|
||||
appendData: { key: "value" },
|
||||
},
|
||||
`/tmp/scaffold-config-${Date.now()}`,
|
||||
@@ -91,6 +94,7 @@ describe("config", () => {
|
||||
const result = await parseConfigFile(
|
||||
{
|
||||
...blankCliConf,
|
||||
name: "-",
|
||||
data: { num: "123" },
|
||||
appendData: { num: "1234" },
|
||||
},
|
||||
|
||||