Skip to content

Commit

Permalink
[Release] 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
justgeek committed Apr 16, 2019
1 parent e91552b commit 41baeb8
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 102 deletions.
15 changes: 5 additions & 10 deletions lib/TemplateFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,32 @@ import TemplateGenerator from './TemplateGenerator';
* TemplateFactory
*/
class TemplateFactory {

/**
* Factory to generate the templates
* @param cli options
*/
static createTemplateFor( cli ) {

static createTemplateFor(cli) {
/**
* Generate Vue 2 component
*/
if( cli.component ) {

if (cli.component) {
return new TemplateGenerator(new ComponentTpl(cli.component, cli.postfix));
}

/**
* Generate Vue 2 directive
*/
if( cli.directive ) {
if (cli.directive) {
return new TemplateGenerator(new DirectiveTpl(cli.directive));
}

/**
* Generate Vue 2 single file component
*/
if( cli.single ) {
if (cli.single) {
return new TemplateGenerator(new SingleTpl(cli.single, cli.folder));
}

}

}

export default TemplateFactory
export default TemplateFactory;
85 changes: 51 additions & 34 deletions lib/TemplateGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import config from './config/config';
* TemplateGenerator
*/
class TemplateGenerator {

/**
* Todo: Inject swig, fs and config to mock them in the future tests
* @param options
*/
constructor( options ) {
constructor(options) {
this.TEMPLATES_DIR = `${__dirname}/blueprints`;
this._create(options);
}
Expand All @@ -22,13 +21,14 @@ class TemplateGenerator {
* @param options
* @private
*/
_create( options = {} ) {
const {name, type, actions, postfix} = options;
_create(options = {}) {
const { name, type, actions, postfix } = options;
const filesType = config.getConfigFile().filesType;
if( options.isDir ) {

if (options.isDir) {
this._createDirectory(this._getDirPath(type), { name, actions, filesType, postfix }, filesType);
} else {
const tpl = this._compileTpl(this._getSingleTpl(type), { name, actions, filesType });
const tpl = this._compileTpl(this._getSingleTpl(type), { name, postfix, actions, filesType });
this._createFile(name, type, filesType.script, tpl);
}
}
Expand All @@ -40,9 +40,9 @@ class TemplateGenerator {
* @returns {*}
* @private
*/
_compileTpl( file, { name, actions, filesType } ) {
_compileTpl(file, { name, actions, filesType, postfix }) {
const compiled = swig.compileFile(file);
return compiled({ name, actions, filesType });
return compiled({ name, actions, filesType, postfix });
}

/**
Expand All @@ -53,9 +53,9 @@ class TemplateGenerator {
* @param tpl
* @private
*/
_createFile( name, type, fileType, tpl ) {
fs.outputFile(this._createFilePath(name, type, fileType), tpl, function( err ) {
if( err ) console.log(err);
_createFile(name, type, fileType, tpl) {
fs.outputFile(this._createFilePath(name, type, fileType), tpl, function(err) {
if (err) console.error(err);
});
}

Expand All @@ -66,20 +66,25 @@ class TemplateGenerator {
* @param data
* @private
*/
_createDirectory( dirPath, data, fileTypes ) {
fs.readdir(dirPath, ( err, dir ) => {
const name = data.name;
const folder = path.join(process.cwd(), name);
_createDirectory(dirPath, data, fileTypes) {
fs.readdir(dirPath, (err, dir) => {
let name = data.name;

let folder = path.join(process.cwd(), name);
let splitIdx = folder.lastIndexOf('/');
if (splitIdx > -1) {
name = folder.slice(splitIdx + 1);
data.name = folder.slice(splitIdx + 1);
}

let filePath;

dir.forEach(tempFile => {
const compiled = this._compileTpl(`${dirPath}/${tempFile}`, data);
let fileName = this._createFileName(tempFile, name, fileTypes, data.postfix);

filePath = path.join(folder, fileName);

fs.outputFile(filePath, compiled, function( err ) {
if( err ) console.log(err);
fs.outputFile(filePath, compiled, function(err) {
if (err) console.error(err);
});
});
});
Expand All @@ -93,28 +98,40 @@ class TemplateGenerator {
* @returns {*}
* @private
*/
_createFileName( tempFile, name, fileTypes, postfix ) {
let newName = tempFile.replace(/temp/, name);
_createFileName(tempFile, name, fileTypes, postfix) {
let newName = tempFile.replace(/temp/, name + '.' + postfix);

if( newName.indexOf('tpl') > -1 ) {
newName = newName.replace(/tpl./, '')
if (newName.indexOf('tpl') > -1) {
newName = newName.replace(/tpl./, '');
newName = newName.replace(/extension/, fileTypes.html);
}

if( newName.indexOf('sty') > -1 ) {
newName = newName.replace(/sty./, '')
if (newName.indexOf('sty') > -1) {
newName = newName.replace(/sty./, '');
newName = newName.replace(/extension/, fileTypes.style);
}

if( newName.indexOf('script') > -1 ) {
newName = newName.replace(/script./, '')
if (newName.indexOf('script') > -1) {
newName = newName.replace(/script./, '');
newName = newName.replace(/extension/, fileTypes.script);
}

if( newName.indexOf('spec') > -1 ) {
if (newName.indexOf('spec') > -1) {
newName = newName.replace(/extension/, fileTypes.spec);
}

if (newName.indexOf('store') > -1) {
newName = newName.replace(/extension/, fileTypes.store);
}

if (newName.indexOf('mutations') > -1) {
newName = newName.replace(/extension/, fileTypes.mutations);
}

if (newName.indexOf('actions') > -1) {
newName = newName.replace(/extension/, fileTypes.actions);
}

return newName;
}

Expand All @@ -125,8 +142,8 @@ class TemplateGenerator {
* @returns {*}
* @private
*/
_getSingleTpl( type , extension = 'js') {
if(type === 'single') {
_getSingleTpl(type, extension = 'js') {
if (type === 'single') {
return `${this.TEMPLATES_DIR}/${type}/temp.vue`;
}
return `${this.TEMPLATES_DIR}/${type}/temp.${type}.${extension}`;
Expand All @@ -138,7 +155,7 @@ class TemplateGenerator {
* @returns {*}
* @private
*/
_getDirPath( type ) {
_getDirPath(type) {
return `${this.TEMPLATES_DIR}/${type}`;
}

Expand All @@ -150,12 +167,12 @@ class TemplateGenerator {
* @returns {*}
* @private
*/
_createFilePath( name, type, fileType ) {
if(type === 'single') {
_createFilePath(name, type, fileType) {
if (type === 'single') {
return path.join(process.cwd(), `${name}.vue`);
}
return path.join(process.cwd(), `${name}.${type}.${fileType}`);
}
}

export default TemplateGenerator
export default TemplateGenerator;
7 changes: 3 additions & 4 deletions lib/blueprints/component/index.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<template src="./{{name}}.{{ filesType.html }}"></template>
<script src="./{{name}}.{{ filesType.script }}"{% if filesType.script !="js" %} lang="{{ filesType.script }}"{% endif %}></script>
<style src="./{{name}}.{{ filesType.style }}" scoped lang="{{ filesType.style }}"></style>

<template src="./{{name}}{% if postfix %}.{{postfix}}{% endif %}.{{ filesType.html }}"></template>
<script src="./{{name}}{% if postfix %}.{{postfix}}{% endif %}.{{ filesType.script }}"{% if filesType.script !="js" %} lang="{{ filesType.script }}"{% endif %}></script>
<style src="./{{name}}{% if postfix %}.{{postfix}}{% endif %}.{{ filesType.style }}" scoped lang="{{ filesType.style }}"></style>
3 changes: 3 additions & 0 deletions lib/blueprints/component/temp.actions.extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const {{name | camelCase}}Actions={

}
7 changes: 7 additions & 0 deletions lib/blueprints/component/temp.mutations.extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { MutationTree } from 'vuex';
export const {{name | camelCase}}Mutations: MutationTree<any> = {
// DO__SOME_ACTION(state: any, payload: entity[]) {
// state.entity = payload;
// },
};

21 changes: 9 additions & 12 deletions lib/blueprints/component/temp.script.extension
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
export default {
name: '{{name | kebabCase}}',
components: {},
props: [],
data () {
return {
import { Component, Vue } from 'vue-property-decorator';

}
@Component({
components: {
},
computed: {
})

},
mounted () {
export default class {{name | camelCase}}{{postfix | camelCase}} extends Vue {

},
methods: {
created(){

}

}


14 changes: 14 additions & 0 deletions lib/blueprints/component/temp.store.extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from 'vuex';
import { {{name | camelCase}}Mutations } from 'from "./{{ name }}.mutations.{{ filesType.script }}";'
import { {{name | camelCase}}Actions} from 'from "./{{ name }}.actions.{{ filesType.script }}";'
class {{name | camelCase}}Store implements Module<any, any> {
public state = {
};
actions = { ...{{name | camelCase}}Actions}

mutations = {...{{name | camelCase}}Mutations}
}

export default new {{name | camelCase}}Store();


2 changes: 1 addition & 1 deletion lib/blueprints/component/temp.sty.extension
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.{{name | kebabCase}} {
#{{name | kebabCase}} {

}
4 changes: 2 additions & 2 deletions lib/blueprints/component/temp.tpl.extension
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% if filesType.html === 'jade' || filesType.html === 'pug' %}
section(class="{{name | kebabCase}}")
section(id="{{name | kebabCase}}")
h1 {{name | kebabCase}} Component
{% else %}
<section class="{{name | kebabCase}}">
<section id="{{name | kebabCase}}">
<h1>{{name | kebabCase}} Component</h1>
</section>
{% endif %}
53 changes: 32 additions & 21 deletions lib/config/cli-options.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
export default [{
export default [
{
name: 'help',
type: Boolean,
group: "options",
group: 'options',
description: 'help'
},
},
{
name: 'html',
type: String,
group: "options",
group: 'options',
description: 'set the default html templates'
},{
},
{
name: 'all',
type: String,
group: 'options',
description: 'set all files to typescript'
},
{
name: 'script',
type: String,
group: "options",
group: 'options',
description: 'set the default script language'
},{
},
{
name: 'component',
alias: 'c',
type: String,
group: "options",
group: 'options',
description: 'generate Vue js component',
defaultOption: true
},{
},
{
name: 'spec',
type: String,
group: "options",
group: 'options',
description: 'set the default spec language'
},{
},
{
name: 'style',
type: String,
group: "options",
group: 'options',
description: 'set the default style file'
},{
},
{
name: 'directive',
alias: 'd',
type: String,
group: "options",
group: 'options',
description: 'generate Vue js directive'
},
{
name: 'single',
alias: 's',
type: String,
group: "options",
group: 'options',
description: 'generate Vue js component single file'
},
{
{
name: 'folder',
alias: 'f',
type: Boolean,
group: "options",
group: 'options',
description: 'generate Vue js component single file inside new folder'
},
{
name: 'postfix',
type: String,
group: "options",
group: 'options',
description: 'create postfix in file name'
},

]
}
];
Loading

0 comments on commit 41baeb8

Please sign in to comment.