This repository has been archived by the owner on Jun 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildInfo.js
152 lines (133 loc) · 3.67 KB
/
buildInfo.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
/**
* Created by sam on 13.11.2016.
*/
const spawnSync = require('child_process').spawnSync;
const path = require('path');
const resolve = path.resolve;
const fs = require('fs');
function dependencyGraph(cwd) {
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const r = spawnSync(npm, ['ls', '--prod', '--json'], {
cwd: cwd
});
if (!r.stdout) {
console.error(cwd, r.error);
return {};
}
return JSON.parse(r.stdout.toString());
}
function gitHead(cwd) {
if (!fs.existsSync(cwd + '/.git')) {
return null;
}
const r = spawnSync('git', ['rev-parse', '--verify', 'HEAD'], {
cwd: cwd
});
if (!r.stdout) {
console.error(cwd, r.error);
return {};
}
return r.stdout.toString().trim();
}
function resolveModules() {
const reg = fs.readFileSync('../phovea_registry.js').toString();
const regex = /import '(.*)\/phovea_registry.js'/g;
const modules = [];
var r;
while ((r = regex.exec(reg)) !== null) {
modules.push(r[1]);
}
return modules;
}
function cleanupDependency(d) {
return d;
}
function resolveWorkspace() {
console.log('resolve parent');
const workspaceDeps = dependencyGraph('..').dependencies;
const modules = new Set(resolveModules());
const resolveModule = (m) => {
console.log('resolve', m);
const pkg = require(`../${m}/package.json`);
const head = gitHead('../' + m);
const repo = pkg.repository.url;
return {
name: pkg.name,
version: pkg.version,
resolved: head ? `${repo.endsWith('.git') ? repo.slice(0, repo.length-4) : repo}/commit/${head}` : pkg.version,
dependencies: deps(pkg.dependencies)
};
};
const deps = (deps) => {
const r = {};
Object.keys(deps).forEach((d) => {
if (d in workspaceDeps) {
r[d] = cleanupDependency(workspaceDeps[d]);
delete workspaceDeps[d];
} else if (modules.has(d)) {
modules.delete(d);
r[d] = resolveModule(d);
} else {
r[d] = '-> link';
}
});
return r;
};
// self =
const root = path.basename(process.cwd());
console.log(root);
modules.delete(root);
const base = resolveModule(root);
base.extraDependencies = {};
while (modules.size > 0) {
let m = Array.from(modules.keys())[0];
modules.delete(m);
base.extraDependencies[m] = resolveModule(m);
}
return base;
}
function resolveSingle() {
console.log('resolve self');
const self = dependencyGraph('.');
const pkg = require(`./package.json`);
const head = gitHead('.');
const deps = {};
Object.keys(self.dependencies || {}).forEach((d) => {
deps[d] = cleanupDependency(self.dependencies[d]);
});
return {
name: self.name,
version: pkg.version,
resolved: head ? `${pkg.repository.url}#${head}` : pkg.version,
dependencies: deps,
extraDependencies: {}
};
}
function generate() {
console.log('building buildInfo');
const isWorkspaceContext = fs.existsSync('../phovea_registry.js');
if (isWorkspaceContext) {
return resolveWorkspace();
} else {
return resolveSingle();
}
}
const IS_WINDOWS = process.platform === 'win32';
function tmpdir() {
if (IS_WINDOWS) {
return process.env.TEMP || process.env.TMP ||
(process.env.SystemRoot || process.env.windir) + '\\temp';
} else {
return process.env.TMPDIR || process.env.TMP || process.env.TEMP || '/tmp';
}
}
module.exports.generate = generate;
module.exports.tmpFile = function() {
const s = generate();
const file = `${tmpdir()}/buildInfo${Math.random().toString(36).slice(-8)}.txt`;
fs.writeFileSync(file, JSON.stringify(s, null, ' '));
return file;
}
if (require.main === module) {
fs.writeFile('deps.json', JSON.stringify(generate(), null, ' '));
}