Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler] Refactor release script #30614

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
"snap": "yarn workspace babel-plugin-react-compiler run snap",
"snap:build": "yarn workspace snap run build",
"postinstall": "perl -p -i -e 's/react\\.element/react.transitional.element/' packages/snap/node_modules/fbt/lib/FbtReactUtil.js && perl -p -i -e 's/didWarnAboutUsingAct = false;/didWarnAboutUsingAct = true;/' packages/babel-plugin-react-compiler/node_modules/react-dom/cjs/react-dom-test-utils.development.js",
"npm:publish": "node scripts/release/publish-manual"
"npm:publish": "node scripts/release/publish"
},
"dependencies": {
"fs-extra": "^4.0.2"
},
"dependencies": {},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,16 @@
const cp = require('child_process');
const ora = require('ora');
const path = require('path');
const yargs = require('yargs');
const util = require('util');
const {hashElement} = require('folder-hash');
const promptForOTP = require('./prompt-for-otp');

const PUBLISHABLE_PACKAGES = [
'babel-plugin-react-compiler',
'eslint-plugin-react-compiler',
'react-compiler-healthcheck',
];

function _spawn(command, args, options, cb) {
const child = cp.spawn(command, args, options);
child.on('close', exitCode => {
cb(null, exitCode);
});
return child;
}
const spawnHelper = util.promisify(_spawn);

function execHelper(command, options, streamStdout = false) {
return new Promise((resolve, reject) => {
const proc = cp.exec(command, options, (error, stdout) =>
error ? reject(error) : resolve(stdout.trim())
);
if (streamStdout) {
proc.stdout.pipe(process.stdout);
}
});
}

async function getDateStringForCommit(commit) {
let dateString = await execHelper(
`git show -s --no-show-signature --format=%cd --date=format:%Y%m%d ${commit}`
);

// On CI environment, this string is wrapped with quotes '...'s
if (dateString.startsWith("'")) {
dateString = dateString.slice(1, 9);
}

return dateString;
}
const {PUBLISHABLE_PACKAGES} = require('./shared/packages');
const {
execHelper,
getDateStringForCommit,
spawnHelper,
} = require('./shared/utils');
const {buildPackages} = require('./shared/build-packages');
const {readJson, writeJson} = require('fs-extra');

