-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix client.version to reflect @apollo/client version from package.json (
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters