-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
122 lines (105 loc) · 4.16 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
const through = require('through2');
const PluginError = require('plugin-error');
const PLUGIN_NAME = 'gulp-package-version-format';
module.exports = function(arg) {
// wild card
let wildcard = 'x';
if ( typeof arg !== 'undefined'){
if ( arg.wildcard.match(/^[*|x|X]$/) ) {
wildcard = arg.wildcard;
}
}
/**
* @this {Transform}
*/
var transform = function(file, encoding, callback) {
// ファイルが指定されているかチェックする
if (file.isNull()) {
return callback(null, file);
}
// ストリームはサポートしない
if (file.isStream()) {
return callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
// メイン処理
const dataJson = JSON.parse(file.contents.toString('utf8'));
const depNames = ['dependencies','devDependencies','peerDependencies','optionalDependencies','bundledDependencies'];
depNames.forEach( function(depName) {
for(var pacName in dataJson[depName]){
dataJson[depName][pacName] = versionFormat(dataJson[depName][pacName], wildcard);
}
});
//json整形
file.contents = Buffer.from( JSON.stringify( dataJson, null, 2 ) + '\n' );
this.push(file);
callback();
};
return through.obj(transform);
};
function versionFormat( version, wildcard='x') {
// wild card set
if ( !wildcard.match(/^[*|x|X]$/) ) {
wildcard = 'x';
}
// Skip if revisions are specified
if ( (version.match( /\./g ) || [] ).length >= 3) {
return version;
}
if ( version.match(/^\^/) ) {
if ( version.match(/\^0\.0\..+$/) ){
//^0.0.5 -> 0.0.5
return version.replace(/^\^0\.0\.(.+)$/, '0.0.'+'$1' );
} else if ( version.match(/^\^0\.\d+\..+$/) ) {
//^0.5.5 -> 0.5.x
return version.replace(/^\^0\.(\d+)..+$/, '0.'+'$1'+'.'+ wildcard );
} else if ( version.match(/^\^\d+\.\d+\..+$/) ) {
//^5.5.5 -> 5.x.x
return version.replace(/^\^(\d+)\.\d+\..+$/, '$1'+'.'+ wildcard +'.'+ wildcard );
} else if ( version.match(/^\^\d+$/) ) {
//^5 -> 5.x.x
return version.replace(/^\^(\d+)$/, '$1'+'.'+ wildcard +'.'+ wildcard );
} else if ( version.match(/^\^0\.\d+$/) ) {
//^0.5 -> 0.5.x
return version.replace(/^\^(0\.\d+)$/, '$1'+'.'+ wildcard );
} else if ( version.match(/^\^\d+\.\d+$/) ) {
//^5.5 -> 5.x.x
return version.replace(/^\^(\d+)\.\d+$/, '$1'+'.'+ wildcard +'.'+ wildcard );
} else {
return version;
}
} else if ( version.match(/^~/) ) {
if ( version.match(/~\d+$/) ) {
//~5 -> 5.x.x
return version.replace(/~(\d+)$/, '$1'+'.'+ wildcard +'.'+ wildcard );
} else if ( version.match(/~0\.\d+$/) ) {
//0.5 -> 0.5.x
return version.replace(/~(0\.\d+)$/, '$1'+'.'+ wildcard );
} else if ( version.match(/~\d+\.\d+$/) ) {
//5.5 -> 5.5.x
return version.replace(/~(\d+\.\d+)$/, '$1'+'.'+ wildcard );
} else {
return version;
}
} else {
if ( version.match(/^\d+\.[*|x|X]\.[*|x|X]$/) ){
//5.x.x -> 5.*.*
return version.replace(/^(\d+)\.[*|x|X]\.[*|x|X]$/, '$1'+'.'+ wildcard +'.'+ wildcard );
} else if ( version.match(/^\d+\.\d+.[*|x|X]$/) ) {
//5.5.x -> 5.5.*
return version.replace(/^(\d+\.\d+)\.[*|x|X]$/, '$1'+'.'+ wildcard );
} else if ( version.match(/^\d+$/) ) {
//5 -> 5.x.x
return version.replace(/^(\d+)$/, '$1'+'.'+ wildcard +'.'+ wildcard );
} else if ( version.match(/^0\.\d+$/) ) {
//0.5 -> 0.5.x
return version.replace(/^(0\.\d+)$/, '$1'+'.'+ wildcard );
} else if ( version.match(/^\d+\.\d+$/) ) {
//5.5 -> 5.5.x
return version.replace(/^(\d+\.\d+)$/, '$1'+'.'+ wildcard );
} else {
return version;
}
}
}
module.exports.versionFormat = versionFormat;