/**
* Script for publishing PUBLISHABLE_PACKAGES to npm. By default, this runs in tarball mode, meaning
Expand Down Expand Up @@ -83,12 +50,21 @@ async function main() {
type: 'boolean',
default: false,
})
.option('ci', {
description: 'Publish packages via CI',
type: 'boolean',
default: false,
})
.option('tags', {
description: 'Tags to publish to npm',
type: 'choices',
choices: ['experimental'],
default: ['experimental'],
})
.help('help')
.parseSync();

const {packages, forReal, debug} = argv;

if (debug === false) {
if (argv.debug === false) {
const currBranchName = await execHelper('git rev-parse --abbrev-ref HEAD');
const isPristine = (await execHelper('git status --porcelain')) === '';
if (currBranchName !== 'main' || isPristine === false) {
Expand All @@ -98,31 +74,19 @@ async function main() {
}
}

let pkgNames = packages;
if (Array.isArray(packages) === false) {
pkgNames = [packages];
let pkgNames = argv.packages;
if (Array.isArray(argv.packages) === false) {
pkgNames = [argv.packages];
}
const spinner = ora(
`Preparing to publish ${
forReal === true ? '(for real)' : '(dry run)'
} [debug=${debug}]`
argv.forReal === true ? '(for real)' : '(dry run)'
} [debug=${argv.debug}]`
).info();

spinner.info('Building packages');
for (const pkgName of pkgNames) {
const command = `yarn workspace ${pkgName} run build`;
spinner.start(`Running: ${command}\n`);
try {
await execHelper(command);
} catch (e) {
spinner.fail(e.toString());
throw e;
}
spinner.succeed(`Successfully built ${pkgName}`);
}
spinner.stop();
await buildPackages(pkgNames);

if (forReal === false) {
if (argv.forReal === false) {
spinner.info('Dry run: Report tarball contents');
for (const pkgName of pkgNames) {
console.log(`\n========== ${pkgName} ==========\n`);
Expand All @@ -143,18 +107,22 @@ async function main() {
);
}

if (forReal === true) {
const otp = await promptForOTP();
if (argv.forReal === true) {
const commit = await execHelper(
'git show -s --no-show-signature --format=%h',
{
cwd: path.resolve(__dirname, '..'),
}
);
const dateString = await getDateStringForCommit(commit);
const otp = argv.ci === false ? await promptForOTP() : null;

for (const pkgName of pkgNames) {
const pkgDir = path.resolve(__dirname, `../../packages/${pkgName}`);
const pkgJsonPath = path.resolve(
__dirname,
`../../packages/${pkgName}/package.json`
);
const {hash} = await hashElement(pkgDir, {
encoding: 'hex',
folders: {exclude: ['node_modules']},
Expand All @@ -163,39 +131,36 @@ async function main() {
const truncatedHash = hash.slice(0, 7);
const newVersion = `0.0.0-experimental-${truncatedHash}-${dateString}`;

spinner.start(`Bumping version: ${pkgName}`);
try {
await execHelper(
`yarn version --new-version ${newVersion} --no-git-tag-version`,
{
cwd: pkgDir,
}
);
await execHelper(
`git add package.json && git commit -m "Bump version to ${newVersion}"`,
{
cwd: pkgDir,
}
);
} catch (e) {
spinner.fail(e.toString());
throw e;
}
spinner.succeed(
`Bumped ${pkgName} to ${newVersion} and added a git commit`
spinner.start(`Writing package.json for ${pkgName}@${newVersion}`);
await writeJson(
pkgJsonPath,
{
...(await readJson(pkgJsonPath)),
version: newVersion,
},
{spaces: 2}
);
}
spinner.succeed(`Wrote package.json for ${pkgName}@${newVersion}`);

for (const pkgName of pkgNames) {
const pkgDir = path.resolve(__dirname, `../../packages/${pkgName}`);
console.log(`\n========== ${pkgName} ==========\n`);
spinner.start(`Publishing ${pkgName} to npm\n`);
spinner.start(`Publishing ${pkgName}@${newVersion} to npm\n`);

const opts = debug === true ? ['publish', '--dry-run'] : ['publish'];
let opts = [];
if (argv.debug === true) {
opts.push('--dry-run');
}
if (otp != null) {
opts.push(`--otp=${otp}`);
}
try {
await spawnHelper(
'npm',
[...opts, '--registry=https://registry.npmjs.org', `--otp=${otp}`],
[
'publish',
...opts,
'--registry=https://registry.npmjs.org',
'--tag=experimental',
],
{
cwd: pkgDir,
stdio: 'inherit',
Expand All @@ -207,9 +172,38 @@ async function main() {
throw e;
}
spinner.succeed(`Successfully published ${pkgName} to npm`);

spinner.start('Pushing tags to npm');
for (const tag of argv.tags) {
try {
let opts;
if (otp != null) {
opts = [
'dist-tag',
'add',
`${pkgName}@${newVersion}`,
tag,
`--otp=${otp}`,
];
} else {
opts = ['dist-tag', 'add', `${pkgName}@${newVersion}`, tag];
}
if (argv.debug === true) {
spinner.info(`dry-run: npm ${opts.join(' ')}`);
} else {
await spawnHelper('npm', opts);
}
} catch (e) {
spinner.fail(e.toString());
throw e;
}
spinner.succeed(
`Successfully pushed dist-tag ${tag} for ${pkgName} to npm`
);
}
}

console.log('\n\n✅ All done, please push version bump commits to GitHub');
console.log('\n\n✅ All done');
}
}

Expand Down
22 changes: 22 additions & 0 deletions compiler/scripts/release/shared/build-packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const ora = require('ora');
const {execHelper} = require('./utils');

async function buildPackages(pkgNames) {
const spinner = ora(`Building packages`).info();
for (const pkgName of pkgNames) {
const command = `yarn workspace ${pkgName} run build`;
spinner.start(`Running: ${command}\n`);
try {
await execHelper(command);
} catch (e) {
spinner.fail(e.toString());
throw e;
}
spinner.succeed(`Successfully built ${pkgName}`);
}
spinner.stop();
}

module.exports = {
buildPackages,
};
9 changes: 9 additions & 0 deletions compiler/scripts/release/shared/packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const PUBLISHABLE_PACKAGES = [
'babel-plugin-react-compiler',
'eslint-plugin-react-compiler',
'react-compiler-healthcheck',
];

module.exports = {
PUBLISHABLE_PACKAGES,
};
41 changes: 41 additions & 0 deletions compiler/scripts/release/shared/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const cp = require('child_process');
const util = require('util');

function execHelper(command, options, streamStdout = false) {
return new Promise((resolve, reject) => {
const proc = cp.exec(command, options, (error, stdout) =>
error ? reject(error) : resolve(stdout.trim())
);
if (streamStdout) {
proc.stdout.pipe(process.stdout);
}
});
}

function _spawn(command, args, options, cb) {
const child = cp.spawn(command, args, options);
child.on('close', exitCode => {
cb(null, exitCode);
});
return child;
}
const spawnHelper = util.promisify(_spawn);

async function getDateStringForCommit(commit) {
let dateString = await execHelper(
`git show -s --no-show-signature --format=%cd --date=format:%Y%m%d ${commit}`
);

// On CI environment, this string is wrapped with quotes '...'s
if (dateString.startsWith("'")) {
dateString = dateString.slice(1, 9);
}

return dateString;
}

module.exports = {
execHelper,
spawnHelper,
getDateStringForCommit,
};
23 changes: 22 additions & 1 deletion compiler/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5434,6 +5434,15 @@ fraction.js@^4.2.0:
resolved "https://registry.yarnpkg.com/fraction.js/-/fraction.js-4.2.0.tgz#448e5109a313a3527f5a3ab2119ec4cf0e0e2950"
integrity sha512-MhLuK+2gUcnZe8ZHlaaINnQLl0xRIGRfcGk2yl8xoQAfHrSsL3rYu6FCmBdkdbhc9EPlwyGHewaRsvwRMJtAlA==

fs-extra@^4.0.2:
version "4.0.3"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94"
integrity sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
universalify "^0.1.0"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -5686,7 +5695,7 @@ gopd@^1.0.1:
dependencies:
get-intrinsic "^1.1.3"

graceful-fs@^4.1.2, graceful-fs@^4.2.4:
graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.4:
version "4.2.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3"
integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==
Expand Down Expand Up @@ -7591,6 +7600,13 @@ json5@^2.1.2, json5@^2.2.3:
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
integrity sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==
optionalDependencies:
graceful-fs "^4.1.6"

"jsx-ast-utils@^2.4.1 || ^3.0.0", jsx-ast-utils@^3.3.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.3.3.tgz#76b3e6e6cece5c69d49a5792c3d01bd1a0cdc7ea"
Expand Down Expand Up @@ -9718,6 +9734,11 @@ unicode-property-aliases-ecmascript@^2.0.0:
resolved "https://registry.yarnpkg.com/unicode-property-aliases-ecmascript/-/unicode-property-aliases-ecmascript-2.1.0.tgz#43d41e3be698bd493ef911077c9b131f827e8ccd"
integrity sha512-6t3foTQI9qne+OZoVQB/8x8rk2k1eVy1gRXhV3oFQ5T6R1dqQ1xtin3XqSlx3+ATBkliTaR/hHyJBm+LVPNM8w==

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==

universalify@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.2.0.tgz#6451760566fa857534745ab1dde952d1b1761be0"
Expand Down