react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / install / main.js
81164 viewsvar assert = require("assert");1var fs = require("fs");2var path = require("path");3var file = path.join(__dirname, "install.js");45exports.makeGlobal = function() {6require("./install");7};89function Reader(file) {10var self = this;11assert.ok(self instanceof Reader);1213var args;14var qhead = {};15var qtail = qhead;1617fs.readFile(file, "utf8", function(err, data) {18args = [err, data];19process.nextTick(flush);20});2122function flush() {23var next = qhead.next24if (next && args) {25qhead = next;26process.nextTick(flush);27next.cb.apply(null, args);28}29}3031self.addCallback = function(cb) {32qtail = qtail.next = { cb: cb };33if (qhead.next === qtail)34process.nextTick(flush);35};36}3738var reader;3940exports.getCode = function(callback) {41reader = reader || new Reader(file);42reader.addCallback(callback);43};4445function rename(installName, code) {46if (installName !== "install")47code = code.replace(48/\bglobal\.install\b/g,49"global." + installName);50return code;51}5253exports.renameCode = function(installName, callback) {54reader = reader || new Reader(file);55reader.addCallback(function(err, data) {56callback(err, rename(installName, data));57});58};5960function getCodeSync() {61return fs.readFileSync(file, "utf8");62}63exports.getCodeSync = getCodeSync;6465exports.renameCodeSync = function(installName) {66return rename(installName, getCodeSync());67};6869// Not perfect, but we need to match the behavior of install.js.70var requireExp = /\brequire\(['"]([^'"]+)['"]\)/g;7172// This function should match the behavior of `ready` and `absolutize` in73// install.js, but the implementations are not worth unifying because we have74// access to the "path" module here.75exports.getRequiredIDs = function(id, source) {76var match, seen = {}, ids = [];7778requireExp.lastIndex = 0;79while ((match = requireExp.exec(source))) {80var rid = match[1];81if (rid.charAt(0) === ".")82rid = path.normalize(path.join(id, "..", match[1]));8384if (!seen.hasOwnProperty(rid)) {85seen[rid] = true;86ids.push(rid);87}88}8990return ids;91};929394