Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix client.version to reflect @apollo/client version. #7448

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Apollo Client 3.3.5

### Improvements

- Restore `client.version` property, reflecting the current `@apollo/client` version from `package.json`. <br/>
[@benjamn](https://github.com/benjamn) in [#7448](https://github.com/apollographql/apollo-client/pull/7448)

## Apollo Client 3.3.4

### Improvements
Expand Down
66 changes: 66 additions & 0 deletions config/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.js"));

// 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.js file is generated from ESM modules
// generated by tsc, including dist/version.js, so verifying core.cjs.js
// 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.js",
);

break;
}

default:
throw new Error(
"Pass either 'update' or 'verify' to config/version.js"
);
}

console.log("ok");
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"scripts": {
"prebuild": "npm run clean",
"build": "tsc",
"postbuild": "npm run invariants && npm run sourcemaps && npm run rollup && npm run prepdist && npm run resolve",
"postbuild": "npm run update-version && npm run invariants && npm run sourcemaps && npm run rollup && npm run prepdist && npm run resolve && npm run verify-version",
"update-version": "node config/version.js update",
"verify-version": "node config/version.js verify",
"invariants": "ts-node-script config/processInvariants.ts",
"sourcemaps": "ts-node-script config/rewriteSourceMaps.ts",
"rollup": "rollup -c ./config/rollup.config.js",
Expand Down