forked from apollographql/apollo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.js
66 lines (54 loc) · 1.94 KB
/
version.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
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const distRoot = path.join(__dirname, "..", "dist");
const versionPath = path.join(distRoot, "version.js");
const pkgJsonPath = path.join(__dirname, "..", "package.json");
const { version } = JSON.parse(fs.readFileSync(pkgJsonPath));
assert.strictEqual(
typeof version, "string",
'"version" field missing from package.json',
);
switch (process.argv[2]) {
case "update": {
const updated = fs
.readFileSync(versionPath, "utf8")
.replace(/\blocal\b/, version);
assert.notEqual(
updated.indexOf(version), -1,
"Failed to update dist/version.js with @apollo/client version",
);
fs.writeFileSync(versionPath, updated);
break;
}
case "verify": {
const {
ApolloClient,
InMemoryCache,
} = require(path.join(distRoot, "core", "core.cjs"));
// Though this may seem like overkill, verifying that ApolloClient is
// constructible in Node.js is actually pretty useful, too!
const client = new ApolloClient({
cache: new InMemoryCache,
});
// Probably not necessary, but it seems wise to clean up any resources
// the client might have acquired during its construction.
client.stop();
// The CommonJS dist/core/core.cjs file is generated from ESM modules
// generated by tsc, including dist/version.js, so verifying core.cjs
// exports an ApolloClient class that defines client.version also serves to
// verify that dist/version.js must have been correctly updated, which is
// convenient because dist/version.js uses ECMAScript module syntax, and is
// thus not importable in all versions of Node.js.
assert.strictEqual(
client.version, version,
"Failed to update dist/version.js and dist/core/core.cjs",
);
break;
}
default:
throw new Error(
"Pass either 'update' or 'verify' to config/version.js"
);
}
console.log("ok");