Skip to content

Commit

Permalink
Add support for localised config .languagebabel - Closes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
gandm committed Sep 12, 2015
1 parent b05ff00 commit e03c01b
Showing 1 changed file with 69 additions and 15 deletions.
84 changes: 69 additions & 15 deletions lib/transpiler.coffee
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
fs = require 'fs-plus'
path = require 'path'
pathIsInside = require 'path-is-inside'
merge = require('lodash/object/merge')

This comment has been minimized.

Copy link
@bolinfest

bolinfest Sep 21, 2015

This and other dependencies are adding a non-trivial amount to the load time for this package.

This comment has been minimized.

Copy link
@gandm

gandm Sep 22, 2015

Owner

@bolinfest Not sure what is going on. Even if I have only any one require removing all the others the package load time is still 150+ms on my CPU.
image

image

This comment has been minimized.

Copy link
@gandm

gandm Sep 22, 2015

Owner

I'm not sure I trust timecop. Never seems to show any coffeescript files being compiled even though I removed the cache!

This comment has been minimized.

Copy link
@bolinfest

bolinfest via email Sep 22, 2015

This comment has been minimized.

Copy link
@gandm

gandm Sep 22, 2015

Owner

I've removed all requires in transpile.coffee. Makes no difference!

This comment has been minimized.

Copy link
@bolinfest

bolinfest via email Sep 23, 2015

This comment has been minimized.

Copy link
@bolinfest

bolinfest Sep 23, 2015

Let's move this conversation back over to #64 where it's easier to track.


class Transpiler
constructor: ->
@transpileErrorNotifications = {}
# setup JSON Schema to parse .languagebabel configs
@jsonSchema = (require 'jjv')() # use jjv as it runs without CSP issues
@jsonSchema.addSchema('localConfig', {
type: 'object',
properties: {
babelMapsPath: { type: 'string' },
babelMapsAddUrl: { type: 'boolean' },
babelSourcePath: { type: 'string' },
babelTranspilePath: { type: 'string' },
createMap: { type: 'boolean' },
createTargetDirectories: { type: 'boolean' },
createTranspiledCode: { type: 'boolean' },
disableWhenNoBabelrcFileInPath: { type: 'boolean' },
suppressSourcePathMessages: { type: 'boolean' },
suppressTranspileOnSaveMessages: { type: 'boolean' },
transpileOnSave: { type: 'boolean' }
},
additionalProperties: false
})
@deprecateConfig()

# transpile sourceFile edited by the optional textEditor
transpile: (sourceFile, textEditor) ->
config = @getConfig()
return if config.transpileOnSave isnt true
pathTo = @getPaths sourceFile, config

localConfig = @getLocalConfig pathTo.sourceFileDir, pathTo.projectPath, {}
# merge local configs with global. local wins
merge config, localConfig
# recalc paths
pathTo = @getPaths sourceFile, config

pathTo = @getPaths(sourceFile, config)
return if config.transpileOnSave isnt true

if config.disableWhenNoBabelrcFileInPath
if not @isBabelrcInPath pathTo.sourceFileDir, path.parse(pathTo.sourceFileDir).root
Expand Down Expand Up @@ -117,25 +143,42 @@ class Transpiler
babelOptions.whitelist = config.whitelistTransformers
return babelOptions

# check for prescence of a .babelrc file path fromDir toDir
isBabelrcInPath: (fromDir, toDir) ->
# enviromnents used in babelrc
babelrc = '.babelrc'
babelrcFile = path.join fromDir, babelrc
if fs.existsSync babelrcFile
return true
if fromDir isnt toDir
return @isBabelrcInPath path.dirname(fromDir), toDir
else return false

# get configuration for language-babel
# get global configuration for language-babel
getConfig: -> atom.config.get('language-babel')

# check for prescence of a .languagebabel file path fromDir toDir
# read, validate and overwrite config as required
getLocalConfig: (fromDir, toDir, localConfig) ->
# get local path overides
localConfigFile = '.languagebabel'
languageBabelCfgFile = path.join fromDir, localConfigFile
if fs.existsSync languageBabelCfgFile
fileContent= fs.readFileSync languageBabelCfgFile, 'utf8'
try
jsonContent = JSON.parse fileContent
catch err
atom.notifications.addError "#{localConfigFile} #{err.message}",
dismissable: true
detail: "File = #{languageBabelCfgFile}\n\n#{fileContent}"
return
schemaErrors = @jsonSchema.validate 'localConfig', jsonContent
if schemaErrors
atom.notifications.addError "#{localConfigFile} Schema Error",
dismissable: true
detail: "File = #{languageBabelCfgFile}\n\n#{fileContent}"
else
# merge local config. config closest sourceFile wins
merge jsonContent, localConfig
localConfig = jsonContent
if fromDir isnt toDir
return @getLocalConfig path.dirname(fromDir), toDir, localConfig
else return localConfig

# calculate absoulte paths of babel source, target js and maps files
# based upon the project directory containing the source
# and the roots of source, transpile path and maps paths defined in config
getPaths: (sourceFile, config) ->
projectContainingSource = atom.project.relativizePath(sourceFile)
projectContainingSource = atom.project.relativizePath sourceFile
# if a project is in the root directory atom passes back a null for
# the project path. We need the real root
if projectContainingSource[0] is null
Expand All @@ -161,4 +204,15 @@ class Transpiler
sourceRoot: absSourceRoot
projectPath: absProjectPath

# check for prescence of a .babelrc file path fromDir toDir
isBabelrcInPath: (fromDir, toDir) ->
# enviromnents used in babelrc
babelrc = '.babelrc'
babelrcFile = path.join fromDir, babelrc
if fs.existsSync babelrcFile
return true
if fromDir isnt toDir
return @isBabelrcInPath path.dirname(fromDir), toDir
else return false

exports.Transpiler = Transpiler

0 comments on commit e03c01b

Please sign in to comment.