-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (112 loc) · 3.43 KB
/
index.js
File metadata and controls
122 lines (112 loc) · 3.43 KB
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
var fs = require("fs");
var isGitRepo = require("is-git-repo");
var childProcess = require("child_process");
var path = require("path");
function getPackageJson(dirPath) {
return new Promise((resolve, reject) => {
fs.readFile(path.join(dirPath, "package.json"), (err, data) => {
if (err) return reject(err);
resolve(JSON.parse(data));
});
});
}
function isGitRepoPromise(path) {
return new Promise((resolve) => {
isGitRepo(path, resolve);
});
}
function getGitHeadSha(path) {
return new Promise((resolve, reject) => {
var gitPath = process.platform === "win32" ? "git.EXE" : "git";
childProcess.exec(
gitPath + " rev-parse HEAD",
{
cwd: path,
},
function (err, stdout) {
if (err) return reject(err);
var sha = stdout.trim();
resolve(sha);
}
);
});
}
function startWith(string, start) {
return string.slice(0, start.length) === start;
}
function isGitDependency(depVersion) {
return (
startWith(depVersion, "git+") ||
startWith(depVersion, "github") ||
startWith(depVersion, "git://")
);
}
function updateGitDependencies(pkgJson, rootPath, opts = {}) {
const pkgDependenciesPath = opts.devDependencies
? "devDependencies"
: "dependencies";
var dependencies = pkgJson[pkgDependenciesPath];
var gitDependencies = Object.keys(dependencies).filter((depName) =>
isGitDependency(dependencies[depName])
);
console.log("git dependencies", gitDependencies);
return Promise.all(
gitDependencies.map((depName) => {
var depVersion = dependencies[depName];
var repoPath = path.join(rootPath, depName);
return isGitRepoPromise(repoPath).then((res) => {
if (!res) {
console.log("no local git repo found for", depName, "at", repoPath);
return null;
}
console.log("local git repo found for", depName, "at", repoPath);
return getGitHeadSha(repoPath).then((sha) => {
console.log(depName, "HEAD sha is", sha);
var depVersionStart = depVersion.split("#")[0];
return [depVersionStart, sha].join("#");
});
});
})
).then((latestVersions) => {
latestVersions.forEach((latestVersion, i) => {
if (typeof latestVersion === "string") {
var depName = gitDependencies[i];
if (pkgJson[pkgDependenciesPath][depName] !== latestVersion) {
pkgJson[pkgDependenciesPath][depName] = latestVersion;
console.log(depName, "updated to", latestVersion);
} else {
console.log(depName, "already uptodate");
}
}
});
return pkgJson;
});
}
function writePackageJson(pkgJson, pkgPath) {
return new Promise((resolve, reject) => {
fs.writeFile(
path.join(pkgPath, "package.json"),
JSON.stringify(pkgJson, null, 2),
(err) => {
if (err) return reject(err);
resolve();
}
);
});
}
exports.getPackageJson = getPackageJson;
exports.isGitRepo = isGitRepoPromise;
exports.getGitHeadSha = getGitHeadSha;
exports.default = function (packagePath) {
packagePath = packagePath || process.cwd();
return getPackageJson(packagePath)
.then((pkgJson) =>
updateGitDependencies(pkgJson, path.join(packagePath, "node_modules"))
)
.then((pkgJson) =>
updateGitDependencies(pkgJson, path.join(packagePath, "node_modules"), {
devDependencies: true,
})
)
.then((pkgJson) => writePackageJson(pkgJson, packagePath));
};