-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.js
106 lines (82 loc) · 3.31 KB
/
utils.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
var path = require('path');
var fs = require('fs');
var config = require( __dirname + '/./config' );
var Promise = require('bluebird');
var _exec = Promise.promisify( require('child_process').exec );
var REGISTRY_NAME = config.REGISTRY_NAME;
var LOCAL_REGISTRY = config.LOCAL_REGISTRY;
var NPM_PATH = config.NPM_PATH;
function exec( cmd, envVars ){
return _exec( cmd, { env: envVars });
}
exports.exec = exec;
function fileExists( fname ){
return new Promise( function( resolve ){
fs.exists( fname, function(exists){
resolve( exists );
});
});
}
exports.fileExists = fileExists;
var readFile = Promise.promisify( fs.readFile );
exports.readFile = readFile;
exports.patchData = function ( data ){
/* Get the list of versions which is available in local cache */
var cacheJsonFile, cacheJsonFileData = [];
if( config.STRICT ){
cacheJsonFile = path.join( NPM_PATH, data.name );
cacheJsonFileData = fs.existsSync( cacheJsonFile ) ? fs.readdirSync( cacheJsonFile ) : [];
}
Object.keys(data.versions).forEach( function( v ){
var val = data.versions[v];
if( cacheJsonFileData.length && config.STRICT ){
/* Suppose our cache.json is at latest revision. It contains lot of versions which is not available in local cache.
Then, Remove the versions which is not in local cache from the list. Otherwise npm will always choose higher versions whcih is not available in our cache */
if( cacheJsonFileData.indexOf(v) == -1 ){
delete data.versions[v];
return;
}
}
var protocal = 'http://';
if( val.dist.tarball.indexOf( 'https:' ) !== false ){
protocal = 'https://';
}
val.dist.tarball = val.dist.tarball.replace( protocal + REGISTRY_NAME, LOCAL_REGISTRY );
});
};
var fetchAndCacheMetadataCmd =[
'mkdir -p $packageCacheDir',
'wget -nv "http://$REGISTRY_NAME/$packageName" -O $cacheFile || { wgetExitStatus=$? && rm $cacheFile; exit $wgetExitStatus ; }'
].join( ';' );
var fetchAndCacheTarballCmd = [
'mkdir -p $packageTarballDir',
'wget -nv $tarballUrl -O $tarballPath || { wgetExitStatus=$? && rm $tarballPath; exit $wgetExitStatus ; }',
'cd $packageTarballDir; tar -xzf package.tgz package/package.json',
].join( ';' );
function encodePackageName( packageName ){
//handle slash in scoped package name but do not convert @
return encodeURIComponent(packageName).replace(/^%40/, '@');
}
exports.fetchAndCacheMetadata = function ( packageName, cacheFile ){
var packageCacheDir = path.dirname( cacheFile );
packageName = encodePackageName( packageName );
return exec( fetchAndCacheMetadataCmd, {
packageCacheDir: packageCacheDir,
REGISTRY_NAME: REGISTRY_NAME,
packageName: packageName,
cacheFile: cacheFile,
});
};
exports.fetchAndCacheTarball = function ( packageName, version, tarballPath, scopeName ){
packageName = encodePackageName( packageName );
var tarballUrl = 'http://' + REGISTRY_NAME + '/' + packageName + '/-/' + packageName + '-' + version + '.tgz';
if (scopeName) {
tarballUrl = 'http://' + REGISTRY_NAME + '/' + scopeName + '/' + packageName + '/-/' + packageName + '-' + version + '.tgz';
}
var packageTarballDir = path.dirname( tarballPath );
return exec( fetchAndCacheTarballCmd, {
packageTarballDir: packageTarballDir,
tarballUrl: tarballUrl,
tarballPath: tarballPath,
});
};