Skip to content

justgeek/vue-generate-component

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vue(X) generator Awesome

This tool is inspired and forked from https://github.com/NetanelBasal/vue-generate-component CLI util for easy generate Vue js component

Intro

This tool is created to help with scaffolding vue apps that uses vuex.It follows Angular Guidelines.
It also uses Vue Class Component. However the generated files will not always satisfy everyone.
that being said; please feel free to do the modifications that match your needs, and be sure you will still save some time 😉
Please feel free to contribute or just send your suggestions/feedback to mt@prollygeek.com

ToDos [Progress]

  • Create pages [WIP]
  • Create single file components [OPEN]
  • Create directives [OPEN]
  • Avail more options for CLI for more convenience [OPEN]
  • Automate release cycle and sem versioning increment [OPEN]

Installation

npm install -D vue-generate-component-x

-- or --

npm install -g vue-generate-component-x

Usage

vgcx <OPTIONS> <NAME>

Options

[
  {
    name: 'help',
    type: Boolean,
    description: 'help'
  },
  {
    name: 'html',
    type: String,
    description: 'set the default html templates'
  },
  {
    name: 'all',
    type: String,
    description: 'set all files to typescript'
  },
  {
    name: 'script',
    type: String,
    description: 'set the default script language'
  },
  {
    name: 'test',
    type: String,
    description: 'set the default test language'
  },
  {
    name: 'style',
    type: String,
    description: 'set the default style file'
  },
  {
    name: 'postfix',
    type: String,
    description: 'create postfix in file name'
  },
  {
    name: 'classExtension',
    type: String,
    description: 'if provided will replace default extension for pages with whatever name provided to this option'
  },
  {
    name: 'dest',
    type: String,
    description: 'destination path to move genrated folder inside'
  }
]

Create new Page

vgcx --script ts --postfix page --dest "/path/to" my-page-name

Will generate a page folder with following structure:

|-- path
    |-- to
        |-- my-page
            |-- index.vue
            |-- my-page.page.html
            |-- my-page.page.scss
            |-- my-page.page.ts
            |-- store
            |   |-- my-page.actions.ts
            |   |-- my-page.getters.js
            |   |-- my-page.mutations.ts
            |   |-- my-page.store.ts
            |-- __tests__
                |-- my-page.test.ts

index.vue

<template src="./my-page.page.html"></template>
<script src="./my-page.page.ts" lang="ts"></script>
<style src="./my-page.page.scss" scoped lang="scss"></style>

my-page.page.html

<section id="my-page" class="page">
  <h1>my-page Component</h1>
</section>

my-page.page.scss

#my-page {

}

my-page.page.ts

import Vue from 'vue';
import Component from 'vue-class-component';

@Component({
  components: {
  },
  name: 'my-page',
})

export default class MyPagePage extends Vue {
  
}

store/my-page.actions.ts

import { Commit } from 'vuex';
import { myPageMutations } from './my-page.mutations';
import { myPageStore } from './my-page.store';

export const namespace = 'myPageStore';

export const myPageActions = {

} 

export const myPageActionsMap = {

} 

store/my-page.mutations.ts

import { myPageState } from './my-page.store';

export const namespace = 'myPageStore';

export const myPageMutations = {

} 

export const myPageMutationsMap = {

} 

store/my-page.getters.ts

import { MyPageState } from  './my-page.store.ts';

export const myPageGetters = {

};

store/my-page.store.ts

import { myPageGetters } from  './my-page.getters';
import { myPageMutationsMap } from  './my-page.mutations';
import { myPageActionsMap } from './my-page.actions';

export interface MyPageState {

}

export const myPageState: MyPageState = {

};

export const myPageStore = {
  state: myPageState,
  getters: {...myPageGetters},
  actions: { ...myPageActionsMap },
  mutations: { ...myPageMutationsMap },
}

__tests__/my-page.test.ts

import Vue from 'vue';
import { mount } from '@vue/test-utils'
import MyPage from './my-page.ts';

describe('MyPage', () => {
  test('is a Vue instance', () => {
    const wrapper = mount(MyPage)
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})

Advanced Usage

This template serves a specific approach when building vue app, specifically when using vuex actions, mutations, getters, and store.
The reason for creating maps for actions and mutations is to enable auto complete when you import them anywhere.

Example:

actions.ts

export const namespace = 'myNameSpace';

export const myActions = {
  DO_SOME_ACTION: `${namespace}/DO_SOME_ACTION`,
};


export const myActionsMap = {
   [myActions.DO_SOME_ACTION]({ commit }: { commit: Commit }) {

     // you can commit mutations here to change state
     console.log('Action Was Called')

  }
}

mutations.ts

export const myMutations = {
  mutateMyState: 'mutateMyState',
}

export const myMutationsMap = {
  [myMutations.mutateMyState](state: MyPageState, payload: Payload) {
    state.someKey = payload;
  },
}

page.ts

import {myActions} from 'actions';

/* Once you import actions or mutations you will be able to use them directly inside your componet 
Also the auto complete will work as expected (should be very useful in large applications)
i.e
myActions. ++++ should open suggestion menu ++++
*/

export default class MyPage extends Vue {
  created(){
    /* store here can be page store or global app store that combines all modules:
       https://vuex.vuejs.org/guide/modules.html
    */
        store.dispatch(myActions.DO_SOME_ACTION);
  }
}

Made with ❤️ by Mohamed Taher <mt@prollygeek.com>

About

Vue js component generator

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 81.3%
  • TypeScript 9.7%
  • Vue 7.8%
  • Other 1.2%