-
Notifications
You must be signed in to change notification settings - Fork 0
/
dist.js
executable file
·209 lines (178 loc) · 5.39 KB
/
dist.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const child = require('child_process');
const readline = require('readline');
// FIXME: more opts:
// - --crate-name <crate>
// - --package <package>
// - --crate-type <type>
function parseArgs() {
const [ _node, _script, ...args ] = process.argv;
let fromCross = false;
let outfile = null;
let crateName = null;
for (let i = 0; i < args.length; i++) {
if (args[i] === "--from-cross") {
fromCross = true;
continue;
}
if (outfile) {
throw new Error("too many arguments");
}
outfile = args[i];
}
if (!outfile) {
outfile = "index.node";
}
if (!crateName) {
crateName = require(path.join(process.cwd(), "package.json")).name;
}
return { fromCross, outfile, crateName };
}
function usage() {
console.error("usage: neon-dist [options] [<outfile>]");
console.error(" --from-cross normalize paths from cross-rs output");
console.error(" <outfile> output file to copy artifact to (default: index.node)");
}
function die(msg) {
usage();
console.error();
console.error(msg);
process.exit(1);
}
function normalize(abs) {
console.error("spawning cross metadata for normalization");
const result = child.spawnSync("cross", ["metadata", "--format-version=1", "--no-deps"], { shell: true });
console.error("child process complete.");
const metadata = JSON.parse(result.stdout);
console.error("metadata:");
console.error(metadata);
const rel = path.relative(path.dirname(metadata.target_directory), abs);
console.error(`absolute path: ${abs}`);
console.error(`relativized path: ${rel}`);
return rel;
}
function processEvent(event, opts) {
const index = event.target.crate_types.indexOf('cdylib');
if (index < 0) {
die(`no library artifact found for ${opts.crateName}`);
}
const abs = event.filenames[index];
console.error(`manifest_path=${event.manifest_path}`);
console.error(`dirname(manifest_path)=${path.dirname(event.manifest_path)}`);
console.error(`abs=${abs}`);
const filename = opts.fromCross ? normalize(abs) : abs;
// FIXME: needs all the logic of cargo-cp-artifact (timestamp check, M1 workaround, async, errors)
fs.copyFileSync(filename, opts.outfile);
}
function parseLine(line) {
try {
const data = JSON.parse(line.trim());
if ((typeof data === 'object') && ('reason' in data)) {
return data;
}
} catch (ignore) { }
return null;
}
/*
function processCargoMetadata(metadata, opts) {
const sub = metadata.find(event => {
return (event.reason === 'compiler-artifact') &&
(event.target.name === opts.crateName);
});
if (!sub) {
die(`no artifact created for ${opts.crateName}`);
}
const index = sub.target.crate_types.indexOf('cdylib');
if (index < 0) {
die(`no library artifact found for ${opts.crateName}`);
}
const abs = sub.filenames[index];
console.error(`manifest_path=${sub.manifest_path}`);
console.error(`dirname(manifest_path)=${path.dirname(sub.manifest_path)}`);
console.error(`abs=${abs}`);
const filename = opts.fromCross ? normalize(abs) : abs;
// FIXME: needs all the logic of cargo-cp-artifact (timestamp check, M1 workaround, async, errors)
fs.copyFileSync(filename, opts.outfile);
}
*/
async function go() {
try {
console.error("starting up neon-dist");
const opts = parseArgs();
console.error("creating stdin stream");
process.stdin.resume();
process.stdin.setEncoding('utf-8');
const rl = readline.createInterface({
input: process.stdin,
terminal: false
});
console.error("created stderr stream");
for await (const line of rl) {
console.error("reading a line");
const event = parseLine(line);
console.error("read a line");
if ((event.reason === 'compiler-artifact') && (event.target.name === opts.crateName)) {
console.error("found the right even, processing");
processEvent(event, opts);
console.error("processed event, closing stdin stream");
break;
}
}
setTimeout(() => {
rl.terminal = false;
rl.close();
console.error("exiting");
process.stdin.unref();
process.exit(0);
}, 0);
} catch (e) {
die(e.message);
}
}
go();
/*
try {
console.error("starting up neon-dist");
const opts = parseArgs();
console.error("creating stdin stream");
process.stdin.resume();
process.stdin.setEncoding('utf-8');
const rl = readline.createInterface({
input: process.stdin,
terminal: false
});
console.error("created stderr stream");
rl.on('line', line => {
try {
console.error("reading a line");
const event = parseLine(line);
console.error("read a line");
if ((event.reason === 'compiler-artifact') && (event.target.name === opts.crateName)) {
console.error("found the right even, processing");
processEvent(event, opts);
console.error("processed event, closing stdin stream");
rl.close();
console.error("exiting");
process.stdin.unref();
process.exit(0);
}
} catch (e) {
die(e.message);
}
});
*/
/*toString(process.stdin, (err, data) => {
if (err) {
die(err.message);
}
const metadata = data.split(/\r?\n/)
.map(line => line.trim())
.filter(line => line)
.map(line => JSON.parse(line));
processCargoMetadata(metadata, opts);
});*/
/*} catch (e) {
die(e.message);
}*/