Skip to content

Commit

Permalink
Add script to update the api version (#843)
Browse files Browse the repository at this point in the history
* 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 059273c.
  • Loading branch information
rattrayalex-stripe authored Mar 20, 2020
1 parent a004973 commit 01c4800
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
1 change: 1 addition & 0 deletions API_VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2020-03-02
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
53 changes: 53 additions & 0 deletions scripts/updateAPIVersion.js
Original file line number Diff line number Diff line change
@@ -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',
'///<reference types=["\']\\.\\./API_VERSION[\'"]'
);
replaceAPIVersion(
'types/test/typescriptTest.ts',
'apiVersion: [\'"]API_VERSION[\'"]'
);
};

if (require.main === module) {
main();
}
36 changes: 0 additions & 36 deletions types/2020-03-02/Addresses.d.ts

This file was deleted.

0 comments on commit 01c4800

Please sign in to comment.