Repository: teleporthq/teleport-code-generators
Pull request #250 (2019-06-05 11:32:12)

refactor: Set default template to the generic packer
#248


Patch:
diff --git a/.travis.yml b/.travis.yml
index 920defbe..b23e8823 100755
--- a/.travis.yml
+++ b/.travis.yml
@@ -6,11 +6,11 @@ script:
   - npm run lint
   - npm run test:coverage
   - npm run upload:coverage
-# deploy:
-#   provider: npm
-#   email: "$NPM_EMAIL"
-#   api_key: "$NPM_TOKEN"
-#   skip_cleanup: true
-#   on:
-#     branch: master
-#     tags: true
+deploy:
+  provider: npm
+  email: "$NPM_EMAIL"
+  api_key: "$NPM_TOKEN"
+  skip_cleanup: true
+  on:
+    branch: master
+    tags: true
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b55ee683..ea015f6a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,60 +1,35 @@
-## v0.7.0 (2019-06-05)
+## Unreleased (2019-05-03)
 
 #### :rocket: New Feature
-  * [#232](https://github.com/teleporthq/teleport-code-generators/pull/232) (feat-#197) Project Packers and publishers, supporting publish for netlify, now, local dist and zip. ([@ionutpasca](https://github.com/ionutpasca))
-  * [#227](https://github.com/teleporthq/teleport-code-generators/pull/227) feat(navlinks): transitionTo can now refer to the state key, not the url ([@alexnm](https://github.com/alexnm))
-  * [#218](https://github.com/teleporthq/teleport-code-generators/pull/218) feat(component-gen): A Generic component generatorby splitting post processing. ([@alexnm](https://github.com/alexnm))
-  * [#194](https://github.com/teleporthq/teleport-code-generators/pull/194) (feat): Mongorepo structure by the adopting Lerna ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#195](https://github.com/teleporthq/teleport-code-generators/pull/195) (feat): Plugin support for Styled Components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-
-#### :electric_plug: React Generators
-* `teleport-component-generator-react`, `teleport-generator-shared`, `teleport-plugin-react-base-component`
-  * [#212](https://github.com/teleporthq/teleport-code-generators/pull/212) Handling no-element tags as root nodes for generating components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
+* `teleport-component-generator-react`, `teleport-component-generator-vue`, `teleport-generator-core`, `teleport-generator-shar
+ed`, `teleport-plugin-import-statements`, `teleport-plugin-react-app-routing`, `teleport-plugin-react-base-component`, `telepor
+t-plugin-react-css-modules`, `teleport-plugin-react-inline-styles`, `teleport-plugin-react-jss`, `teleport-plugin-react-proptyp
+es`, `teleport-plugin-react-styled-components`, `teleport-plugin-react-styled-jsx`, `teleport-plugin-vue-app-routing`, `telepor
+t-plugin-vue-base-component`, `teleport-plugin-vue-css`, `teleport-project-generator-react-basic`, `teleport-project-generator-
+react-next`, `teleport-project-generator-vue-basic`, `teleport-project-generator-vue-nuxt`
+  * [#194](https://github.com/teleporthq/teleport-code-generators/pull/194) Feature/lerna ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
 * Other
-  * [#195](https://github.com/teleporthq/teleport-code-generators/pull/195) (feat): Plugin support for Styled Components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#201](https://github.com/teleporthq/teleport-code-generators/pull/201) fix(style-flavours): Fixed style flavours w.r.t to their behaviour ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
+  * [#195](https://github.com/teleporthq/teleport-code-generators/pull/195) Feature/teleport plugin styled components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
 
-#### :electric_plug: Vue Generator
-* `teleport-plugin-vue-base-component`
-  * [#231](https://github.com/teleporthq/teleport-code-generators/pull/231) fix(vue-base-component): Support for nested conditionals in Vue ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
+#### :electric_plug: React Generators
+* [#195](https://github.com/teleporthq/teleport-code-generators/pull/195) Feature/teleport plugin styled components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
+* [#201](https://github.com/teleporthq/teleport-code-generators/pull/201) fix(style-flavours): Fixed style flavours w.r.t to their behaviour ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
 
 #### :nail_care: Style Flavours
-* `teleport-component-generator-react`, `teleport-generator-shared`, `teleport-plugin-react-styled-components`
-  * [#216](https://github.com/teleporthq/teleport-code-generators/pull/216) fix(react-styled-components): Fixed of adding styled by default ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-* Other
-  * [#195](https://github.com/teleporthq/teleport-code-generators/pull/195) (feat): Plugin support for Styled Components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#201](https://github.com/teleporthq/teleport-code-generators/pull/201) fix(style-flavours): Fixed style flavours w.r.t to their behaviour ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-
-#### :earth_asia: Core
-  * [#225](https://github.com/teleporthq/teleport-code-generators/pull/225) feat(proj-gen): reusing linker functionality + formatting from generic component generators ([@alexnm](https://github.com/alexnm))
-  * [#218](https://github.com/teleporthq/teleport-code-generators/pull/218) feat(component-gen): A Generic component generatorby splitting post processing. ([@alexnm](https://github.com/alexnm))
+* [#195](https://github.com/teleporthq/teleport-code-generators/pull/195) Feature/teleport plugin styled components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
+* [#201](https://github.com/teleporthq/teleport-code-generators/pull/201) fix(style-flavours): Fixed style flavours w.r.t to their behaviour ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
 
 #### :bug: Bug Fix
-  * [#231](https://github.com/teleporthq/teleport-code-generators/pull/231) fix(vue-base-component): Support for nested conditionals in Vue ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#224](https://github.com/teleporthq/teleport-code-generators/pull/224) Feature/validator extension + bug fix  ([@anamariaoros](https://github.com/anamariaoros))
-  * [#221](https://github.com/teleporthq/teleport-code-generators/pull/221) feat(plugin-react-proptypes): Added isRequired attribute for proptypes ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#223](https://github.com/teleporthq/teleport-code-generators/pull/223) Fix/#175 Components name are same as generated from Router ([@alexnm](https://github.com/alexnm))
-  * [#212](https://github.com/teleporthq/teleport-code-generators/pull/212) Handling no-element tags as root nodes for generating components ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#216](https://github.com/teleporthq/teleport-code-generators/pull/216) fix(react-styled-components): Fixed of adding styled by default ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#201](https://github.com/teleporthq/teleport-code-generators/pull/201) fix(style-flavours): Fixed style flavours w.r.t to their behaviour ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
+* [#201](https://github.com/teleporthq/teleport-code-generators/pull/201) fix(style-flavours): Fixed style flavours w.r.t to their behaviour ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
 
 #### :crystal_ball: UIDL
-  * [#224](https://github.com/teleporthq/teleport-code-generators/pull/224) Feature/validator extension + bug fix  ([@anamariaoros](https://github.com/anamariaoros))
-  * [#221](https://github.com/teleporthq/teleport-code-generators/pull/221) feat(plugin-react-proptypes): Added isRequired attribute for proptypes ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#219](https://github.com/teleporthq/teleport-code-generators/pull/219) refactor(generator-core): Schema to support array as  a default value ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-  * [#173](https://github.com/teleporthq/teleport-code-generators/pull/173) refactor(uidl-samples/component-schema): Update component schema to a… ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
-
-#### :house: Dev Experience
-  * [#241](https://github.com/teleporthq/teleport-code-generators/pull/241) Feat/240 prep for release ([@alexnm](https://github.com/alexnm))
-  * [#228](https://github.com/teleporthq/teleport-code-generators/pull/228) chore: add tests for plugins and generic component generator ([@alexnm](https://github.com/alexnm))
-  * [#220](https://github.com/teleporthq/teleport-code-generators/pull/220) Tests ([@anamariaoros](https://github.com/anamariaoros))
+* [#173](https://github.com/teleporthq/teleport-code-generators/pull/173) refactor(uidl-samples/component-schema): Update component schema to a… ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
 
-#### Committers: 5
+#### Committers: 3
 - Alex Moldovan ([@alexnm](https://github.com/alexnm))
-- Anamaria Oros ([@anamariaoros](https://github.com/anamariaoros))
 - Jaya Krishna Namburu ([@JayaKrishnaNamburu](https://github.com/JayaKrishnaNamburu))
 - Luca Guzzon ([@lguzzon](https://github.com/lguzzon))
-- Pașca Ionuț ([@ionutpasca](https://github.com/ionutpasca))
+
 
 ## v0.6.0 (2019-04-09)
 
diff --git a/README.md b/README.md
index 9972e53c..76c67201 100755
--- a/README.md
+++ b/README.md
@@ -189,7 +189,7 @@ generator.addMapping(reactMapping)
 Next, we can add all our **plugins** to the existing generator. Note that the **order** of the plugins is important as they will be run in **sequence** on the input UIDL:
 
 ```javascript
-generator.addPlugin(reactPlugin) // the base plugin needs to be the first one!
+generator.addPlugin(reactPlugin)
 generator.addPlugin(styledJSXPlugin)
 generator.addPlugin(propTypesPlugin)
 generator.addPlugin(importStatementsPlugin)
diff --git a/lerna.json b/lerna.json
index 09cd6fb3..ce6ab82f 100644
--- a/lerna.json
+++ b/lerna.json
@@ -24,8 +24,7 @@
       "style flavours": ":nail_care: Style Flavours",
       "core": ":earth_asia: Core",
       "bug": ":bug: Bug Fix",
-      "uidl": ":crystal_ball: UIDL",
-      "dev experience": ":house: Dev Experience"
+      "uidl": ":crystal_ball: UIDL"
     }
   }
 }
diff --git a/packages/teleport-project-generator-react-basic/src/index.ts b/packages/teleport-project-generator-react-basic/src/index.ts
index 3e38e880..fefa3951 100644
--- a/packages/teleport-project-generator-react-basic/src/index.ts
+++ b/packages/teleport-project-generator-react-basic/src/index.ts
@@ -45,7 +45,7 @@ export const createReactBasicGenerator = (generatorOptions: ProjectGeneratorOpti
 
   const generateProject: GenerateProjectFunction = async (
     input: Record<string, unknown>,
-    template: TemplateDefinition,
+    template: TemplateDefinition = {},
     options: ProjectGeneratorOptions = {}
   ) => {
     // Step 0: Validate project input and transform to UIDL and validate content of UIDL
diff --git a/packages/teleport-project-generator-react-next/src/index.ts b/packages/teleport-project-generator-react-next/src/index.ts
index 3a26b70c..abd763ec 100644
--- a/packages/teleport-project-generator-react-next/src/index.ts
+++ b/packages/teleport-project-generator-react-next/src/index.ts
@@ -41,7 +41,7 @@ export const createReactNextGenerator = (generatorOptions: ProjectGeneratorOptio
 
   const generateProject: GenerateProjectFunction = async (
     input: Record<string, unknown>,
-    template: TemplateDefinition,
+    template: TemplateDefinition = {},
     options: ProjectGeneratorOptions = {}
   ) => {
     // Step 0: Validate project input
diff --git a/packages/teleport-project-generator-vue-basic/src/index.ts b/packages/teleport-project-generator-vue-basic/src/index.ts
index aab65934..d95174c6 100644
--- a/packages/teleport-project-generator-vue-basic/src/index.ts
+++ b/packages/teleport-project-generator-vue-basic/src/index.ts
@@ -41,7 +41,7 @@ export const createVueBasicGenerator = (generatorOptions: ProjectGeneratorOption
 
   const generateProject: GenerateProjectFunction = async (
     input: Record<string, unknown>,
-    template: TemplateDefinition,
+    template: TemplateDefinition = {},
     options: ProjectGeneratorOptions = {}
   ) => {
     // Step 0: Validate project input
diff --git a/packages/teleport-project-generator-vue-nuxt/src/index.ts b/packages/teleport-project-generator-vue-nuxt/src/index.ts
index e34fe6ed..6eaafde0 100644
--- a/packages/teleport-project-generator-vue-nuxt/src/index.ts
+++ b/packages/teleport-project-generator-vue-nuxt/src/index.ts
@@ -42,7 +42,7 @@ export const createVueNuxtGenerator = (generatorOptions: ProjectGeneratorOptions
 
   const generateProject: GenerateProjectFunction = async (
     input: Record<string, unknown>,
-    template: TemplateDefinition,
+    template: TemplateDefinition = {},
     options: ProjectGeneratorOptions = {}
   ) => {
     // Step 0: Validate project input
diff --git a/packages/teleport-project-packer/src/constants.ts b/packages/teleport-project-packer/src/constants.ts
index 5b51653c..68a1a805 100644
--- a/packages/teleport-project-packer/src/constants.ts
+++ b/packages/teleport-project-packer/src/constants.ts
@@ -1,1 +1,9 @@
 export const GITHUB_API_BASE_URL = 'https://api.github.com'
+
+export const DEFAULT_TEMPLATE = {
+  templateFolder: {
+    name: 'teleport-project',
+    files: [],
+    subFolders: [],
+  },
+}
diff --git a/packages/teleport-project-packer/src/errors.ts b/packages/teleport-project-packer/src/errors.ts
index e40589f2..b39b5b6d 100644
--- a/packages/teleport-project-packer/src/errors.ts
+++ b/packages/teleport-project-packer/src/errors.ts
@@ -1,4 +1,3 @@
-export const NO_TEMPLATE_PROVIDED = 'No template provided'
 export const NO_REMOTE_TEMPLATE_PROVIDED = 'No remote definition of template provided'
 export const NO_GENERATOR_FUNCTION_PROVIDED = 'No generator function provided'
 export const NO_PUBLISHER_PROVIDED = 'No publisher provided'
diff --git a/packages/teleport-project-packer/src/index.ts b/packages/teleport-project-packer/src/index.ts
index 3092e67b..6a251be3 100644
--- a/packages/teleport-project-packer/src/index.ts
+++ b/packages/teleport-project-packer/src/index.ts
@@ -9,11 +9,11 @@ import {
 } from '@teleporthq/teleport-types'
 import { injectAssetsToProject, fetchTemplate } from './utils'
 import {
-  NO_TEMPLATE_PROVIDED,
   NO_GENERATOR_FUNCTION_PROVIDED,
   NO_PUBLISHER_PROVIDED,
   NO_REMOTE_TEMPLATE_PROVIDED,
 } from './errors'
+import { DEFAULT_TEMPLATE } from './constants'
 
 export interface PackerFactoryParams {
   publisher?: Publisher<any, any>
@@ -56,10 +56,7 @@ export const createProjectPacker: PackerFactory = (params: PackerFactoryParams =
   const loadTemplate = async (
     templateToLoad?: TemplateDefinition
   ): Promise<LoadTemplateResponse> => {
-    template = templateToLoad || template
-    if (!template) {
-      return { success: false, payload: NO_TEMPLATE_PROVIDED }
-    }
+    template = templateToLoad || template || DEFAULT_TEMPLATE
 
     if (template.templateFolder) {
       return { success: true, payload: template.templateFolder }
@@ -82,9 +79,6 @@ export const createProjectPacker: PackerFactory = (params: PackerFactoryParams =
     const definedProjectUIDL = uidl
 
     const packTemplate = packParams.template || template
-    if (!packTemplate) {
-      return { success: false, payload: NO_TEMPLATE_PROVIDED }
-    }
 
     const packGeneratorFunction = packParams.generatorFunction || generatorFunction
     if (!packGeneratorFunction) {
diff --git a/packages/teleport-shared/src/utils/project-utils.ts b/packages/teleport-shared/src/utils/project-utils.ts
index a5b5e6ec..80fb52af 100644
--- a/packages/teleport-shared/src/utils/project-utils.ts
+++ b/packages/teleport-shared/src/utils/project-utils.ts
@@ -261,12 +261,12 @@ export const generateLocalDependenciesPrefix = (
   meta: LocalDependenciesMeta
 ): string => {
   const initialComponentsPath =
-    template.meta && template.meta.componentsPath
+    template && template.meta && template.meta.componentsPath
       ? template.meta.componentsPath
       : [].concat(meta.defaultComponentsPath)
 
   const initialPagesPath =
-    template.meta && template.meta.pagesPath
+    template && template.meta && template.meta.pagesPath
       ? template.meta.pagesPath
       : [].concat(meta.defaultPagesPath)
 
diff --git a/packages/teleport-types/src/generators.ts b/packages/teleport-types/src/generators.ts
index 8f98bcf5..b191ff19 100644
--- a/packages/teleport-types/src/generators.ts
+++ b/packages/teleport-types/src/generators.ts
@@ -177,7 +177,7 @@ export interface ProjectGeneratorOutput {
 
 export type GenerateProjectFunction = (
   input: Record<string, unknown>,
-  template: TemplateDefinition,
+  template?: TemplateDefinition,
   options?: ProjectGeneratorOptions
 ) => Promise<ProjectGeneratorOutput>
 


Test patch:
diff --git a/packages/teleport-project-packer-test/src/index.ts b/packages/teleport-project-packer-test/src/index.ts
index cea852df..3bdf65be 100644
--- a/packages/teleport-project-packer-test/src/index.ts
+++ b/packages/teleport-project-packer-test/src/index.ts
@@ -17,19 +17,12 @@ const generators = {
   'vue-nuxt': vueNuxtGenerator,
 }
 
-const publisher = createDiskPublisher({ outputPath: 'dist' })
+const publisher = createDiskPublisher({
+  outputPath: 'dist',
+})
 
 const packProject = async (projectType: string) => {
-  const template = {
-    templateFolder: {
-      name: projectType,
-      files: [],
-      subFolders: [],
-    },
-  }
-
   projectPacker.setPublisher(publisher)
-  projectPacker.setTemplate(template)
   projectPacker.setGeneratorFunction(generators[projectType].generateProject)
 
   const result = await projectPacker.pack(projectUIDL as ProjectUIDL)
diff --git a/packages/teleport-project-packer/__tests__/index.ts b/packages/teleport-project-packer/__tests__/index.ts
index 58bd19d7..84980a44 100644
--- a/packages/teleport-project-packer/__tests__/index.ts
+++ b/packages/teleport-project-packer/__tests__/index.ts
@@ -18,11 +18,11 @@ import {
 
 import { createProjectPacker } from '../src'
 import {
-  NO_TEMPLATE_PROVIDED,
   NO_REMOTE_TEMPLATE_PROVIDED,
   NO_GENERATOR_FUNCTION_PROVIDED,
   NO_PUBLISHER_PROVIDED,
 } from '../src/errors'
+import { DEFAULT_TEMPLATE } from '../src/constants'
 
 const assetFile = readFileSync(join(__dirname, 'asset.png'))
 const base64File = new Buffer(assetFile).toString('base64')
@@ -60,12 +60,12 @@ describe('teleport generic project packer', () => {
     expect(() => packer.setPublisher(publisher)).not.toThrow()
   })
 
-  it('should fail to load template if no template definition is provided', async () => {
+  it('should load default template if no template definition is provided', async () => {
     const packer = createProjectPacker()
 
     const { success, payload } = await packer.loadTemplate()
-    expect(success).toBeFalsy()
-    expect(payload).toBe(NO_TEMPLATE_PROVIDED)
+    expect(success).toBeTruthy()
+    expect(JSON.stringify(payload)).toBe(JSON.stringify(DEFAULT_TEMPLATE.templateFolder))
   })
 
   it('should load template if template folder is described in the definition', async () => {
@@ -88,14 +88,6 @@ describe('teleport generic project packer', () => {
     expect(payload).toBe(NO_REMOTE_TEMPLATE_PROVIDED)
   })
 
-  it('should fail to pack if no template is provided', async () => {
-    const packer = createProjectPacker()
-
-    const { success, payload } = await packer.pack(projectJson as ProjectUIDL)
-    expect(success).toBeFalsy()
-    expect(payload).toBe(NO_TEMPLATE_PROVIDED)
-  })
-
   it('should fail to pack if no generator function is provided', async () => {
     const packer = createProjectPacker()