/**
 * Step 5
 * Where you write the generator specific files (routes, controllers, etc)
 */

const fs = require('fs')
const path = require('path')
const TRAILS_TEMPLATE = path.dirname(require.resolve('trails/archetype'))

export default {
  genericApi () {
    this.fs.copy(path.resolve(TRAILS_TEMPLATE, 'api/services', '**'), this.destinationPath('api/services'))
    this.fs.copy(path.resolve(TRAILS_TEMPLATE, 'api/models', '**'), this.destinationPath('api/models'))
  },
  serverDependentApi () {
    const server = this.answers['web-engine']
    const dest = this.destinationPath()
    const PROJECT_PATH = this.destinationPath('node_modules/trailpack-' + server)
    const WEB_SERVER_ARCH = path.resolve(PROJECT_PATH, 'archetype', '**')

    this.npmInstall('trailpack-' + server, {
      save: true
    }, (err) => {
      if (err)
        return
      /* FIXME: return always wrong even the folders exists
       if (!this.fs.exists(PROJECT_PATH) || !this.fs.exists(path.resolve(PROJECT_PATH, 'archetype'))) {
       throw new Error('No archetype exist')
       }
       */
      this.fs.copy(WEB_SERVER_ARCH, dest)
    });
  },
  config () {
    this.fs.copy(path.resolve(TRAILS_TEMPLATE, 'config', '**'), this.destinationPath('config'))
  },
  root () {
    this.fs.copy(path.resolve(TRAILS_TEMPLATE, '.trailsrc'), this.destinationPath('.trailsrc'))
    this.fs.copy(path.resolve(TRAILS_TEMPLATE, 'index.js'), this.destinationPath('index.js'))
    this.fs.copy(path.resolve(TRAILS_TEMPLATE, 'server.js'), this.destinationPath('server.js'))
    this.fs.copy(path.resolve(TRAILS_TEMPLATE, 'api/index.js'), this.destinationPath('api/index.js'))
  },
  pkg () {
    // node:app generator will merge into this
    if (!this.options['skip-install']) {
      let trailsPackage = require(path.resolve(TRAILS_TEMPLATE, 'package.json'))
      this.fs.writeJSON(this.destinationPath('package.json'), trailsPackage)
    }

  }
};

@@ -543,204 +543,8 @@
 ne'%5D
-%0A    const dest = this.destinationPath()%0A    const PROJECT_PATH = this.destinationPath('node_modules/trailpack-' + server)%0A    const WEB_SERVER_ARCH = path.resolve(PROJECT_PATH, 'archetype', '**')
 %0A%0A  
@@ -655,127 +655,143 @@
 urn%0A
+%0A
       
-/* FIXME: return always wrong even the folders exists%0A       if (!this.fs.exists(PROJECT_PATH) %7C%7C !this.fs.exists
+const PROJECT_PATH = path.dirname(require.resolve('trailpack-' + server))%0A%0A      fs.accessSync(PROJECT_PATH)%0A      fs.accessSync
 (pat
@@ -831,21 +831,18 @@
 e'))
-) %7B%0A 
+%0A%0A
       th
 row 
@@ -841,103 +841,102 @@
   th
-row new Error('No archetype exist')%0A       %7D%0A       */%0A      this.fs.copy(WEB_SERVER_ARCH, dest
+is.fs.copy(path.resolve(PROJECT_PATH, 'archetype', '**'), this.destinationPath('api/policies')
 )%0A  

revert to 4f0abb44955ebf8ea61c7b0dfc5eb1281a2990f3