|
| 1 | +import * as fs from 'fs' |
| 2 | +import * as path from 'path' |
| 3 | +import { IScaffold } from './index' |
| 4 | + |
| 5 | +class Scaffold { |
| 6 | + private config: IScaffold.IConfig |
| 7 | + private DefaultConfig: IScaffold.IConfig = { |
| 8 | + templates: [], |
| 9 | + output: path.resolve(process.cwd()) |
| 10 | + } |
| 11 | + |
| 12 | + constructor(config: IScaffold.IConfig) { |
| 13 | + // this.config = utils.merge<IScaffold.IConfig>(this.DefaultConfig, config) |
| 14 | + this.config = (Object as any).assign({}, this.DefaultConfig, config) |
| 15 | + console.info('Config loaded:', this.config) |
| 16 | + } |
| 17 | + |
| 18 | + private parseLocals(text: string): string { |
| 19 | + let out = text.toString() |
| 20 | + const pattern = /{{\s*(.+)\s*}}/gi |
| 21 | + return out.replace(pattern, (match: string, $1: string) => { |
| 22 | + const upperName = 'MyComponent' |
| 23 | + const lowerName = 'myComponent' |
| 24 | + |
| 25 | + const replaceMap = { |
| 26 | + Name: upperName, |
| 27 | + name: lowerName, |
| 28 | + property: 'someProp' |
| 29 | + } as any |
| 30 | + |
| 31 | + return replaceMap[$1] |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + private getFileList(pathList: string[]): string[] { |
| 36 | + let outList: string[] = [] |
| 37 | + |
| 38 | + pathList.forEach((checkPath: string) => { |
| 39 | + const stat = fs.lstatSync(checkPath) |
| 40 | + if (stat.isFile()) { |
| 41 | + console.info('pushing', checkPath) |
| 42 | + outList.push(checkPath) |
| 43 | + } else if (stat.isDirectory()) { |
| 44 | + console.info('going into dir', checkPath) |
| 45 | + const innerFiles = fs.readdirSync(checkPath).map(p => path.join(checkPath, p)) |
| 46 | + outList = outList.concat(this.getFileList(innerFiles)) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + return outList |
| 51 | + } |
| 52 | + |
| 53 | + private getFileContents(filePath: string): string { |
| 54 | + return fs.readFileSync(filePath).toString() |
| 55 | + } |
| 56 | + |
| 57 | + private getOutputPath(file: string): string { |
| 58 | + let out |
| 59 | + |
| 60 | + if (typeof this.config.output === 'function') { |
| 61 | + out = this.config.output(file) |
| 62 | + } else { |
| 63 | + out = this.config.output + '/' + path.basename(file) |
| 64 | + } |
| 65 | + |
| 66 | + return this.parseLocals(out) |
| 67 | + } |
| 68 | + |
| 69 | + private writeFile(filePath: string, fileContents: string): void { |
| 70 | + if (!fs.existsSync(path.dirname(filePath))) { |
| 71 | + fs.mkdirSync(path.dirname(filePath)) |
| 72 | + } |
| 73 | + fs.writeFileSync(filePath, fileContents, { encoding: 'utf-8' }) |
| 74 | + } |
| 75 | + |
| 76 | + public run() { |
| 77 | + const inputFiles = this.getFileList(this.config.templates) |
| 78 | + console.info(inputFiles) |
| 79 | + inputFiles.forEach((file: string) => { |
| 80 | + const outputPath = this.getOutputPath(file) |
| 81 | + const contents = this.getFileContents(file) |
| 82 | + const outputContents = this.parseLocals(contents) |
| 83 | + |
| 84 | + this.writeFile(outputPath, contents) |
| 85 | + |
| 86 | + console.info({outputPath, outputContents}) |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +const templateDir = process.cwd() + '/examples' |
| 92 | +const scf = new Scaffold({ |
| 93 | + templates: [templateDir + '/test-input/Component'], |
| 94 | + output: templateDir + '/test-output' |
| 95 | +}) |
| 96 | + |
| 97 | +scf.run() |
0 commit comments