/
grab.js
91 lines (80 loc) · 2.3 KB
/
grab.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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const watch = require('watch')
const copy = require('ncp').ncp
const rimraf = require('rimraf')
const Getopt = require('node-getopt')
const curDir = process.cwd()
const getopt = new Getopt([
[ 'w', 'watch', 'Watch the files for changes.' ],
[ 'r', 'remove', 'Remove the files for clean reinstall.' ]
])
getopt.setHelp(
'Usage: node grab.js repository [repository+] [OPTION]\n' +
'grabs all files but the node_modules and files starting with a ".".\n' +
'\n' +
'[[OPTIONS]]'
)
getopt.parseSystem()
if (getopt.argv.length < 1) {
getopt.showHelp()
process.exit(0)
}
function removeStatusFunc (name) {
return function (err) {
if (err) {
console.error('Error removing ' + name)
console.error(err)
} else {
console.log('Done removing ' + name)
}
}
}
function copyStatusFunc (name) {
return function (err) {
if (err) {
console.error('Error copying ' + name)
console.error(err)
} else {
console.log('Done copying ' + name)
}
}
}
for (let reponame of getopt.argv) {
let nodeModulePath = path.join(curDir, 'node_modules', reponame)
let sourceRepoPath = path.join(curDir, '..', reponame)
if (!fs.existsSync(nodeModulePath)) {
throw new Error('please install package once before grabbing to ensure that all dependencies are satisfied')
}
if (!getopt.options.remove) {
console.log('copying ' + reponame)
const filterRegEx = new RegExp(reponame + '(.(?!node_modules)[^.]|$)')
copy(sourceRepoPath, nodeModulePath, {
filter: filterRegEx,
stopOnError: true
}, copyStatusFunc(reponame))
if (getopt.options.watch) {
watch.watchTree(sourceRepoPath, {
filter: function (name) {
return filterRegEx.test(name)
}
}, function (f, curr, prev) {
if (typeof f === 'object') {
console.log('watching ' + reponame)
} else {
const t = path.join(nodeModulePath, path.relative(sourceRepoPath, f))
if (curr.nlink === 0) {
rimraf(t, removeStatusFunc(f))
} else {
copy(f, t, copyStatusFunc(f))
}
}
})
}
} else {
console.log('removing ' + reponame)
rimraf(nodeModulePath, removeStatusFunc(reponame))
}
}