Compare commits

..

3 Commits

Author SHA1 Message Date
Chen Asraf
f07affa124 add basename to output config function (fixes #3) 2021-04-19 22:24:21 +03:00
Chen Asraf
ce22a2c34c disable overwriting files + parse JSON for locals 2021-02-28 01:38:51 +02:00
Chen Asraf
7c0c347002 fix: binary files 2021-02-01 15:24:57 +02:00
15 changed files with 189 additions and 96 deletions

View File

@@ -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
@@ -15,39 +17,45 @@ yarn [global] add simple-scaffold
### Command Line Options
Scaffold Generator
```plaintext
Scaffold Generator
Generate scaffolds for your project based on file templates.
Usage: simple-scaffold scaffold-name [options]
Generate scaffolds for your project based on file templates.
Usage: simple-scaffold scaffold-name [options]
Options
-n, --name string Component output name
-t, --templates File[] A glob pattern of template files to load.
A template file may be of any type and extension, and supports Handlebars as
a parsing engine for the file names and contents, so you may customize both
with variables from your configuration.
-o, --output File The output directory to put the new files in. They will attempt to maintain
their regular structure as they are found, if possible.
-l, --locals Key=Value[] A key-value map for the template to use in parsing.
-S, --create-sub-folder Boolean Whether to create a subdirectory with {{Name}} in the output directory.
default=true
-h, --help Display this help message
-n, --name string Component output name
-t, --templates File[] A glob pattern of template files to load.
A template file may be of any type and extension, and supports Handlebars as
a parsing engine for the file names and contents, so you may customize both
with variables from your configuration.
-o, --output File The output directory to put the new files in. They will attempt to maintain
their regular structure as they are found, if possible.
-l, --locals Key=Value A key-value map for the template to use in parsing.
-w, --overwrite Boolean Whether to overwrite files when they are found to already exist. default=true
-S, --create-sub-folder Boolean Whether to create a subdirectory with {{Name}} in the output directory.
default=true
-h, --help Display this help message
```
You can add this as a script in your `package.json`:
```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 --template scaffolds/component/**/* --output src/components --locals 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}}>`, as per the Handlebars formatting rules.
Your context will be pre-populated with the following:
- `{{Name}}`: CapitalizedName of the component
- `{{name}}`: camelCasedName of the component
@@ -55,25 +63,27 @@ Any `locals` you add in the config will populate with their names wrapped in `{{
They are all stringified, so be sure to parse them accordingly by creating a script, if necessary.
### 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'),
name: "component",
templates: [path.join(__dirname, "scaffolds", "component")],
output: path.join(__dirname, "src", "components"),
createSubFolder: true,
locals: {
property: 'value',
}
property: "value",
},
}).run()
```
The exception in the config is that `output`, when used in Node directly, may also be passed a function for each input file to output into a dynamic path:
```javascript
config.output = (filename, basePath) => [basePath, filename].join(path.sep)
```
@@ -81,6 +91,7 @@ config.output = (filename, basePath) => [basePath, filename].join(path.sep)
## Example Scaffold Input
### Input Directory structure
```
- project
- scaffold
@@ -91,6 +102,7 @@ config.output = (filename, basePath) => [basePath, filename].join(path.sep)
```
#### project/scaffold/{{Name}}.js
```js
const React = require('react')
@@ -102,6 +114,7 @@ module.exports = class {{Name}} extends React.Component {
```
### Run Example
```bash
simple-scaffold MyComponent \
-t project/scaffold/**/* \
@@ -110,7 +123,9 @@ simple-scaffold MyComponent \
```
## Example Scaffold Output
#### Directory structure
```
- project
- src
@@ -121,6 +136,7 @@ simple-scaffold MyComponent \
```
With `createSubfolder = false`:
```
- project
- src
@@ -130,12 +146,13 @@ With `createSubfolder = false`:
```
#### 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="my-component">MyComponent Component</div>
}
}
```

87
cmd.ts
View File

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

2
dist/cmd.js vendored

File diff suppressed because one or more lines are too long

2
dist/cmd.js.map vendored

File diff suppressed because one or more lines are too long

5
dist/index.d.ts vendored
View File

@@ -7,9 +7,12 @@ declare namespace IScaffold {
export interface Config {
name?: string
templates: string[]
output: string | ((path: string, base: string) => string)
output:
| string
| ((fullPath: string, basedir: string, basename: string) => string)
locals?: Locals
createSubfolder?: boolean
overwrite?: boolean | ((path: string) => boolean)
}
export interface Locals {

2
dist/index.js vendored
View File

@@ -1,2 +1,2 @@
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.library=e():t.library=e()}(global,(function(){return(()=>{"use strict";var t={493:function(t,e,o){var i=this&&this.__assign||function(){return(i=Object.assign||function(t){for(var e,o=1,i=arguments.length;o<i;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0});var r=o(747),n=o(622),s=o(878),c=o(778),a=function(){function t(t){this.locals={};var e={name:"scaffold",templates:[],output:process.cwd(),createSubfolder:!0};this.config=i(i({},e),t);var o={Name:this.config.name[0].toUpperCase()+this.config.name.slice(1),name:this.config.name[0].toLowerCase()+this.config.name.slice(1)};this.locals=i(i({},o),t.locals)}return t.prototype.parseLocals=function(t){return c.compile(t,{noEscape:!0})(this.locals)},t.prototype.fileList=function(t){for(var e=[],o=0,i=t;o<i.length;o++){var r=i[o],c=s.sync(r,{dot:!0}).map((function(t){return"/"==t[0]?t:n.join(process.cwd(),t)})),a=r.indexOf("*"),l=r;a>=0&&(l=r.slice(0,a-1));for(var f=0,u=c;f<u.length;f++){var p=u[f];e.push({base:l,file:p})}}return e},t.prototype.getFileContents=function(t){return console.log(r.readFileSync(t)),r.readFileSync(t).toString()},t.prototype.getOutputPath=function(t,e){var o;if("function"==typeof this.config.output)o=this.config.output(t,e);else{var i=this.config.output+(this.config.createSubfolder?"/"+this.config.name+"/":"/"),r=t.indexOf(e),n=t;r>=0&&(n=t.slice(r+e.length+1)),o=i+n}return this.parseLocals(o)},t.prototype.writeFile=function(t,e){var o=n.dirname(t);this.writeDirectory(o,t),console.info("Writing file:",t),r.writeFile(t,e,{encoding:"utf-8"},(function(t){if(t)throw t}))},t.prototype.run=function(){console.log("Generating scaffold: "+this.config.name+"...");var t,e=this.fileList(this.config.templates),o=0;console.log("Template files:",e);for(var i=0,n=e;i<n.length;i++){t=n[i];var s=void 0,c=void 0,a=void 0,l=void 0,f=void 0;try{if(o++,l=t.file,f=t.base,s=this.getOutputPath(l,f),r.lstatSync(l).isDirectory()){this.writeDirectory(s,l);continue}c=this.getFileContents(l),a=this.parseLocals(c),console.info("Writing:",{file:l,base:f,outputPath:s,outputContents:a.replace("\n","\\n")}),this.writeFile(s,a)}catch(t){throw console.error("Error while processing file:",{file:l,base:f,contents:c,outputPath:s,outputContents:a}),t}}if(!o)throw new Error("No files to scaffold!");console.log("Done")},t.prototype.writeDirectory=function(t,e){var o=n.dirname(t);r.existsSync(o)||this.writeDirectory(o,t),r.existsSync(t)||(console.info("Creating directory:",{file:e,outputPath:t}),r.mkdirSync(t))},t}();e.default=a},747:t=>{t.exports=require("fs")},878:t=>{t.exports=require("glob")},778:t=>{t.exports=require("handlebars")},622:t=>{t.exports=require("path")}},e={};return function o(i){if(e[i])return e[i].exports;var r=e[i]={exports:{}};return t[i].call(r.exports,r,r.exports,o),r.exports}(493)})()}));
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.library=e():t.library=e()}(global,(function(){return(()=>{"use strict";var t={493:function(t,e,o){var i=this&&this.__assign||function(){return(i=Object.assign||function(t){for(var e,o=1,i=arguments.length;o<i;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0});var r=o(747),n=o(622),s=o(878),c=o(778),a=function(){function t(t){this.locals={};var e={name:"scaffold",templates:[],output:process.cwd(),createSubfolder:!0,overwrite:!0};this.config=i(i({},e),t);var o={Name:this.config.name[0].toUpperCase()+this.config.name.slice(1),name:this.config.name[0].toLowerCase()+this.config.name.slice(1)};this.locals=i(i({},o),t.locals)}return t.prototype.parseLocals=function(t){try{return c.compile(t,{noEscape:!0})(this.locals)}catch(e){return console.warn("Problem using Handlebars, returning unmodified content"),t}},t.prototype.fileList=function(t){for(var e=[],o=0,i=t;o<i.length;o++){var r=i[o],c=s.sync(r,{dot:!0}).map((function(t){return"/"==t[0]?t:n.join(process.cwd(),t)})),a=r.indexOf("*"),l=r;a>=0&&(l=r.slice(0,a-1));for(var f=0,u=c;f<u.length;f++){var p=u[f];e.push({base:l,file:p})}}return e},t.prototype.getFileContents=function(t){return console.log(r.readFileSync(t)),r.readFileSync(t).toString()},t.prototype.getOutputPath=function(t,e){var o;if("function"==typeof this.config.output)o=this.config.output(t,e,n.basename(t));else{var i=this.config.output+(this.config.createSubfolder?"/"+this.config.name+"/":"/"),r=t.indexOf(e),s=t;r>=0&&(s=t!==e?t.slice(r+e.length+1):n.basename(t)),o=i+s}return this.parseLocals(o)},t.prototype.writeFile=function(t,e){var o=n.dirname(t);this.writeDirectory(o,t),r.writeFile(t,e,{encoding:"utf-8"},(function(t){if(t)throw t}))},t.prototype.shouldWriteFile=function(t){var e,o,i="boolean"==typeof this.config.overwrite?this.config.overwrite:null===(o=(e=this.config).overwrite)||void 0===o?void 0:o.call(e,t);return!r.existsSync(t)||!1!==i},t.prototype.run=function(){console.log("Generating scaffold: "+this.config.name+"...");var t,e=this.fileList(this.config.templates),o=0;console.log("Template files:",e);for(var i=0,n=e;i<n.length;i++){t=n[i];var s=void 0,c=void 0,a=void 0,l=void 0,f=void 0;try{if(o++,l=t.file,f=t.base,s=this.getOutputPath(l,f),r.lstatSync(l).isDirectory()){this.writeDirectory(s,l);continue}c=this.getFileContents(l),a=this.parseLocals(c),this.shouldWriteFile(s)?(console.info("Writing:",{file:l,base:f,outputPath:s,outputContents:a.replace("\n","\\n")}),this.writeFile(s,a)):console.log("Skipping file "+s)}catch(t){throw console.error("Error while processing file:",{file:l,base:f,contents:c,outputPath:s,outputContents:a}),t}}if(!o)throw new Error("No files to scaffold!");console.log("Done")},t.prototype.writeDirectory=function(t,e){var o=n.dirname(t);r.existsSync(o)||this.writeDirectory(o,t),r.existsSync(t)||(console.info("Creating directory:",{file:e,outputPath:t}),r.mkdirSync(t))},t}();e.default=a},747:t=>{t.exports=require("fs")},878:t=>{t.exports=require("glob")},778:t=>{t.exports=require("handlebars")},622:t=>{t.exports=require("path")}},e={};return function o(i){if(e[i])return e[i].exports;var r=e[i]={exports:{}};return t[i].call(r.exports,r,r.exports,o),r.exports}(493)})()}));
//# sourceMappingURL=index.js.map

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

1
dist/scaffold.d.ts vendored
View File

@@ -8,6 +8,7 @@ declare class SimpleScaffold {
private getFileContents;
private getOutputPath;
private writeFile;
private shouldWriteFile;
run(): void;
private writeDirectory;
}

2
dist/test.js vendored
View File

@@ -1,2 +1,2 @@
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.library=e():t.library=e()}(global,(function(){return(()=>{"use strict";var t={493:function(t,e,o){var r=this&&this.__assign||function(){return(r=Object.assign||function(t){for(var e,o=1,r=arguments.length;o<r;o++)for(var i in e=arguments[o])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0});var i=o(747),n=o(622),s=o(878),a=o(778),c=function(){function t(t){this.locals={};var e={name:"scaffold",templates:[],output:process.cwd(),createSubfolder:!0};this.config=r(r({},e),t);var o={Name:this.config.name[0].toUpperCase()+this.config.name.slice(1),name:this.config.name[0].toLowerCase()+this.config.name.slice(1)};this.locals=r(r({},o),t.locals)}return t.prototype.parseLocals=function(t){return a.compile(t,{noEscape:!0})(this.locals)},t.prototype.fileList=function(t){for(var e=[],o=0,r=t;o<r.length;o++){var i=r[o],a=s.sync(i,{dot:!0}).map((function(t){return"/"==t[0]?t:n.join(process.cwd(),t)})),c=i.indexOf("*"),l=i;c>=0&&(l=i.slice(0,c-1));for(var u=0,p=a;u<p.length;u++){var f=p[u];e.push({base:l,file:f})}}return e},t.prototype.getFileContents=function(t){return console.log(i.readFileSync(t)),i.readFileSync(t).toString()},t.prototype.getOutputPath=function(t,e){var o;if("function"==typeof this.config.output)o=this.config.output(t,e);else{var r=this.config.output+(this.config.createSubfolder?"/"+this.config.name+"/":"/"),i=t.indexOf(e),n=t;i>=0&&(n=t.slice(i+e.length+1)),o=r+n}return this.parseLocals(o)},t.prototype.writeFile=function(t,e){var o=n.dirname(t);this.writeDirectory(o,t),console.info("Writing file:",t),i.writeFile(t,e,{encoding:"utf-8"},(function(t){if(t)throw t}))},t.prototype.run=function(){console.log("Generating scaffold: "+this.config.name+"...");var t,e=this.fileList(this.config.templates),o=0;console.log("Template files:",e);for(var r=0,n=e;r<n.length;r++){t=n[r];var s=void 0,a=void 0,c=void 0,l=void 0,u=void 0;try{if(o++,l=t.file,u=t.base,s=this.getOutputPath(l,u),i.lstatSync(l).isDirectory()){this.writeDirectory(s,l);continue}a=this.getFileContents(l),c=this.parseLocals(a),console.info("Writing:",{file:l,base:u,outputPath:s,outputContents:c.replace("\n","\\n")}),this.writeFile(s,c)}catch(t){throw console.error("Error while processing file:",{file:l,base:u,contents:a,outputPath:s,outputContents:c}),t}}if(!o)throw new Error("No files to scaffold!");console.log("Done")},t.prototype.writeDirectory=function(t,e){var o=n.dirname(t);i.existsSync(o)||this.writeDirectory(o,t),i.existsSync(t)||(console.info("Creating directory:",{file:e,outputPath:t}),i.mkdirSync(t))},t}();e.default=c},743:(t,e,o)=>{Object.defineProperty(e,"__esModule",{value:!0});var r=o(493),i=o(622).join(process.cwd(),"examples");new r.default({templates:[i+"/test-input/Component/**/*"],output:i+"/test-output/no-create-subpath",createSubfolder:!1,locals:{property:"myProp",value:'"value"'}}).run(),new r.default({templates:[i+"/test-input/Component/**/*"],output:i+"/test-output",locals:{property:"myProp",value:'"value"'}}).run()},747:t=>{t.exports=require("fs")},878:t=>{t.exports=require("glob")},778:t=>{t.exports=require("handlebars")},622:t=>{t.exports=require("path")}},e={};return function o(r){if(e[r])return e[r].exports;var i=e[r]={exports:{}};return t[r].call(i.exports,i,i.exports,o),i.exports}(743)})()}));
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.library=t():e.library=t()}(global,(function(){return(()=>{"use strict";var e={493:function(e,t,o){var r=this&&this.__assign||function(){return(r=Object.assign||function(e){for(var t,o=1,r=arguments.length;o<r;o++)for(var n in t=arguments[o])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e}).apply(this,arguments)};Object.defineProperty(t,"__esModule",{value:!0});var n=o(747),i=o(622),s=o(878),a=o(778),l=function(){function e(e){this.locals={};var t={name:"scaffold",templates:[],output:process.cwd(),createSubfolder:!0,overwrite:!0};this.config=r(r({},t),e);var o={Name:this.config.name[0].toUpperCase()+this.config.name.slice(1),name:this.config.name[0].toLowerCase()+this.config.name.slice(1)};this.locals=r(r({},o),e.locals)}return e.prototype.parseLocals=function(e){try{return a.compile(e,{noEscape:!0})(this.locals)}catch(t){return console.warn("Problem using Handlebars, returning unmodified content"),e}},e.prototype.fileList=function(e){for(var t=[],o=0,r=e;o<r.length;o++){var n=r[o],a=s.sync(n,{dot:!0}).map((function(e){return"/"==e[0]?e:i.join(process.cwd(),e)})),l=n.indexOf("*"),c=n;l>=0&&(c=n.slice(0,l-1));for(var u=0,p=a;u<p.length;u++){var f=p[u];t.push({base:c,file:f})}}return t},e.prototype.getFileContents=function(e){return console.log(n.readFileSync(e)),n.readFileSync(e).toString()},e.prototype.getOutputPath=function(e,t){var o;if("function"==typeof this.config.output)o=this.config.output(e,t,i.basename(e));else{var r=this.config.output+(this.config.createSubfolder?"/"+this.config.name+"/":"/"),n=e.indexOf(t),s=e;n>=0&&(s=e!==t?e.slice(n+t.length+1):i.basename(e)),o=r+s}return this.parseLocals(o)},e.prototype.writeFile=function(e,t){var o=i.dirname(e);this.writeDirectory(o,e),n.writeFile(e,t,{encoding:"utf-8"},(function(e){if(e)throw e}))},e.prototype.shouldWriteFile=function(e){var t,o,r="boolean"==typeof this.config.overwrite?this.config.overwrite:null===(o=(t=this.config).overwrite)||void 0===o?void 0:o.call(t,e);return!n.existsSync(e)||!1!==r},e.prototype.run=function(){console.log("Generating scaffold: "+this.config.name+"...");var e,t=this.fileList(this.config.templates),o=0;console.log("Template files:",t);for(var r=0,i=t;r<i.length;r++){e=i[r];var s=void 0,a=void 0,l=void 0,c=void 0,u=void 0;try{if(o++,c=e.file,u=e.base,s=this.getOutputPath(c,u),n.lstatSync(c).isDirectory()){this.writeDirectory(s,c);continue}a=this.getFileContents(c),l=this.parseLocals(a),this.shouldWriteFile(s)?(console.info("Writing:",{file:c,base:u,outputPath:s,outputContents:l.replace("\n","\\n")}),this.writeFile(s,l)):console.log("Skipping file "+s)}catch(e){throw console.error("Error while processing file:",{file:c,base:u,contents:a,outputPath:s,outputContents:l}),e}}if(!o)throw new Error("No files to scaffold!");console.log("Done")},e.prototype.writeDirectory=function(e,t){var o=i.dirname(e);n.existsSync(o)||this.writeDirectory(o,e),n.existsSync(e)||(console.info("Creating directory:",{file:t,outputPath:e}),n.mkdirSync(e))},e}();t.default=l},743:(e,t,o)=>{Object.defineProperty(t,"__esModule",{value:!0});var r=o(493),n=o(622).join(process.cwd(),"examples");new r.default({templates:[n+"/test-input/Component/**/*"],output:n+"/test-output/no-create-subpath",createSubfolder:!1,locals:{property:"myProp",value:'"value"'}}).run(),new r.default({templates:[n+"/test-input/Component/**/*"],output:n+"/test-output",locals:{property:"myProp",value:'"value"'}}).run(),new r.default({templates:[n+"/test-input/Component/**/*"],output:function(e,t,o){return console.log({file:e,basedir:t,basename:o}),e},locals:{property:"myProp",value:'"value"'}}).run()},747:e=>{e.exports=require("fs")},878:e=>{e.exports=require("glob")},778:e=>{e.exports=require("handlebars")},622:e=>{e.exports=require("path")}},t={};return function o(r){if(t[r])return t[r].exports;var n=t[r]={exports:{}};return e[r].call(n.exports,n,n.exports,o),n.exports}(743)})()}));
//# sourceMappingURL=test.js.map

2
dist/test.js.map vendored

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,19 @@
import * as React from 'react'
import * as css from './Scaffold.css'
class Scaffold extends React.Component<any> {
private myProp
constructor(props: any) {
super(props)
this.myProp = "value"
}
public render() {
return (
<div className={ css.Scaffold } />
)
}
}
export default Scaffold

5
index.d.ts vendored
View File

@@ -7,9 +7,12 @@ declare namespace IScaffold {
export interface Config {
name?: string
templates: string[]
output: string | ((path: string, base: string) => string)
output:
| string
| ((fullPath: string, basedir: string, basename: string) => string)
locals?: Locals
createSubfolder?: boolean
overwrite?: boolean | ((path: string) => boolean)
}
export interface Locals {

View File

@@ -1,6 +1,6 @@
{
"name": "simple-scaffold",
"version": "0.6.0",
"version": "0.7.1",
"description": "Create files based on templates",
"repository": "https://github.com/chenasraf/simple-scaffold.git",
"author": "Chen Asraf <inbox@casraf.com>",

View File

@@ -14,11 +14,13 @@ class SimpleScaffold {
templates: [],
output: process.cwd(),
createSubfolder: true,
overwrite: true,
}
this.config = { ...DefaultConfig, ...config }
const DefaultLocals = {
// TODO improve
Name: this.config.name![0].toUpperCase() + this.config.name!.slice(1),
name: this.config.name![0].toLowerCase() + this.config.name!.slice(1),
}
@@ -27,10 +29,15 @@ class SimpleScaffold {
}
private parseLocals(text: string): string {
const template = handlebars.compile(text, {
noEscape: true,
})
return template(this.locals)
try {
const template = handlebars.compile(text, {
noEscape: true,
})
return template(this.locals)
} catch (e) {
console.warn("Problem using Handlebars, returning unmodified content")
return text
}
}
private fileList(input: string[]): IScaffold.FileRepr[] {
@@ -60,7 +67,7 @@ class SimpleScaffold {
let out: string
if (typeof this.config.output === "function") {
out = this.config.output(file, basePath)
out = this.config.output(file, basePath, path.basename(file))
} else {
const outputDir =
this.config.output +
@@ -68,7 +75,11 @@ class SimpleScaffold {
const idx = file.indexOf(basePath)
let relativeFilePath = file
if (idx >= 0) {
relativeFilePath = file.slice(idx + basePath.length + 1)
if (file !== basePath) {
relativeFilePath = file.slice(idx + basePath.length + 1)
} else {
relativeFilePath = path.basename(file)
}
}
out = outputDir + relativeFilePath
}
@@ -79,7 +90,6 @@ class SimpleScaffold {
private writeFile(filePath: string, fileContents: string): void {
const baseDir = path.dirname(filePath)
this.writeDirectory(baseDir, filePath)
console.info("Writing file:", filePath)
fs.writeFile(filePath, fileContents, { encoding: "utf-8" }, (err) => {
if (err) {
throw err
@@ -87,6 +97,16 @@ class SimpleScaffold {
})
}
private shouldWriteFile(filePath: string) {
const overwrite =
typeof this.config.overwrite === "boolean"
? this.config.overwrite
: this.config.overwrite?.(filePath)
const exists = fs.existsSync(filePath)
return !exists || overwrite !== false
}
public run(): void {
console.log(`Generating scaffold: ${this.config.name}...`)
const templates = this.fileList(this.config.templates)
@@ -108,14 +128,17 @@ class SimpleScaffold {
}
contents = this.getFileContents(file)
outputContents = this.parseLocals(contents)
console.info("Writing:", {
file,
base,
outputPath,
outputContents: outputContents.replace("\n", "\\n"),
})
this.writeFile(outputPath, outputContents)
if (this.shouldWriteFile(outputPath)) {
console.info("Writing:", {
file,
base,
outputPath,
outputContents: outputContents.replace("\n", "\\n"),
})
this.writeFile(outputPath, outputContents)
} else {
console.log(`Skipping file ${outputPath}`)
}
} catch (e) {
console.error("Error while processing file:", {
file,

38
test.ts
View File

@@ -1,23 +1,35 @@
import SimpleScaffold from './scaffold'
import * as path from 'path'
import SimpleScaffold from "./scaffold"
import * as path from "path"
const templateDir = path.join(process.cwd(), 'examples')
const templateDir = path.join(process.cwd(), "examples")
new SimpleScaffold({
templates: [templateDir + '/test-input/Component/**/*'],
output: templateDir + '/test-output/no-create-subpath',
templates: [templateDir + "/test-input/Component/**/*"],
output: templateDir + "/test-output/no-create-subpath",
createSubfolder: false,
locals: {
property: 'myProp',
value: '"value"'
}
property: "myProp",
value: '"value"',
},
}).run()
new SimpleScaffold({
templates: [templateDir + '/test-input/Component/**/*'],
output: templateDir + '/test-output',
templates: [templateDir + "/test-input/Component/**/*"],
output: templateDir + "/test-output",
locals: {
property: 'myProp',
value: '"value"'
}
property: "myProp",
value: '"value"',
},
}).run()
new SimpleScaffold({
templates: [templateDir + "/test-input/Component/**/*"],
output: (file, basedir, basename) => {
console.log({ file, basedir, basename })
return file
},
locals: {
property: "myProp",
value: '"value"',
},
}).run()