Skip to content
This repository was archived by the owner on Jun 1, 2020. It is now read-only.

Commit edf7849

Browse files
committed
sync with auto-update.js
1 parent bfa8419 commit edf7849

2 files changed

Lines changed: 48 additions & 30 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"gift": "^0.10",
2525
"glob": "^7.1.4",
2626
"is-there": "^4.4.4",
27+
"chmodr": "^1.2.0",
2728
"lodash": "^4.17.15",
2829
"semver-stable": "^2.0.4"
2930
},

updaters/npm.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var _ = require('lodash');
66
var path = require('path');
77
var request = require('superagent');
88
var tarball = require('tarball-extract');
9+
var chmodr = require('chmodr');
910

1011
var assert = require('assert');
1112
var fs = require('fs-extra');
@@ -113,85 +114,101 @@ var getPackageTempPath = function(pkg, version) {
113114
return path.normalize(path.join(__dirname, '../', GIT_REPO_LOCAL_FOLDER, pkg.name, version));
114115
};
115116

116-
var processNewVersion = function(library, version) {
117+
var processNewVersion = function (pkg, version) {
118+
var npmName = pkg.autoupdate.target;
117119

118120
// sometimes the tar is extracted to a dir that isnt called 'package' - get that dir via glob
119-
var extractLibPath = glob.sync(getPackageTempPath(library, version) + "/*/")[0];
120-
var npmName = library.autoupdate.target;
121+
var extractLibPath = glob.sync(getPackageTempPath(pkg, version) + '/*/')[0];
121122

122123
if (!extractLibPath) {
123124
// even more rarely, the tar doesnt seem to get extracted at all.. which is probably a bug in that lib.
124-
var msg = npmName + "@" + version +
125-
" - never got extracted! This problem usually goes away on next run." +
126-
" Couldnt find extract dir here: " + getPackageTempPath(library, version);
127-
console.log(msg.red);
125+
var msg = npmName + '@' + version +
126+
' - never got extracted! This problem usually goes away on next run.' +
127+
' Couldnt find extract dir here: ' + getPackageTempPath(pkg, version);
128+
console.log(msg.error);
128129
return;
129130
}
130131

131-
var libPath = getPackagePath(library, version);
132+
// trick to handle wrong permission lib like clipboard.js@0.0.7
133+
fs.chmodSync(extractLibPath, 0755);
134+
chmodr.sync(extractLibPath, 0755);
135+
136+
var libPath = getPackagePath(pkg, version);
132137
var isAllowedPath = isAllowedPathFn(extractLibPath);
133138
var newPath = path.join(libPath, 'package.json');
134-
135-
if (isThere(newPath)) { // turn this off for now
139+
if (fs.existsSync(newPath)) { // turn this off for now
136140
var newPkg = parse(newPath);
137141
if (isValidFileMap(newPkg)) {
138-
library.npmFileMap = newPkg.npmFileMap;
142+
pkg.npmFileMap = newPkg.npmFileMap;
139143
}
140144
}
141145

142-
var npmFileMap = library.npmFileMap;
146+
var npmFileMap = pkg.autoupdate.fileMap;
143147
var errors = [];
144148
var updated = false;
145-
_.each(npmFileMap, function(fileSpec) {
146-
var basePath = fileSpec.basePath || "";
147-
148-
_.each(fileSpec.files, function(file) {
149+
_.each(npmFileMap, function (fileSpec) {
150+
var basePath = fileSpec.basePath || '';
151+
if (fileSpec.files.length === 0) {
152+
fs.mkdirsSync(libPath);
153+
return;
154+
}
155+
_.each(fileSpec.files, function (file) {
149156
var libContentsPath = path.normalize(path.join(extractLibPath, basePath));
150157
if (!isAllowedPath(libContentsPath)) {
151-
errors.push(error(npmName + " contains a malicious file path: " +
158+
errors.push(error(npmName + ' contains a malicious file path: ' +
152159
libContentsPath, error.FILE_PATH));
153160
return;
154161
}
155-
var files = glob.sync(path.join(libContentsPath, file));
162+
163+
var files = glob.sync(path.join(libContentsPath, file), { nodir: true });
156164
if (files.length === 0) {
157165
// usually old versions have this problem
158166
var msg;
159-
msg = (npmName + "@" + version + " - couldnt find file in npmFileMap.") +
160-
(" Doesnt exist: " + path.join(libContentsPath, file)).info;
167+
msg = (npmName + '@' + version + ' - couldnt find file in npmFileMap.') +
168+
(' Doesnt exist: ' + path.join(libContentsPath, file)).info;
161169
fs.mkdirsSync(libPath);
162170
console.log(msg);
163171
}
164172

165-
_.each(files, function(extractFilePath) {
166-
if (extractFilePath.match(/(dependencies|\.zip\s*$)/i)) {
173+
_.each(files, function (extractFilePath) {
174+
if (extractFilePath.match(/(\.zip\s*$)/i)) {
167175
return;
168176
}
177+
169178
var copyPart = path.relative(libContentsPath, extractFilePath);
170179
var copyPath = path.join(libPath, copyPart);
171-
fs.mkdirsSync(path.dirname(copyPath));
172-
fs.copySync(extractFilePath, copyPath);
180+
if (fs.statSync(extractFilePath).size !== 0){
181+
// don't copy the empty file from the source
182+
fs.mkdirsSync(path.dirname(copyPath));
183+
fs.copySync(extractFilePath, copyPath);
184+
fs.chmodSync(copyPath, '0644');
185+
} else {
186+
console.log('Warning! '.warn + copyPart.gray + ' is empty, file will not be copied!');
187+
}
173188
updated = true;
174189
});
175190
});
176191
});
192+
177193
if (updated) {
178194
newVersionCount++;
179-
var libPatha = path.normalize(path.join(__dirname, 'ajax', 'libs', library.name, 'package.json'));
195+
var libPatha = path.normalize(path.join(__dirname, 'ajax', 'libs', pkg.name, 'package.json'));
180196
console.log('------------'.red, libPatha.green);
181197
if (
182-
(!library.version) ||
198+
(!pkg.version) ||
183199
(
184-
semver.gt(version, library.version) &&
200+
semver.gt(version, pkg.version) &&
185201
(
186202
stable.is(version) ||
187-
(!stable.is(version) && !stable.is(library.version))
203+
(!stable.is(version) && !stable.is(pkg.version))
188204
)
189205
)
190206
) {
191-
library.version = version;
192-
fs.writeFileSync(libPatha, JSON.stringify(library, null, 2) + '\n', 'utf8');
207+
pkg.version = version;
208+
fs.writeFileSync(libPatha, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
193209
}
194210
}
211+
195212
return errors;
196213
};
197214

0 commit comments

Comments
 (0)