Skip to content

Commit

Permalink
Fix client.version to reflect @apollo/client version from package.json (
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn authored Dec 10, 2020
1 parent 98b6ee1 commit 172fb3c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
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

0 comments on commit 172fb3c

Please sign in to comment.