This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.js
118 lines (98 loc) · 2.83 KB
/
setup.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
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
/**
* Cross-platform node.js postinstall & test script for coffeescript projects.
*
* Adapted from setup.js provided by Brunch.
*/
var exec = require("child_process").exec,
sysPath = require("path"),
fs = require("fs"),
mode = process.argv[2],
fsExists = fs.exists || sysPath.exists,
fsExistsSync = fs.existsSync || sysPath.existsSync,
FILE_ENCODING = "utf-8",
EOL = "\n";
var getBinaryPath = function (binary) {
var path;
if (fsExistsSync(path = sysPath.join("node_modules", ".bin", binary))) {
return path;
}
if (fsExistsSync(path = sysPath.join("..", ".bin", binary))) {
return path;
}
return binary;
};
var execute = function (script, params, callback) {
var path = getBinaryPath(script),
command = path + " " + params;
console.log("Executing", command);
exec(command, function (error, stdout, stderr) {
if (error !== null) {
return process.stderr.write(stderr.toString());
}
console.log(stdout.toString());
if (callback && typeof callback === "function") {
callback();
}
});
};
var concat = function (opts) {
var fileList = opts.src,
distPath = opts.dest,
out = fileList.map(function (filePath) {
return fs.readFileSync(filePath, FILE_ENCODING);
});
fs.writeFileSync(distPath, out.join(EOL), FILE_ENCODING);
console.log(" " + distPath + " built.");
};
var compileCoffee = function () {
execute("coffee", "-bc -o build/ src/", function () {
fs.exists("./bin", function (exists) {
if (!exists) {
fs.mkdirSync("./bin");
}
concat({
src: ["src/hashbang.txt", "build/sow.js"],
dest: "bin/sow"
});
});
});
};
var togglePostinstall = function (add) {
var pkg = require("./package.json");
if (add) {
if (!pkg.scripts) {
pkg.scripts = {};
}
pkg.scripts.postinstall = "node setup.js postinstall";
} else if (pkg.scripts && pkg.scripts.postinstall) {
delete pkg.scripts.postinstall;
}
fs.writeFileSync("package.json", JSON.stringify(pkg, null, 4) + "\n");
};
switch (mode) {
// Remove `.postinstall` script to prevent stupid npm bugs.
case "prepublish":
togglePostinstall(false);
compileCoffee();
break;
// Bring back `.postinstall` script.
case "postpublish":
togglePostinstall(true);
break;
// Compile coffeescript for git users.
case "postinstall":
fsExists(sysPath.join(__dirname, "build"), function (exists) {
if (exists) {
return;
}
compileCoffee();
});
break;
// Run tests.
case "test":
execute(
"mocha",
"--compilers coffee:coffee-script --require test/common.coffee --colors"
);
break;
}