From 01c4800a7536e95dc57782133e1c8a058605ed31 Mon Sep 17 00:00:00 2001 From: "Alex Rattray (Stripe)" Date: Fri, 20 Mar 2020 13:19:58 -0700 Subject: [PATCH] Add script to update the api version (#843) * Add script to update the api version * Delete Addresses.d.ts, which is obsolete - Address lives in shared * Script refactors * DEMO: make-believe 2020-12-12 API Version * Revert "DEMO: make-believe 2020-12-12 API Version" This reverts commit 059273c2200d880ac81c12f3a617acaf7d6da703. --- API_VERSION | 1 + package.json | 2 +- scripts/updateAPIVersion.js | 53 +++++++++++++++++++++++++++++++++ types/2020-03-02/Addresses.d.ts | 36 ---------------------- 4 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 API_VERSION create mode 100755 scripts/updateAPIVersion.js delete mode 100644 types/2020-03-02/Addresses.d.ts diff --git a/API_VERSION b/API_VERSION new file mode 100644 index 0000000000..f3688a0931 --- /dev/null +++ b/API_VERSION @@ -0,0 +1 @@ +2020-03-02 diff --git a/package.json b/package.json index 75c98c2685..9115b19d44 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "test": "yarn lint && yarn test-typescript && yarn mocha", "test-typescript": "tsc --build types/test", "lint": "eslint --ext .js,.jsx,.ts .", - "fix": "yarn lint --fix", + "fix": "yarn lint --fix && ./scripts/updateAPIVersion.js", "report": "nyc -r text -r lcov report", "coveralls": "cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" } diff --git a/scripts/updateAPIVersion.js b/scripts/updateAPIVersion.js new file mode 100755 index 0000000000..4c0e1c6034 --- /dev/null +++ b/scripts/updateAPIVersion.js @@ -0,0 +1,53 @@ +#!/usr/bin/env node + +/* eslint-disable no-sync,no-nested-ternary */ +const fs = require('fs'); +const path = require('path'); + +const read = (file) => fs.readFileSync(path.resolve(file)).toString(); +const write = (file, str) => fs.writeFileSync(path.resolve(file), str); +const edit = (file, cb) => write(file, cb(read(file))); + +const API_VERSION = '2[0-9][2-9][0-9]-[0-9]{2}-[0-9]{2}'; + +const main = () => { + const apiVersion = read('API_VERSION').trim(); + + const replaceAPIVersion = (file, pattern) => + edit(file, (text) => { + const parts = pattern.split('API_VERSION'); + return text.replace( + new RegExp(parts.map((x) => `(${x})`).join(API_VERSION), 'g'), + parts.length === 0 + ? apiVersion + : parts.length === 1 + ? `$1${apiVersion}` + : parts.length === 2 + ? `$1${apiVersion}$2` + : 'UNEXPECTED' + ); + }); + + replaceAPIVersion('README.md', 'apiVersion: [\'"]API_VERSION[\'"]'); + replaceAPIVersion('package.json', '"types": "types/API_VERSION/index.d.ts'); + replaceAPIVersion( + 'examples/webhook-signing/typescript-node-express/express-ts.ts', + 'apiVersion: [\'"]API_VERSION[\'"]' + ); + replaceAPIVersion( + 'types/lib.d.ts', + 'export type LatestApiVersion = [\'"]API_VERSION[\'"]' + ); + replaceAPIVersion( + 'types/test/typescriptTest.ts', + '///