<commit_before>{$, ScrollView} = require 'atom-space-pen-views'
pathM = require 'path'
desc = require '../utils/text-description'
util = require '../utils/util'

module.exports =
class ChameleonBuilderView extends ScrollView
  @content: ->
    @iframe outlet: 'iframeContainer', class: 'builder-iframe', src: ''

  getURI: -> @uri

  getTitle: ->
    @uri.replace('atom://', '')

  initialize: (options) ->
    @uri = options.uri
    @appConfig = options.appConfig

  attached: ->
    util.eventEmitter().on 'server_on', (e)=>
      @.attr 'src', e
       .on 'load', ()=>
        window.frames[0].postMessage JSON.stringify(@appConfig), e

    getBuilderConfig = (e) =>
      builderConfig = JSON.parse e.data
      @appConfig.builderConfig = builderConfig
      console.log @appConfig
      if @appConfig.projectInfo?
        @createProject @appConfig
      else
        @createModule @appConfig
      window.removeEventListener 'message', getBuilderConfig, false

    window.addEventListener 'message', getBuilderConfig, false

  createModule: (options) ->
    util.createModule options, (err) =>
      return console.error err if err?
      console.log 'success'

  createProject: (options) ->
    info = options.projectInfo
    Util.createDir info.appPath, (err) =>
      if err
        console.error err
        alert "应用创建失败#{':权限不足' if err.code is 'EACCES'}"
      else
        appConfigPath = pathM.join info.appPath,desc.projectConfigFileName
        appConfig = Util.formatAppConfigToObj(info)
        util.createModule options, (err) =>
          return console.error err if err?
          console.log 'success'
          <commit_msg>no message<commit_after>{$, ScrollView} = require 'atom-space-pen-views'
pathM = require 'path'
desc = require '../utils/text-description'
util = require '../utils/util'
_ = require 'underscore-plus'

module.exports =
class ChameleonBuilderView extends ScrollView
  @content: ->
    @iframe outlet: 'iframeContainer', class: 'builder-iframe', src: ''

  getURI: -> @uri

  getTitle: ->
    @uri.replace('atom://', '')

  initialize: (options) ->
    @uri = options.uri
    @appConfig = options.appConfig

  attached: ->
    util.eventEmitter().on 'server_on', (e)=>
      @.attr 'src', e
       .on 'load', ()=>
        window.frames[0].postMessage JSON.stringify(@appConfig), e

    getBuilderConfig = (e) =>
      builderConfig = JSON.parse e.data
      @appConfig.builderConfig = builderConfig
      console.log @appConfig
      if @appConfig.projectInfo?
        @createProject @appConfig
      else
        @createModule @appConfig
      window.removeEventListener 'message', getBuilderConfig, false

    window.addEventListener 'message', getBuilderConfig, false

  createModule: (options) ->
    util.createModule options, (err) =>
      return console.error err if err?
      console.log 'success'

  createProject: (options) ->
    info = options.projectInfo
    Util.createDir info.appPath, (err) =>
      if err
        console.error err
        alert "应用创建失败#{':权限不足' if err.code is 'EACCES'}"
      else
        appConfigPath = pathM.join info.appPath,desc.projectConfigFileName
        appConfig = Util.formatAppConfigToObj(info)
        util.createModule options, (err) =>
          return console.error err if err?
          console.log 'success'
          moduleInfo = options.moduleInfo
          moduleId = moduleInfo.identifier
          appConfig.mainModule = moduleId
          appConfig.modules[moduleId] = desc.minVersion
          Util.writeJson appConfigPath, appConfig, (err) =>
            throw err if err
            atom.workspace.open appConfigPath
            aft = =>
              Util.rumAtomCommand('tree-view:reveal-active-file')
            _.debounce(aft,300)
          alert desc.createAppSuccess
          atom.project.addPath(info.appPath)
          Util.rumAtomCommand 'tree-view:toggle' if $('.tree-view-resizer').length is 0
{'fraction_non_alphanumeric': 0.12155260469867211, 'fraction_numerical': 0.0015321756894790602, 'mean_word_length': 2.7163187855787476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3850299', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1144, 'n_words': 265}