added --quiet flag

This commit is contained in:
Chen Asraf
2021-09-26 11:28:38 +03:00
parent cd25b04886
commit 7f98d469a3
13 changed files with 92 additions and 37 deletions

View File

@@ -25,18 +25,20 @@ Scaffold Generator
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.
-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
-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 JSON string A JSON string for the template to use in parsing.
-w, --overwrite Boolean Whether to overwrite files when they are found to already exist. Default=true
-q, --quiet Boolean When set to true, logs will not output (including warnings and errors).
Default=false
-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`:
@@ -94,9 +96,9 @@ config.output = (fullPath, baseDir, baseName) => {
## Example Scaffold Input
### Input Directory structure
### Input directory structure
```
```plaintext
- project
- scaffold
- {{Name}}.js
@@ -128,9 +130,9 @@ simple-scaffold MyComponent \
## Example Scaffold Output
#### Directory structure
### Output directory structure
```
```plaintext
- project
- src
- components
@@ -141,7 +143,7 @@ simple-scaffold MyComponent \
With `createSubfolder = false`:
```
```plaintext
- project
- src
- components

31
cmd.ts
View File

@@ -60,17 +60,26 @@ const defs: Def[] = [
{
name: "overwrite",
alias: "w",
description: `Whether to overwrite files when they are found to already exist. {bold default=true}`,
description: `Whether to overwrite files when they are found to already exist. {bold Default=true}`,
type: booleanParser,
typeLabel: "{underline Boolean}",
defaultValue: true,
},
{
name: "quiet",
alias: "q",
description:
"When set to {bold true}, logs will not output (including warnings and errors). {bold Default=false}",
type: booleanParser,
typeLabel: "{underline Boolean}",
defaultValue: false,
},
{
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}",
"Whether to create a subdirectory with \\{\\{Name\\}\\} in the {underline output} directory. {bold Default=true}",
type: booleanParser,
defaultValue: true,
},
@@ -82,7 +91,13 @@ const defs: Def[] = [
},
]
const args = cliArgs(defs, { camelCase: true })
const args = cliArgs(defs, { camelCase: true }) as Omit<
IScaffold.Config,
"createSubFolder"
> & {
help: boolean
createSubFolder: boolean
}
const help = [
{
@@ -96,12 +111,19 @@ if (args.createSubFolder === null) {
args.createSubFolder = true
}
if (args.quiet === null) {
args.quiet = true
}
if (args.help || !args.name) {
console.log(cliUsage(help))
process.exit(0)
}
console.info("Config:", args)
if (!args.quiet) {
console.info("Config:", args)
}
new SimpleScaffold({
name: args.name,
templates: args.templates,
@@ -109,4 +131,5 @@ new SimpleScaffold({
locals: args.locals,
createSubfolder: args.createSubFolder,
overwrite: args.overwrite,
quiet: args.quiet,
}).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

1
dist/index.d.ts vendored
View File

@@ -13,6 +13,7 @@ declare namespace IScaffold {
locals?: Locals
createSubfolder?: boolean
overwrite?: boolean | ((path: string) => boolean)
quiet?: 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,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)})()}));
!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)},i=this&&this.__spreadArrays||function(){for(var t=0,e=0,o=arguments.length;e<o;e++)t+=arguments[e].length;var r=Array(t),i=0;for(e=0;e<o;e++)for(var n=arguments[e],s=0,a=n.length;s<a;s++,i++)r[i]=n[s];return r};Object.defineProperty(e,"__esModule",{value:!0});var n=o(747),s=o(622),a=o(878),l=o(778),f=function(){function t(t){this.locals={};var e={name:"scaffold",templates:[],output:process.cwd(),createSubfolder:!0,overwrite:!0,quiet:!1};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){try{return l.compile(t,{noEscape:!0})(this.locals)}catch(e){return this.warn("Problem using Handlebars, returning unmodified content"),t}},t.prototype.fileList=function(t){for(var e=[],o=0,r=t;o<r.length;o++){var i=r[o],n=a.sync(i,{dot:!0}).map((function(t){return"/"==t[0]?t:s.join(process.cwd(),t)})),l=i.indexOf("*"),f=i;l>=0&&(f=i.slice(0,l-1));for(var c=0,p=n;c<p.length;c++){var u=p[c];e.push({base:f,file:u})}}return e},t.prototype.getFileContents=function(t){return this.log(n.readFileSync(t)),n.readFileSync(t).toString()},t.prototype.getOutputPath=function(t,e){var o;if("function"==typeof this.config.output)o=this.config.output(t,e,s.basename(t));else{var r=this.config.output+(this.config.createSubfolder?"/"+this.config.name+"/":"/"),i=t.indexOf(e),n=t;i>=0&&(n=t!==e?t.slice(i+e.length+1):s.basename(t)),o=r+n}return this.parseLocals(o)},t.prototype.writeFile=function(t,e){var o=s.dirname(t);this.writeDirectory(o,t),n.writeFile(t,e,{encoding:"utf-8"},(function(t){if(t)throw t}))},t.prototype.shouldWriteFile=function(t){var e,o,r="boolean"==typeof this.config.overwrite?this.config.overwrite:null===(o=(e=this.config).overwrite)||void 0===o?void 0:o.call(e,t);return!n.existsSync(t)||!1!==r},t.prototype.run=function(){this.log("Generating scaffold: "+this.config.name+"...");var t,e=this.fileList(this.config.templates),o=0;this.log("Template files:",e);for(var r=0,i=e;r<i.length;r++){t=i[r];var s=void 0,a=void 0,l=void 0,f=void 0,c=void 0;try{if(o++,f=t.file,c=t.base,s=this.getOutputPath(f,c),n.lstatSync(f).isDirectory()){this.writeDirectory(s,f);continue}a=this.getFileContents(f),l=this.parseLocals(a),this.shouldWriteFile(s)?(this.info("Writing:",{file:f,base:c,outputPath:s,outputContents:l.replace("\n","\\n")}),this.writeFile(s,l)):this.log("Skipping file "+s)}catch(t){throw this.error("Error while processing file:",{file:f,base:c,contents:a,outputPath:s,outputContents:l}),t}}if(!o)throw new Error("No files to scaffold!");this.log("Done")},t.prototype.writeDirectory=function(t,e){var o=s.dirname(t);n.existsSync(o)||this.writeDirectory(o,t),n.existsSync(t)||(this.info("Creating directory:",{file:e,outputPath:t}),n.mkdirSync(t))},t.prototype._log=function(t){for(var e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];if(!this.config.quiet){var r=console[t];r.apply(void 0,e)}},t.prototype.log=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this._log.apply(this,i(["log"],t))},t.prototype.info=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this._log.apply(this,i(["info"],t))},t.prototype.warn=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this._log.apply(this,i(["warn"],t))},t.prototype.error=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this._log.apply(this,i(["error"],t))},t}();e.default=f},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}(493)})()}));
//# sourceMappingURL=index.js.map

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

6
dist/scaffold.d.ts vendored
View File

@@ -1,3 +1,4 @@
/// <reference types="node" />
import { IScaffold } from "./index.d";
declare class SimpleScaffold {
config: IScaffold.Config;
@@ -11,5 +12,10 @@ declare class SimpleScaffold {
private shouldWriteFile;
run(): void;
private writeDirectory;
_log(method: keyof typeof console, ...args: any[]): void;
log(...args: any[]): void;
info(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
}
export default SimpleScaffold;

2
dist/test.js vendored
View File

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

2
dist/test.js.map vendored

File diff suppressed because one or more lines are too long

1
index.d.ts vendored
View File

@@ -13,6 +13,7 @@ declare namespace IScaffold {
locals?: Locals
createSubfolder?: boolean
overwrite?: boolean | ((path: string) => boolean)
quiet?: boolean
}
export interface Locals {

View File

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

View File

@@ -15,6 +15,7 @@ class SimpleScaffold {
output: process.cwd(),
createSubfolder: true,
overwrite: true,
quiet: false,
}
this.config = { ...DefaultConfig, ...config }
@@ -35,7 +36,7 @@ class SimpleScaffold {
})
return template(this.locals)
} catch (e) {
console.warn("Problem using Handlebars, returning unmodified content")
this.warn("Problem using Handlebars, returning unmodified content")
return text
}
}
@@ -59,7 +60,7 @@ class SimpleScaffold {
}
private getFileContents(filePath: string): string {
console.log(fs.readFileSync(filePath))
this.log(fs.readFileSync(filePath))
return fs.readFileSync(filePath).toString()
}
@@ -108,13 +109,13 @@ class SimpleScaffold {
}
public run(): void {
console.log(`Generating scaffold: ${this.config.name}...`)
this.log(`Generating scaffold: ${this.config.name}...`)
const templates = this.fileList(this.config.templates)
let fileConf,
count = 0
console.log("Template files:", templates)
this.log("Template files:", templates)
for (fileConf of templates) {
let outputPath, contents, outputContents, file, base
try {
@@ -129,7 +130,7 @@ class SimpleScaffold {
contents = this.getFileContents(file)
outputContents = this.parseLocals(contents)
if (this.shouldWriteFile(outputPath)) {
console.info("Writing:", {
this.info("Writing:", {
file,
base,
outputPath,
@@ -137,10 +138,10 @@ class SimpleScaffold {
})
this.writeFile(outputPath, outputContents)
} else {
console.log(`Skipping file ${outputPath}`)
this.log(`Skipping file ${outputPath}`)
}
} catch (e) {
console.error("Error while processing file:", {
this.error("Error while processing file:", {
file,
base,
contents,
@@ -155,7 +156,7 @@ class SimpleScaffold {
throw new Error("No files to scaffold!")
}
console.log("Done")
this.log("Done")
}
private writeDirectory(outputPath: string, file: any): void {
@@ -164,13 +165,34 @@ class SimpleScaffold {
this.writeDirectory(parent, outputPath)
}
if (!fs.existsSync(outputPath)) {
console.info("Creating directory:", {
this.info("Creating directory:", {
file,
outputPath,
})
fs.mkdirSync(outputPath)
}
}
_log(method: keyof typeof console, ...args: any[]): void {
if (this.config.quiet) {
return
}
const fn = console[method] as (...a: any[]) => void
fn(...args)
}
log(...args: any[]): void {
this._log("log", ...args)
}
info(...args: any[]): void {
this._log("info", ...args)
}
warn(...args: any[]): void {
this._log("warn", ...args)
}
error(...args: any[]): void {
this._log("error", ...args)
}
}
export default SimpleScaffold