Skip to content

Commit 652621f

Browse files
author
Chen Asraf
committed
Scaffold basically works, file path resolving sucky atm
1 parent 4896c10 commit 652621f

11 files changed

Lines changed: 3266 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60+
dist

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "attach",
10+
"name": "Attach by Process ID",
11+
"processId": "${command:PickProcess}"
12+
},
13+
{
14+
"type": "node",
15+
"request": "launch",
16+
"name": "Debug Scaffold",
17+
"protocol": "inspector",
18+
"program": "${workspaceFolder}/dist/scaffold.js"
19+
}
20+
]
21+
}

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
# scaffolder
2-
Create files based on templates
2+
Scaffolder allows you to create your structured files based on templates.
3+
4+
### How to use
5+
6+
#### Install
7+
You Scaffolder by using `npm`. Global flag is useful if you want easy cli access to it.
8+
9+
```
10+
npm install -g scaffolder
11+
```
12+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react'
2+
3+
class {{Name}} extends React.Component {
4+
private {{property}}
5+
6+
constructor() {
7+
this.{{property}} = {{value}}
8+
}
9+
}
10+
11+
export default {{Name}}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react'
2+
3+
class {{Name}} extends React.Component {
4+
private {{property}}
5+
6+
constructor() {
7+
this.{{property}} = {{value}}
8+
}
9+
}
10+
11+
export default {{Name}}

index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export namespace IScaffold {
2+
3+
export interface IConfig {
4+
templates: string[]
5+
output: string | ((path: string) => string)
6+
}
7+
8+
export interface IReplacement {
9+
find: string | RegExp
10+
replace(): string
11+
[other: string]: any
12+
}
13+
14+
}

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "scaffolder",
3+
"version": "1.0.0",
4+
"description": "Create files based on templates",
5+
"repository": "https://github.com/chenasraf/scaffolder.git",
6+
"author": "Chen Asraf <chen@asraf.me>",
7+
"license": "MIT",
8+
"main": "./dist/scaffold.js",
9+
"bin": "./dist/scaffold.js",
10+
"scripts": {
11+
"build": "webpack -p",
12+
"dev": "webpack --watch",
13+
"start": "chmod +x ./dist/scaffold.js && node ./dist/scaffold.js"
14+
},
15+
"devDependencies": {
16+
"@types/node": "^8.0.50",
17+
"ts-loader": "^3.1.1",
18+
"typescript": "^2.6.1",
19+
"webpack": "^3.8.1",
20+
"webpack-dev-server": "^2.9.4"
21+
},
22+
"dependencies": {}
23+
}

scaffold.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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()

tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "es2015",
5+
"declaration": true,
6+
"outDir": "./dist",
7+
"strict": true,
8+
"sourceMap": true
9+
},
10+
"exclude": [
11+
"examples"
12+
]
13+
}

webpack.config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path')
2+
3+
module.exports = {
4+
target: 'node',
5+
entry: './scaffold.ts',
6+
output: {
7+
filename: 'scaffold.js',
8+
path: path.resolve(__dirname, 'dist')
9+
},
10+
resolve: {
11+
extensions: ['.ts']
12+
},
13+
module: {
14+
rules: [
15+
{
16+
test: /\.tsx?$/,
17+
loader: 'ts-loader',
18+
exclude: ['./examples', './utils']
19+
}
20+
]
21+
}
22+
}

0 commit comments

Comments
 (0)