From 1e0abf919889a475c8f94bd56d47e395dd28e491 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Mon, 1 Jan 2018 23:10:59 +0200 Subject: [PATCH] Published + renamed, bugfixes --- .gitignore | 1 + examples/test-input/Component/{%Name%}.tsx | 13 ++++++++++++ examples/test-input/Component/{{Name}}.tsx | 11 ----------- examples/test-output/MyComponent.tsx | 11 ----------- package.json | 16 +++++++-------- scaffold.ts | 23 ++++++---------------- scripts/test.js | 12 +++++++++++ 7 files changed, 40 insertions(+), 47 deletions(-) create mode 100644 examples/test-input/Component/{%Name%}.tsx delete mode 100644 examples/test-input/Component/{{Name}}.tsx delete mode 100644 examples/test-output/MyComponent.tsx create mode 100644 scripts/test.js diff --git a/.gitignore b/.gitignore index 5ccd65a..7953ed0 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ typings/ .env dist +examples/test-output diff --git a/examples/test-input/Component/{%Name%}.tsx b/examples/test-input/Component/{%Name%}.tsx new file mode 100644 index 0000000..b368779 --- /dev/null +++ b/examples/test-input/Component/{%Name%}.tsx @@ -0,0 +1,13 @@ +import * as React from 'react' + +class {%Name%} extends React.Component { + private {%property%} + + constructor() { + this.{%property%} = {%value%} + } + +
+} + +export default {%Name%} diff --git a/examples/test-input/Component/{{Name}}.tsx b/examples/test-input/Component/{{Name}}.tsx deleted file mode 100644 index 1573bfe..0000000 --- a/examples/test-input/Component/{{Name}}.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import * as React from 'react' - -class {{Name}} extends React.Component { - private {{property}} - - constructor() { - this.{{property}} = {{value}} - } -} - -export default {{Name}} diff --git a/examples/test-output/MyComponent.tsx b/examples/test-output/MyComponent.tsx deleted file mode 100644 index 1573bfe..0000000 --- a/examples/test-output/MyComponent.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import * as React from 'react' - -class {{Name}} extends React.Component { - private {{property}} - - constructor() { - this.{{property}} = {{value}} - } -} - -export default {{Name}} diff --git a/package.json b/package.json index a5eda5e..3134c2a 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,17 @@ { - "name": "scaffolder", - "version": "1.0.0", + "name": "simple-scaffold", + "version": "0.1.1", "description": "Create files based on templates", - "repository": "https://github.com/chenasraf/scaffolder.git", - "author": "Chen Asraf ", + "repository": "https://github.com/chenasraf/simple-scaffold.git", + "author": "Chen Asraf ", "license": "MIT", "main": "./dist/scaffold.js", "bin": "./dist/scaffold.js", "scripts": { - "build": "webpack -p", + "build": "webpack -p && chmod +x ./dist/scaffold.js", "dev": "webpack --watch", - "start": "chmod +x ./dist/scaffold.js && node ./dist/scaffold.js" + "start": "node ./dist/scaffold.js", + "test": "yarn build && node ./scripts/test.js" }, "devDependencies": { "@types/node": "^8.0.50", @@ -18,6 +19,5 @@ "typescript": "^2.6.1", "webpack": "^3.8.1", "webpack-dev-server": "^2.9.4" - }, - "dependencies": {} + } } diff --git a/scaffold.ts b/scaffold.ts index 668d0f5..9b99d27 100644 --- a/scaffold.ts +++ b/scaffold.ts @@ -2,7 +2,7 @@ import * as fs from 'fs' import * as path from 'path' import { IScaffold } from './index' -class Scaffold { +class SimpleScaffold { private config: IScaffold.IConfig private locals = {} as any public scaffoldName = process.argv[2] @@ -28,7 +28,7 @@ class Scaffold { private parseLocals(text: string): string { let out = text.toString() - const pattern = /{{\s*(.+)\s*}}/gi + const pattern = /{[%]\s*([^%{}]+)\s*[%]}/gi return out.replace(pattern, (match: string, $1: string) => this.locals[$1]) } @@ -60,7 +60,7 @@ class Scaffold { if (typeof this.config.output === 'function') { out = this.config.output(file) } else { - out = this.config.output + '/' + path.basename(file) + out = this.config.output + `/${this.scaffoldName}/` + path.basename(file) } return this.parseLocals(out) @@ -70,6 +70,7 @@ class Scaffold { if (!fs.existsSync(path.dirname(filePath))) { fs.mkdirSync(path.dirname(filePath)) } + console.info('Writing file:', filePath) fs.writeFileSync(filePath, fileContents, { encoding: 'utf-8' }) } @@ -80,21 +81,9 @@ class Scaffold { const outputPath = this.getOutputPath(file) const contents = this.getFileContents(file) const outputContents = this.parseLocals(contents) - - this.writeFile(outputPath, contents) - - console.info({outputPath, outputContents}) + this.writeFile(outputPath, outputContents) }) } } -const templateDir = process.cwd() + '/examples' -const scf = new Scaffold({ - templates: [templateDir + '/test-input/Component'], - output: templateDir + '/test-output', - locals: { - property: 'myProp' - } -}) - -scf.run() +exports.default = SimpleScaffold diff --git a/scripts/test.js b/scripts/test.js new file mode 100644 index 0000000..29da849 --- /dev/null +++ b/scripts/test.js @@ -0,0 +1,12 @@ +const Scaffolder = require('../dist/scaffold').default + +const templateDir = process.cwd() + '/examples' + +const scf = new Scaffolder({ + templates: [templateDir + '/test-input/Component'], + output: templateDir + '/test-output', + locals: { + property: 'myProp', + value: '"value"' + } +}).run()