-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
81 lines (64 loc) · 2.46 KB
/
index.js
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
'use strict';
var path = require('path')
, fs = require('fs')
, semver = require('semver')
, injectNodeGyp = require('./lib/inject-node_gyp')
, injectNodeExtensions = require('./lib/inject-node_extensions')
, copyFileSync = require('./lib/copy-file-sync')
var node_0_10 = semver.Range('>= 0.10.0 < 0.11')
, node_greater_0_10 = semver.Range('>= 0.11.13')
function getFiles(nodeDir) {
return { node_gyp_file : path.join(nodeDir, 'node.gyp')
, node_extensions_h_file : path.join(nodeDir, 'src', 'node_extensions.h')
, node_extensions_cc_file : path.join(nodeDir, 'src', 'node_extensions.cc') }
}
function backup(file) {
var copy = file + '.orig'
if (!fs.existsSync(copy)) copyFileSync(file, copy);
}
function restore(file) {
var copy = file + '.orig'
if (fs.existsSync(copy)) copyFileSync(copy, file);
}
function inject_0_10(projectDir, nodeDir, cb) {
var files = getFiles(nodeDir);
backup(files.node_gyp_file)
backup(files.node_extensions_h_file)
backup(files.node_extensions_cc_file)
injectNodeGyp(projectDir, nodeDir, onnodegyped);
function onnodegyped(err, res) {
if (err) return cb(err);
injectNodeExtensions(
files.node_extensions_h_file
, files.node_extensions_cc_file
, res.extensions
, cb)
}
}
function inject_greater_0_10(projectDir, nodeDir, cb) {
var files = getFiles(nodeDir);
backup(files.node_gyp_file)
injectNodeGyp(projectDir, nodeDir, cb);
}
function restore_0_10(nodeDir) {
var files = getFiles(nodeDir);
restore(files.node_gyp_file)
restore(files.node_extensions_h_file)
restore(files.node_extensions_cc_file)
}
function restore_greater_0_10(nodeDir) {
var files = getFiles(nodeDir);
restore(files.node_gyp_file)
}
exports.inject = function inject(nodeVersion, projectDir, nodeDir, cb) {
if (node_0_10.test(nodeVersion)) return inject_0_10(projectDir, nodeDir, cb);
if (node_greater_0_10.test(nodeVersion)) return inject_greater_0_10(projectDir, nodeDir, cb);
cb(new Error('nad only works for node 0.10 and greater'));
}
exports.restore = function restore(nodeVersion, nodeDir) {
if (node_0_10.test(nodeVersion)) return restore_0_10(nodeDir);
if (node_greater_0_10.test(nodeVersion)) return restore_greater_0_10(nodeDir);
throw new Error('nad only works for node 0.10 and greater');
}
exports.injectNodeGyp = injectNodeGyp;
exports.injectNodeExtensions = injectNodeExtensions;