forked from necccc/npm-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm_utils.js
154 lines (108 loc) · 4.17 KB
/
npm_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
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
const npm = require('npm')
const mv = require('mv')
const curry = require('lodash.curry')
const async = require('async')
const publishAsync = function (registry, path, callback) {
npm.load({
registry: registry
}, () => {
let tgz = path + '.tgz'
npm.commands.publish([tgz], (err, data) => {
if (err) return callback(err);
callback(null, path)
})
})
}
const getTarball = function (moduleName, registry, version, callback) {
const versionedModule = [moduleName, version].join('@')
npm.load({
registry: registry
}, () => {
npm.commands.pack([versionedModule], (err, data) => {
if (err) return callback(err);
const tarballEscaped = versionedModule
.replace(/^@/, '') // remove first @
.replace('@','-') // change @0.0.0 to -0.0.0
.replace('/', '-'); // change scope/ to scope-
const tarball = `${tarballEscaped}.tgz`;
const tarballFrom = process.cwd() + '/' + tarball
const tarballTo = process.cwd() + '/npm-migrate_tmp/' + tarball
mv(tarballFrom, tarballTo, { mkdirp: true }, (err) => {
if (err) return callback(err);
callback(null, tarballTo)
})
})
})
}
function getRemainingVersions (moduleName, oldRegistry, newRegistry, oldRegistryVersions) {
return new Promise((resolve) => {
npm.config.set('registry', newRegistry);
npm.commands.info([moduleName], (err, data) => {
if (err) {
return resolve(oldRegistryVersions);
}
const latest = Object.keys(data)[0];
const newRegistryVersions = data[latest].versions;
console.log('New Registry Versions', newRegistryVersions);
remainingVersions = oldRegistryVersions.filter((v) => !newRegistryVersions.includes(v));
if (!remainingVersions.length) {
console.log('No more versions of this package to migrate');
} else {
console.log('Remaining Versions to Migrate', remainingVersions);
}
resolve(remainingVersions);
});
npm.config.set('registry', oldRegistry);
});
}
module.exports.getVersionList = function (moduleName, oldRegistry, newRegistry) {
return new Promise((resolve, reject) => {
npm.load({
registry: oldRegistry
}, () => {
npm.commands.info([moduleName], function (err, data) {
if (err) return reject(err);
const latest = Object.keys(data)[0];
const oldRegistryVersions = data[latest].versions;
console.log('Old Registry Versions', oldRegistryVersions);
return getRemainingVersions(moduleName, oldRegistry, newRegistry, oldRegistryVersions)
.then((remainingVersions) => {
resolve(remainingVersions);
});
})
})
})
}
module.exports.getTarballs = function (moduleName, registry, versions) {
let curried_getTarball = curry(getTarball)
let series = versions.map((version) => curried_getTarball(moduleName, registry, version))
return new Promise((resolve, reject) => {
async.series(
series,
(err, results) => {
if (err) return reject(err);
resolve(results)
})
})
}
module.exports.publishSeries = function (registry, packageFolders) {
let curried_publishAsync = curry(publishAsync),
series = packageFolders.map((folder) => curried_publishAsync(registry, folder))
return new Promise((resolve, reject) => {
async.series(
series,
(err, results) => {
if (err) return reject(err);
resolve(results)
})
})
}
const packageFromPath = function (path) {
return path
.replace(process.cwd() + '/npm-migrate_tmp/', '')
.replace('/package', '')
}
module.exports.packageFromPaths = function (paths) {
return paths.map(packageFromPath)
}
module.exports.packageFromPath = packageFromPath;