mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
This allows it to be required without having any side effects which is needed for the clean task that just wanted to remove the cache directory.
38 lines
1.1 KiB
CoffeeScript
38 lines
1.1 KiB
CoffeeScript
crypto = require 'crypto'
|
|
fs = require 'fs'
|
|
path = require 'path'
|
|
|
|
CoffeeScript = require 'coffee-script'
|
|
CSON = require 'season'
|
|
mkdir = require('mkdirp').sync
|
|
|
|
cacheDir = '/tmp/atom-compile-cache'
|
|
coffeeCacheDir = path.join(cacheDir, 'coffee')
|
|
CSON.setCacheDir(path.join(cacheDir, 'cson'))
|
|
|
|
getCachePath = (coffee) ->
|
|
digest = crypto.createHash('sha1').update(coffee, 'utf8').digest('hex')
|
|
path.join(coffeeCacheDir, "#{digest}.coffee")
|
|
|
|
getCachedJavaScript = (cachePath) ->
|
|
if stat = fs.statSyncNoException(cachePath)
|
|
try
|
|
fs.readFileSync(cachePath, 'utf8') if stat.isFile()
|
|
|
|
compileCoffeeScript = (coffee, filePath, cachePath) ->
|
|
js = CoffeeScript.compile(coffee, filename: filePath)
|
|
try
|
|
mkdir(path.dirname(cachePath))
|
|
fs.writeFileSync(cachePath, js)
|
|
js
|
|
|
|
requireCoffeeScript = (module, filePath) ->
|
|
coffee = fs.readFileSync(filePath, 'utf8')
|
|
cachePath = getCachePath(coffee)
|
|
js = getCachedJavaScript(cachePath) ? compileCoffeeScript(coffee, filePath, cachePath)
|
|
module._compile(js, filePath)
|
|
|
|
module.exports =
|
|
cacheDir: cacheDir
|
|
register: -> require.extensions['.coffee'] = requireCoffeeScript
|