Skip to content

Commit

Permalink
chore: make release script more stable
Browse files Browse the repository at this point in the history
- read talisman version from constants instead of package.json
- throw error if jscodeshift failed to change files
  • Loading branch information
pgmanutd committed Sep 24, 2020
1 parent edc7f59 commit 1a6efb9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"prettier": "^2.1.2",
"replace-in-file": "^6.1.0",
"ts-jest": "^26.4.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.3"
},
"@pika/pack": {
Expand Down
30 changes: 22 additions & 8 deletions scripts/release/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint-disable no-console, @typescript-eslint/no-var-requires, import/no-extraneous-dependencies */

require('ts-node').register();

const fs = require('fs');

const request = require('request');
const execShPromise = require('exec-sh').promise;
const replace = require('replace-in-file');

const pkg = require('../../package.json');
const { META_INFO } = require('../../src/constants');

const pkgVersion = pkg.version;
const oldVersion = META_INFO.version.replace('v', '');

const publishNewTag = async ({ newVersion }) => {
await execShPromise(`npm run publish -- ${newVersion} --no-release-draft`, {
Expand All @@ -18,19 +22,29 @@ const commitChanges = async ({ newVersion }) => {
await execShPromise(
[
'git add --all',
`git commit -m "build(tw-talisman): upgrade from ${pkgVersion} to ${newVersion}" -n`,
`git commit -m "build(tw-talisman): upgrade from ${oldVersion} to ${newVersion}" -n`,
],
{ cwd: process.cwd() },
);
};

const replaceChecksumsInFiles = async ({ checksums }) => {
const stringifiedChecksums = JSON.stringify(checksums);
const cwd = process.cwd();
const file = 'src/constants.ts';

const originalStats = fs.statSync(`${cwd}/${file}`);

await execShPromise(
`npx jscodeshift --parser=ts --extensions=ts --transform scripts/release/transform.js src/constants.ts --checksums='${stringifiedChecksums}'`,
`npx jscodeshift --parser=ts --extensions=ts --transform scripts/release/transform.js ${file} --checksums='${stringifiedChecksums}'`,
{ cwd: process.cwd() },
);

const newStats = fs.statSync(`${cwd}/${file}`);

if (originalStats.mtimeMs === newStats.mtimeMs) {
throw new Error('Failed to replace checksum');
}
};

const replaceVersionsInFiles = ({ tagName }) => {
Expand All @@ -39,7 +53,7 @@ const replaceVersionsInFiles = ({ tagName }) => {
try {
const changedFiles = replace.sync({
files: [`${cwd}/README.md`, `${cwd}/src/constants.ts`],
from: new RegExp(`v${pkgVersion}`, 'g'),
from: new RegExp(`v${oldVersion}`, 'g'),
to: tagName,
});

Expand Down Expand Up @@ -97,7 +111,7 @@ const getNextTagName = () => {
(error, { body, statusCode }) => {
if (!error && statusCode === 200) {
const index = body.findIndex(({ tag_name: tagName }) =>
tagName.includes(pkgVersion),
tagName.includes(oldVersion),
);

if (index === 0) {
Expand Down Expand Up @@ -139,10 +153,10 @@ const getNextTagName = () => {
],
});

replaceVersionsInFiles({ tagName });

await replaceChecksumsInFiles({ checksums });

replaceVersionsInFiles({ tagName });

await commitChanges({ newVersion });

await publishNewTag({ newVersion });
Expand Down

0 comments on commit 1a6efb9

Please sign in to comment.