-
Notifications
You must be signed in to change notification settings - Fork 102
/
appc-npm
186 lines (145 loc) · 4.4 KB
/
appc-npm
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var pkg;
try {
pkg = JSON.parse(fs.readFileSync('package.json', {
encoding: 'utf-8'
}));
} catch (e) {
fail('Could not read package.json');
}
var appcNPM = pkg['appc-npm'];
if (typeof appcNPM !== 'object') {
fail('Could not find \'appc-npm\' in package.json');
}
var target = findTarget(__dirname);
if (!target) {
fail('Could not find project');
}
var targetPath = (typeof appcNPM.target === 'object') ? appcNPM.target[target.name] : appcNPM.target;
if (typeof targetPath !== 'string') {
fail('Could not find \'appc-npm.target.' + target.name + '\' or \'appc-npm.target\' in package.json');
}
var ignore = toArray(appcNPM.ignore);
ignore.push('appc-npm');
copySync(__dirname, path.join(target.path, targetPath), function (fullPath) {
var relPath = fullPath.substr(__dirname.length + 1);
return ignore.indexOf(relPath) === -1;
});
function findTarget(dir) {
if (fs.existsSync(path.join(dir, 'appc.json'))) {
return {
name: 'arrow',
path: dir
};
} else if (fs.existsSync(path.join(dir, 'app', 'controllers', 'index.js'))) {
return {
name: 'alloy',
path: dir
};
} else if (fs.existsSync(path.join(dir, 'tiapp.xml'))) {
return {
name: 'titanium',
path: dir
};
}
dirUp = path.resolve(dir, '..', '..');
if (!dirUp || dirUp === dir) {
return;
}
return findTarget(dirUp);
}
function fail(err) {
console.error(err);
process.exit(1);
}
function toArray(val) {
if (typeof val === 'string') {
return [val];
} else if (Object.prototype.toString.call(val) === '[object Array]') {
return val;
} else {
return [];
}
}
/* jshint ignore:start */
// https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy-sync.js
function copySync (src, dest, options) {
if (typeof options === 'function' || options instanceof RegExp) {
options = {filter: options}
}
options = options || {}
options.recursive = !!options.recursive
// default to true for now
options.clobber = 'clobber' in options ? !!options.clobber : true
options.filter = options.filter || function () { return true }
var stats = options.recursive ? fs.lstatSync(src) : fs.statSync(src)
var destFolder = path.dirname(dest)
var destFolderExists = fs.existsSync(destFolder)
var performCopy = false
if (stats.isFile()) {
if (options.filter instanceof RegExp) performCopy = options.filter.test(src)
else if (typeof options.filter === 'function') performCopy = options.filter(src)
if (performCopy) {
if (!destFolderExists) mkdirsSync(destFolder)
copyFileSync(src, dest, options.clobber)
}
} else if (stats.isDirectory()) {
if (!fs.existsSync(dest)) mkdirsSync(dest)
var contents = fs.readdirSync(src)
contents.forEach(function (content) {
copySync(path.join(src, content), path.join(dest, content), {filter: options.filter, recursive: true})
})
} else if (options.recursive && stats.isSymbolicLink()) {
var srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
}
}
// https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy-file-sync.js
function copyFileSync (srcFile, destFile, clobber) {
if (fs.existsSync(destFile) && !clobber) {
throw Error('EEXIST')
}
// simplified to work with vanilla fs
fs.createReadStream(srcFile).pipe(fs.createWriteStream(destFile));
}
// https://github.com/jprichardson/node-fs-extra/blob/master/lib/mkdirs/mkdirs.js
var o777 = parseInt('0777', 8)
function mkdirsSync (p, opts, made) {
if (!opts || typeof opts !== 'object') {
opts = { mode: opts }
}
var mode = opts.mode
var xfs = opts.fs || fs
if (mode === undefined) {
mode = o777 & (~process.umask())
}
if (!made) made = null
p = path.resolve(p)
try {
xfs.mkdirSync(p)//, mode) <!-- failed
made = made || p
} catch (err0) {
switch (err0.code) {
case 'ENOENT' :
made = mkdirsSync(path.dirname(p), opts, made)
mkdirsSync(p, opts, made)
break
// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
var stat
try {
stat = xfs.statSync(p)
} catch (err1) {
throw err0
}
if (!stat.isDirectory()) throw err0
break
}
}
return made
}
/* jshint ignore:end */