Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Build Documentation

on:
pull_request:
branches: [master, develop]
jobs:
docs:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[skip docs]')"
steps:
- uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ examples/test-output/**/*
dist/
.DS_Store
tmp/
docs/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ simple-scaffold MyComponent \

#### Contents of `project/scaffold/{{Name}}.jsx`

```typescriptreact
```typescript
import React from 'react'

export default {{camelCase name}}: React.FC = (props) => {
Expand Down Expand Up @@ -345,7 +345,7 @@ With `createSubFolder = false`:

#### Contents of `project/scaffold/MyComponent/MyComponent.jsx`

```typescriptreact
```typescript
import React from 'react'

export default MyComponent: React.FC = (props) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simple-scaffold",
"version": "1.1.0",
"description": "Create files based on templates",
"description": "A simple command to generate any file structure, from single components to entire app boilerplates.",
"repository": "https://github.com/chenasraf/simple-scaffold.git",
"author": "Chen Asraf <inbox@casraf.com>",
"license": "MIT",
Expand Down Expand Up @@ -48,6 +48,7 @@
"rimraf": "^3.0.2",
"ts-jest": "^27.0.3",
"ts-node": "^10.1.0",
"typedoc": "^0.22.15",
"typescript": "^4.3.5"
}
}
68 changes: 36 additions & 32 deletions src/scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @module
* Simple Scaffold
*
* See [readme](README.md)
*/
import path from "path"

import {
Expand Down Expand Up @@ -31,46 +37,45 @@ import { LogLevel, ScaffoldConfig } from "./types"
* The contents and names will be replaced with the transformed values so you can use your original structure as a
* boilerplate for other projects, components, modules, or even single files.
*
* The files will maintain their structure, starting from the directory containing the template (or the template itself
* if it is already a directory), and will output from that directory into the directory defined by `config.output`.
*
* #### Helpers
* Helpers are functions you can use to transform your `{{ var }}` contents into other values without having to
* pre-define the data and use a duplicated key. Common cases are transforming name-case format
* (e.g. `MyName` &rarr; `my_name`), so these have been provided as defaults:
*
* | 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 |
* | upperCase | `{{ upperCase name }}` | MYNAME |
* | lowerCase | `{{ lowerCase name }}` | myname |
* pre-define the data and use a duplicated key.
*
* Any functions you provide in `helpers` option will also be available to you to make custom formatting as you see fit
* (for example, formatting a date)
*
* For available default values, see {@link DefaultHelperKeys}.
*
* @param {ScaffoldConfig} config The main configuration object
*
* @see {@link DefaultHelperKeys}
*
* @category Main
*/
export async function Scaffold({ ...options }: ScaffoldConfig): Promise<void> {
options.output ??= process.cwd()
export async function Scaffold(config: ScaffoldConfig): Promise<void> {
config.output ??= process.cwd()

registerHelpers(options)
registerHelpers(config)
try {
options.data = { name: options.name, Name: pascalCase(options.name), ...options.data }
logInitStep(options)
for (let _template of options.templates) {
config.data = { name: config.name, Name: pascalCase(config.name), ...config.data }
logInitStep(config)
for (let _template of config.templates) {
try {
const { nonGlobTemplate, origTemplate, isDirOrGlob, isGlob, template } = await getTemplateGlobInfo(
options,
config,
_template
)
const files = await getFileList(options, template)
const files = await getFileList(config, template)
for (const inputFilePath of files) {
if (await isDir(inputFilePath)) {
continue
}
const relPath = makeRelativePath(path.dirname(removeGlob(inputFilePath).replace(nonGlobTemplate, "")))
const basePath = getBasePath(relPath)
logInputFile(options, {
logInputFile(config, {
origTemplate,
relPath,
template,
Expand All @@ -80,7 +85,7 @@ export async function Scaffold({ ...options }: ScaffoldConfig): Promise<void> {
isDirOrGlob,
isGlob,
})
await handleTemplateFile(options, options.data, {
await handleTemplateFile(config, {
templatePath: inputFilePath,
basePath,
})
Expand All @@ -90,25 +95,24 @@ export async function Scaffold({ ...options }: ScaffoldConfig): Promise<void> {
}
}
} catch (e: any) {
log(options, LogLevel.Error, e)
log(config, LogLevel.Error, e)
throw e
}
}
async function handleTemplateFile(
options: ScaffoldConfig,
data: Record<string, string>,
config: ScaffoldConfig,
{ templatePath, basePath }: { templatePath: string; basePath: string }
): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
const { inputPath, outputPathOpt, outputDir, outputPath, exists } = await getTemplateFileInfo(options, data, {
const { inputPath, outputPathOpt, outputDir, outputPath, exists } = await getTemplateFileInfo(config, {
templatePath,
basePath,
})
const overwrite = getOptionValueForFile(options, inputPath, options.overwrite ?? false)
const overwrite = getOptionValueForFile(config, inputPath, config.overwrite ?? false)

log(
options,
config,
LogLevel.Debug,
`\nParsing ${templatePath}`,
`\nBase path: ${basePath}`,
Expand All @@ -119,10 +123,10 @@ async function handleTemplateFile(
`\n`
)

await createDirIfNotExists(path.dirname(outputPath), options)
await createDirIfNotExists(path.dirname(outputPath), config)

log(options, LogLevel.Info, `Writing to ${outputPath}`)
await copyFileTransformed(options, { exists, overwrite, outputPath, inputPath })
log(config, LogLevel.Info, `Writing to ${outputPath}`)
await copyFileTransformed(config, { exists, overwrite, outputPath, inputPath })
resolve()
} catch (e: any) {
handleErr(e)
Expand Down
Loading