Transpile assets in-place

This commit is contained in:
Antonio Scandurra
2016-07-27 16:09:49 +02:00
parent 7ca4145eb9
commit d4795ab562
4 changed files with 27 additions and 41 deletions

View File

@@ -3,11 +3,11 @@
'use strict'
const cleanOutputDirectory = require('./lib/clean-output-directory')
const copyAssets = require('./lib/copy-assets')
const transpileBabelPaths = require('./lib/transpile-babel-paths')
const transpileCoffeeScriptPaths = require('./lib/transpile-coffee-script-paths')
const copyStaticAssets = require('./lib/copy-static-assets')
cleanOutputDirectory()
copyAssets()
transpileBabelPaths()
transpileCoffeeScriptPaths()
copyStaticAssets()

View File

@@ -7,16 +7,20 @@ const path = require('path')
const fs = require('fs-extra')
const computeDestinationPath = require('./compute-destination-path')
const CONFIG = require('../config')
const glob = require('glob')
module.exports = function () {
console.log('Copying static assets...');
const sourcePaths = [
path.join(CONFIG.repositoryRootPath, 'static'),
console.log('Copying assets...');
const srcPaths = [
path.join(CONFIG.repositoryRootPath, 'dot-atom'),
path.join(CONFIG.repositoryRootPath, 'exports'),
path.join(CONFIG.repositoryRootPath, 'node_modules'),
path.join(CONFIG.repositoryRootPath, 'static'),
path.join(CONFIG.repositoryRootPath, 'src'),
path.join(CONFIG.repositoryRootPath, 'vendor')
]
for (let srcPath of sourcePaths) {
srcPaths.concat(glob.sync(path.join(CONFIG.repositoryRootPath, 'spec', '*.*'), {ignore: '**/*-spec.*'}))
for (let srcPath of srcPaths) {
fs.copySync(srcPath, computeDestinationPath(srcPath))
}
}

View File

@@ -8,9 +8,7 @@ module.exports = transpileBabelPaths
const babel = require('babel-core')
const fs = require('fs')
const glob = require('glob')
const mkdirp = require('mkdirp')
const path = require('path')
const computeDestinationPath = require('./compute-destination-path')
const CONFIG = require('../config')
const BABEL_OPTIONS = require('../../static/babelrc.json')
@@ -25,9 +23,9 @@ const BUFFER = Buffer(PREFIX_LENGTH)
function transpileBabelPaths () {
console.log('Transpiling Babel paths...');
for (let srcPath of glob.sync(`${CONFIG.repositoryRootPath}/src/**/*.js`)) {
if (usesBabel(srcPath)) {
transpileBabelPath(srcPath, computeDestinationPath(srcPath))
for (let path of glob.sync(`${CONFIG.electronAppPath}/src/**/*.js`)) {
if (usesBabel(path)) {
transpileBabelPath(path)
}
}
}
@@ -40,16 +38,8 @@ function usesBabel (path) {
return BABEL_PREFIXES.indexOf(filePrefix) !== -1
}
function transpileBabelPath (srcPath, destPath) {
function transpileBabelPath (path) {
const options = Object.assign({}, BABEL_OPTIONS)
options.sourceFileName = path.relative(path.dirname(destPath), srcPath)
if (process.platform === 'win32') {
options.sourceFileName = options.sourceFileName.replace(/\\/g, '/')
}
options.sourceMapTarget = path.basename(destPath)
let result = babel.transformFileSync(srcPath, options)
mkdirp.sync(path.dirname(destPath))
fs.writeFileSync(destPath, result.code)
options.sourceMap = null
fs.writeFileSync(path, babel.transformFileSync(path, options).code)
}

View File

@@ -10,34 +10,26 @@ const mkdirp = require('mkdirp')
const path = require('path')
const CONFIG = require('../config')
const computeDestinationPath = require('./compute-destination-path')
const GLOBS = [
'src/**/*.coffee,spec/*.coffee',
'!spec/*-spec.coffee',
'exports/**/*.coffee',
'static/**/*.coffee'
]
module.exports =
function transpileCoffeeScriptPaths () {
console.log('Transpiling CoffeeScript paths...');
for (let srcPath of getPathsToTranspile()) {
transpileCoffeeScriptPath(srcPath, computeDestinationPath(srcPath).replace(/coffee$/, 'js'))
for (let path of getPathsToTranspile()) {
transpileCoffeeScriptPath(path)
}
}
function getPathsToTranspile () {
let paths = []
paths = paths.concat(glob.sync(`${CONFIG.repositoryRootPath}/src/**/*.coffee`))
paths = paths.concat(glob.sync(`${CONFIG.repositoryRootPath}/spec/*.coffee`, {ignore: '**/*-spec.coffee'}))
paths = paths.concat(glob.sync(`${CONFIG.repositoryRootPath}/exports/**/*.coffee`))
paths = paths.concat(glob.sync(`${CONFIG.electronAppPath}/src/**/*.coffee`))
paths = paths.concat(glob.sync(`${CONFIG.electronAppPath}/spec/*.coffee`, {ignore: '**/*-spec.coffee'}))
paths = paths.concat(glob.sync(`${CONFIG.electronAppPath}/exports/**/*.coffee`))
return paths
}
function transpileCoffeeScriptPath (srcPath, destPath) {
const inputCode = fs.readFileSync(srcPath, 'utf8')
let outputCode = coffee.compile(inputCode)
mkdirp.sync(path.dirname(destPath))
fs.writeFileSync(destPath, outputCode)
function transpileCoffeeScriptPath (coffeePath) {
const inputCode = fs.readFileSync(coffeePath, 'utf8')
const jsPath = coffeePath.replace(/coffee$/g, 'js')
fs.writeFileSync(jsPath, coffee.compile(inputCode))
fs.unlinkSync(coffeePath)
}