Skip to content

Instantly share code, notes, and snippets.

@elisee
Created November 10, 2014 18:32
Show Gist options
  • Save elisee/ebe992cbfe4c5aac8a5f to your computer and use it in GitHub Desktop.
Save elisee/ebe992cbfe4c5aac8a5f to your computer and use it in GitHub Desktop.
Script to fix prototype.constructor for all derived classes in THREE.js
readdirp = require 'readdirp'
path = require 'path'
_ = require 'lodash'
async = require 'async'
fs = require 'fs'
readdirp { root: path.join( __dirname, '..' ), fileFilter: [ '*.js' ] }, (err, res) ->
return console.log err if err?
files = _.filter res.files, (x) -> x.fullPath.indexOf('node_modules') == -1 and x.fullPath.indexOf('build') == -1 and x.fullPath.indexOf('test') == -1
console.log "Scanning #{files.length} JS files..."
file = _.find files, (x) -> x.fullPath.indexOf('AWDLoader') != -1
async.each files, (file, callback) ->
fs.readFile file.fullPath, { encoding: 'utf8' }, (err, content) ->
lines = content.split '\n'
inserts = []
for line, i in lines
pos = line.indexOf('.prototype = Object.create(')
continue if pos == -1
if i + 1 < lines.length and lines[i+1].indexOf('prototype.constructor') != -1
console.log "Skipping line #{i} of #{file.path}:"
console.log "found prototype.constructor in the next line"
continue
start = Math.max(line.lastIndexOf(' ', pos - 1), line.lastIndexOf('\t', pos - 1)) + 1
className = line.substring(start, pos)
###console.log "Found at line #{i} of #{file.path}:"
console.log line###
tabs = ''
for x in line
break if x != '\t'
tabs += '\t'
# console.log "Found #{tabs.length} tabs at beginning of line"
inserts.push { line: i + 1, content: "#{tabs}#{className}.prototype.constructor = #{className};" }
return callback() if inserts.length == 0
console.log "Inserting #{inserts.length} lines in #{file.path}"
for insert in inserts.reverse()
lines.splice insert.line, 0, insert.content
fs.writeFile "#{file.fullPath}", lines.join('\n'), { encoding: 'utf8' }, callback
, (err) ->
console.log 'Done!'
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment