forked from pilwon/ultimate-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm-scripts.js
95 lines (78 loc) · 2.08 KB
/
npm-scripts.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
/*
* npm-scripts.js
*/
'use strict';
var exec = require('child_process').exec,
fs = require('fs'),
path = require('path'),
util = require('util');
var project = require('./project');
var commands = {
prepublish: prepublish,
postpublish: postpublish,
preinstall: preinstall,
postinstall: postinstall,
preuninstall: preuninstall,
postuninstall: postuninstall,
preupdate: preupdate,
postupdate: postupdate
};
var grey = function (text) {
return '\x1B[90m' + text + '\x1B[39m';
};
function prepublish() {
console.log(grey('npm prepublish script executed'));
}
function postpublish() {
console.log(grey('npm postpublish script executed'));
}
function preinstall() {
console.log(grey('npm preinstall script executed'));
}
function postinstall() {
// Generate config files from samples if they don't already exist.
['development', 'production', 'staging'].forEach(function (env) {
var from = path.join(project.path.config, env + '.sample.json'),
to = path.join(project.path.config, env + '.json');
if (!fs.existsSync(to)) {
exec(util.format('cp -u %s %s', from, to), function (err) {
if (err) {
console.error(err);
process.exit(1);
}
});
}
});
console.log(grey('npm postinstall script executed'));
}
function preuninstall() {
console.log(grey('npm preuninstall script executed'));
}
function postuninstall() {
console.log(grey('npm postuninstall script executed'));
}
function preupdate() {
console.log(grey('npm preupdate script executed'));
}
function postupdate() {
console.log(grey('npm postupdate script executed'));
}
function usage() {
console.error();
console.error(' Usage:');
console.error();
Object.keys(commands).forEach(function (command) {
console.error(grey(util.format(' node %s %s',
path.basename(__filename, '.js'),
command)));
});
console.error();
process.exit(1);
}
function main() {
if (process.argv.length !== 3 || !commands.hasOwnProperty(process.argv[2])) {
usage();
}
commands[process.argv[2]]();
}
main();