Welcome to mirror list, hosted at ThFree Co, Russian Federation.

install.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aaf96395b602104a548225831e8153e3bb47f303 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// npm install command

var npm = require("../npm"),
  utils = require("./utils"),
  method = utils.method,
  fail = utils.fail,
  succeed = utils.succeed,
  bind = utils.bind,
  queue = require("./utils/queue").queue,
  fetch = require("./utils/fetch"),
  copy = require("./utils/copy"),
  sys = require("sys"),
  posix = require("posix"),
  path = require("path");

module.exports = install;

function install (tarball, name) {
  
  var ROOT = path.join(process.ENV.HOME, ".node_libraries"),
    packageRoot = path.join(ROOT, name)
    targetDir = path.join(packageRoot, "package"),
    targetTgz = path.join(targetDir, name+".tgz"),
    p = new process.Promise;
  
  // if the targetDir already exists, then abort.
  posix.stat(targetDir)
    .addCallback(fail(p, targetDir + " exists."))
    .addErrback(function () {
      if (tarball.match(/^https?:\/\//)) {
        return fetchAndInstall(tarball, name)
          .addCallback(succeed(p))
          .addErrback(fail(p, "fetchAndInstall failed"));
      }
      // install from a file.
      if (tarball.indexOf("file://") === 0) tarball = tarball.substr("file://".length);
      
      posix.stat(tarball)
        .addErrback(fail(p, "tarball not found: "+tarball))
        .addCallback(function () {
          queue([
            // make the directory
            ensureDir(targetDir, 0755),
            // copy the tarball into it.
            bind(copy, null, tarball, targetTgz),
            // cd into that
            method(process, "chdir", targetDir),
            // unpack
            unpackTar(targetTgz),
            // read the json
            readJson(path.join(targetDir, "package.json"), name),
            // link up the "main", if there is one.
            linkMain(name, path.join(packageRoot, "index.js"), targetDir),
            // run the "make", if there is one.
            runMake(name)
            // success!
          ], function (f) { return f() })
            .addCallback(succeed(p, "successfully installed "+name))
            .addErrback(fail(p));
        });
    });
  return p;
};


function fetchAndInstall (tarball, name) {
  var p = new process.Promise;
  
  var target = path.join("/tmp", "npm-"+name+"-"+tarball.replace(/[^a-zA-Z0-9]/g, "_")+".tgz");
  fetch(tarball, target)
    .addErrback(fail(p, "Could not fetch "+tarball+" to "+target))
    .addCallback(function () {
      install(target, name)
        .addCallback(succeed(p, "Succeeded from within fetchAndInstall"))
        .addErrback(fail(p, "Failed from within fetchAndInstall"));
    });
  
  return p;
};

function ensureDir (dir, chmod) { return function () {
  var dirs = dir.split("/"),
    walker = [""];
  return queue(dirs, function (d) {
    var p = new process.Promise;
    walker.push(d);
    var dir = walker.join("/");
    posix.stat(dir)
      .addErrback(function () {
        posix.mkdir(dir, chmod)
          .addCallback(succeed(p))
          .addErrback(fail(p, "Failed to mkdir ["+walker.join("/")+"]"));
      })
      .addCallback(function (s) {
        if (s.isDirectory()) p.emitSuccess();
        else p.emitError("Failed to mkdir "+dir+": File exists");
      });
    return p;
  });
}};

// TODO: Do the unpacking in JS somehow, maybe?
function unpackTar (tarball) { return function () {
  utils.log("about to unpack "+tarball+" in the cwd of "+process.cwd());
  var p = new process.Promise;
  process.createChildProcess("tar", ["xzvf", tarball, "--strip", "1"])
    .addListener("error", function (chunk) {
      if (chunk) process.stdio.writeError(chunk)
    })
    .addListener("output", function (chunk) {
      if (chunk) process.stdio.write(chunk)
    })
    .addListener("exit", function (code) {
      p[code ? "emitError" : "emitSuccess"](code);
    });
  return p;
}};

function readJson (jsonFile, name) { return function () {
  var p = new process.Promise;
  posix.cat(jsonFile)
    .addErrback(fail(p, "Could not read "+jsonFile))
    .addCallback(function (json) {
      try {
        json = JSON.parse(json);
        npm.set(name, json);
        p.emitSuccess();
      } catch (ex) {
        p.emitError("Failed to parse json\n"+ex.message);
      }
    });
  return p;
}};

function linkMain (name, proxyFile, packageRoot) { return function () {
  var data = npm.get(name);
  if (!data.main) return;
  var p = new process.Promise;
  // write out an index.js at ROOT/<name>/index.js
  // which proxies to ROOT/<name>/package/<main>
  var code =
    "// generated by npm, please don't touch!\n"+
    "module.exports=require("+
      JSON.stringify(path.join(packageRoot, data.main)) +
    ");\n";
  posix.open(proxyFile,
      process.O_CREAT | process.O_EXCL | process.O_WRONLY, 0755)
    .addErrback(fail(p, "Could not open "+proxyFile))
    .addCallback(function (fd) {
      posix.write(fd, code, "ascii")
        .addErrback(fail(p, "Could not write to "+proxyFile))
        .addCallback(succeed(p));
    });
  return p;
}};

function runMake (name) { return function () {
  var data = npm.get(name);
  if (!data.make) return;
  var p = new process.Promise;
  sys.exec(data.make)
    .addErrback(fail(p, "Failed to run make command: "+data.make))
    .addCallback(succeed(p));
  return p;
}};