@@ -13,14 +13,15 @@ import {
1313 pathExists ,
1414 pascalCase ,
1515 isDir ,
16+ removeGlob ,
1617} from "./utils"
17- import { ScaffoldConfig } from "./types"
18+ import { LogLevel , ScaffoldConfig } from "./types"
1819
1920export async function Scaffold ( config : ScaffoldConfig ) {
2021 try {
2122 const options = { ...config }
2223 const data = { name : options . name , Name : pascalCase ( options . name ) , ...options . data }
23- log ( options , "Config :", {
24+ log ( options , LogLevel . Debug , "Full config :", {
2425 name : options . name ,
2526 templates : options . templates ,
2627 output : options . output ,
@@ -29,19 +30,33 @@ export async function Scaffold(config: ScaffoldConfig) {
2930 overwrite : options . overwrite ,
3031 quiet : options . quiet ,
3132 } )
32- log ( options , "Data:" , data )
33+ log ( options , LogLevel . Info , "Data:" , data )
3334 for ( let template of config . templates ) {
3435 try {
35- const _isDir = await isDir ( template )
36- const basePath = path
37- . resolve ( process . cwd ( ) , _isDir ? template : path . dirname ( template . replace ( "*" , "" ) . replace ( "//" , "/" ) ) )
38- . replace ( process . cwd ( ) , "." )
39- if ( _isDir ) {
36+ const _isGlob = template . includes ( "*" )
37+ const _nonGlobTemplate = _isGlob ? removeGlob ( template ) : template
38+ const _isDir = _isGlob ? false : await isDir ( template )
39+ const _shouldAddGlob = ! _isGlob && ! _isDir
40+ if ( _shouldAddGlob ) {
4041 template = template + "/**/*"
4142 }
4243 const files = await promisify ( glob ) ( template , { dot : true , debug : false } )
4344 for ( const templatePath of files ) {
4445 if ( ! ( await isDir ( templatePath ) ) ) {
46+ const basePath = path
47+ . resolve (
48+ process . cwd ( ) ,
49+ _isDir
50+ ? templatePath . replace ( template , "" )
51+ : path . dirname ( removeGlob ( templatePath ) . replace ( _nonGlobTemplate , "" ) )
52+ )
53+ . replace ( process . cwd ( ) + "/" , "" )
54+ . replace ( process . cwd ( ) , "" )
55+ log (
56+ options ,
57+ LogLevel . Debug ,
58+ `\ntemplate: ${ template } \ntemplatePath: ${ templatePath } , \nbase path: ${ basePath } \n`
59+ )
4560 await handleTemplateFile ( templatePath , basePath , options , data )
4661 }
4762 }
@@ -63,35 +78,53 @@ async function handleTemplateFile(
6378) : Promise < void > {
6479 return new Promise ( async ( resolve , reject ) => {
6580 try {
66- log ( options , `Parsing ${ templatePath } ` )
67- const inputPath = path . join ( process . cwd ( ) , templatePath )
81+ const inputPath = path . resolve ( process . cwd ( ) , templatePath )
6882 const outputPathOpt = getOptionValueForFile ( inputPath , data , options . output )
6983 const outputDir = path . resolve (
7084 process . cwd ( ) ,
71- ...( [ outputPathOpt , options . createSubFolder ? options . name : undefined ] . filter ( Boolean ) as string [ ] )
85+ ...( [ outputPathOpt , basePath , options . createSubFolder ? options . name : undefined ] . filter ( Boolean ) as string [ ] )
86+ )
87+ log (
88+ options ,
89+ LogLevel . Debug ,
90+ `\nParsing ${ templatePath } ` ,
91+ `\nBase path: ${ basePath } ` ,
92+ `\nFull input path: ${ inputPath } ` ,
93+ `\nFull output path: ${ outputDir } \n`
7294 )
7395 const outputPath = path . join ( outputDir , handlebarsParse ( path . basename ( inputPath ) , data ) )
7496 const overwrite = getOptionValueForFile ( inputPath , data , options . overwrite ?? false )
7597 const exists = await pathExists ( outputPath )
7698
99+ log (
100+ options ,
101+ LogLevel . Debug ,
102+ "Filename parsed:" ,
103+ handlebarsParse ( path . basename ( inputPath ) , data ) ,
104+ "Orig:" ,
105+ path . basename ( inputPath )
106+ // "Test:",
107+ // handlebarsParse("{{name}} {{name pascalCase}}", data)
108+ )
109+
77110 await createDirIfNotExists ( outputDir , options )
78111
79- log ( options , `Writing to ${ outputPath } ` )
112+ log ( options , LogLevel . Info , `Writing to ${ outputPath } ` )
80113 if ( ! exists || overwrite ) {
81114 if ( exists && overwrite ) {
82- log ( options , `File ${ outputPath } exists, overwriting` )
115+ log ( options , LogLevel . Info , `File ${ outputPath } exists, overwriting` )
83116 }
84117 const templateBuffer = await readFile ( inputPath )
85118 const outputContents = handlebarsParse ( templateBuffer , data )
86119
87120 if ( ! options . dryRun ) {
88121 await writeFile ( outputPath , outputContents )
89122 } else {
90- log ( options , "Content output:" )
91- log ( options , outputContents )
123+ log ( options , LogLevel . Info , "Content output:" )
124+ log ( options , LogLevel . Info , outputContents )
92125 }
93126 } else if ( exists ) {
94- log ( options , `File ${ outputPath } already exists, skipping` )
127+ log ( options , LogLevel . Info , `File ${ outputPath } already exists, skipping` )
95128 }
96129 resolve ( )
97130 } catch ( e : any ) {
0 commit comments