mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 00:20:28 +01:00
* Don't bother including `unstable_` in error The method names don't get stripped out of the production bundles because they are passed as arguments to the error decoder. Let's just always use the unprefixed APIs in the messages. * Set up experimental builds The experimental builds are packaged exactly like builds in the stable release channel: same file structure, entry points, and npm package names. The goal is to match what will eventually be released in stable as closely as possible, but with additional features turned on. Versioning and Releasing ------------------------ The experimental builds will be published to the same registry and package names as the stable ones. However, they will be versioned using a separate scheme. Instead of semver versions, experimental releases will receive arbitrary version strings based on their content hashes. The motivation is to thwart attempts to use a version range to match against future experimental releases. The only way to install or depend on an experimental release is to refer to the specific version number. Building -------- I did not use the existing feature flag infra to configure the experimental builds. The reason is because feature flags are designed to configure a single package. They're not designed to generate multiple forks of the same package; for each set of feature flags, you must create a separate package configuration. Instead, I've added a new build dimension called the **release channel**. By default, builds use the **stable** channel. There's also an **experimental** release channel. We have the option to add more in the future. There are now two dimensions per artifact: build type (production, development, or profiling), and release channel (stable or experimental). These are separate dimensions because they are combinatorial: there are stable and experimental production builds, stable and experimental developmenet builds, and so on. You can add something to an experimental build by gating on `__EXPERIMENTAL__`, similar to how we use `__DEV__`. Anything inside these branches will be excluded from the stable builds. This gives us a low effort way to add experimental behavior in any package without setting up feature flags or configuring a new package.
263 lines
8.5 KiB
JavaScript
263 lines
8.5 KiB
JavaScript
'use strict';
|
|
|
|
const {exec} = require('child-process-promise');
|
|
const {createPatch} = require('diff');
|
|
const {hashElement} = require('folder-hash');
|
|
const {readdirSync, readFileSync, statSync, writeFileSync} = require('fs');
|
|
const {readJson, writeJson} = require('fs-extra');
|
|
const http = require('request-promise-json');
|
|
const logUpdate = require('log-update');
|
|
const {join} = require('path');
|
|
const createLogger = require('progress-estimator');
|
|
const prompt = require('prompt-promise');
|
|
const theme = require('./theme');
|
|
|
|
// The following packages are published to NPM but not by this script.
|
|
// They are released through a separate process.
|
|
const RELEASE_SCRIPT_PACKAGE_SKIPLIST = [
|
|
'react-devtools',
|
|
'react-devtools-core',
|
|
'react-devtools-inline',
|
|
];
|
|
|
|
// https://www.npmjs.com/package/progress-estimator#configuration
|
|
const logger = createLogger({
|
|
storagePath: join(__dirname, '.progress-estimator'),
|
|
});
|
|
|
|
const confirm = async message => {
|
|
const confirmation = await prompt(theme`\n{caution ${message}} (y/N) `);
|
|
prompt.done();
|
|
if (confirmation !== 'y' && confirmation !== 'Y') {
|
|
console.log(theme`\n{caution Release cancelled.}`);
|
|
process.exit(0);
|
|
}
|
|
};
|
|
|
|
const execRead = async (command, options) => {
|
|
const {stdout} = await exec(command, options);
|
|
|
|
return stdout.trim();
|
|
};
|
|
|
|
const getArtifactsList = async buildID => {
|
|
const buildMetadataURL = `https://circleci.com/api/v1.1/project/github/facebook/react/${buildID}?circle-token=${
|
|
process.env.CIRCLE_CI_API_TOKEN
|
|
}`;
|
|
const buildMetadata = await http.get(buildMetadataURL, true);
|
|
if (!buildMetadata.workflows || !buildMetadata.workflows.workflow_id) {
|
|
console.log(
|
|
theme`{error Could not find workflow info for build ${buildID}.}`
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const workflowID = buildMetadata.workflows.workflow_id;
|
|
const workflowMetadataURL = `https://circleci.com/api/v2/workflow/${workflowID}/jobs?circle-token=${
|
|
process.env.CIRCLE_CI_API_TOKEN
|
|
}`;
|
|
const workflowMetadata = await http.get(workflowMetadataURL, true);
|
|
const job = workflowMetadata.items.find(
|
|
({name}) => name === 'process_artifacts'
|
|
);
|
|
if (!job || !job.job_number) {
|
|
console.log(
|
|
theme`{error Could not find "process_artifacts" job for workflow ${workflowID}.}`
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const jobArtifactsURL = `https://circleci.com/api/v1.1/project/github/facebook/react/${
|
|
job.job_number
|
|
}/artifacts?circle-token=${process.env.CIRCLE_CI_API_TOKEN}`;
|
|
const jobArtifacts = await http.get(jobArtifactsURL, true);
|
|
|
|
return jobArtifacts;
|
|
};
|
|
|
|
const getBuildInfo = async () => {
|
|
const cwd = join(__dirname, '..', '..');
|
|
|
|
const isExperimental = process.env.RELEASE_CHANNEL === 'experimental';
|
|
|
|
const branch = await execRead('git branch | grep \\* | cut -d " " -f2', {
|
|
cwd,
|
|
});
|
|
const commit = await execRead('git show -s --format=%h', {cwd});
|
|
const checksum = await getChecksumForCurrentRevision(cwd);
|
|
const version = isExperimental
|
|
? `0.0.0-experimental-${commit}`
|
|
: `0.0.0-${commit}`;
|
|
|
|
// Only available for Circle CI builds.
|
|
// https://circleci.com/docs/2.0/env-vars/
|
|
const buildNumber = process.env.CIRCLE_BUILD_NUM;
|
|
|
|
// React version is stored explicitly, separately for DevTools support.
|
|
// See updateVersionsForCanary() below for more info.
|
|
const packageJSON = await readJson(
|
|
join(cwd, 'packages', 'react', 'package.json')
|
|
);
|
|
const reactVersion = isExperimental
|
|
? `${packageJSON.version}-experimental-canary-${commit}`
|
|
: `${packageJSON.version}-canary-${commit}`;
|
|
|
|
return {branch, buildNumber, checksum, commit, reactVersion, version};
|
|
};
|
|
|
|
const getChecksumForCurrentRevision = async cwd => {
|
|
const packagesDir = join(cwd, 'packages');
|
|
const hashedPackages = await hashElement(packagesDir, {
|
|
encoding: 'hex',
|
|
files: {exclude: ['.DS_Store']},
|
|
});
|
|
return hashedPackages.hash.slice(0, 7);
|
|
};
|
|
|
|
const getPublicPackages = () => {
|
|
const packagesRoot = join(__dirname, '..', '..', 'packages');
|
|
|
|
return readdirSync(packagesRoot).filter(dir => {
|
|
if (RELEASE_SCRIPT_PACKAGE_SKIPLIST.includes(dir)) {
|
|
return false;
|
|
}
|
|
|
|
const packagePath = join(packagesRoot, dir, 'package.json');
|
|
|
|
if (dir.charAt(0) !== '.' && statSync(packagePath).isFile()) {
|
|
const packageJSON = JSON.parse(readFileSync(packagePath));
|
|
|
|
return packageJSON.private !== true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
};
|
|
|
|
const handleError = error => {
|
|
logUpdate.clear();
|
|
|
|
const message = error.message.trim().replace(/\n +/g, '\n');
|
|
const stack = error.stack.replace(error.message, '');
|
|
|
|
console.log(theme`{error ${message}}\n\n{path ${stack}}`);
|
|
process.exit(1);
|
|
};
|
|
|
|
const logPromise = async (promise, text, estimate) =>
|
|
logger(promise, text, {estimate});
|
|
|
|
const printDiff = (path, beforeContents, afterContents) => {
|
|
const patch = createPatch(path, beforeContents, afterContents);
|
|
const coloredLines = patch
|
|
.split('\n')
|
|
.slice(2) // Trim index file
|
|
.map((line, index) => {
|
|
if (index <= 1) {
|
|
return theme.diffHeader(line);
|
|
}
|
|
switch (line[0]) {
|
|
case '+':
|
|
return theme.diffAdded(line);
|
|
case '-':
|
|
return theme.diffRemoved(line);
|
|
case ' ':
|
|
return line;
|
|
case '@':
|
|
return null;
|
|
case '\\':
|
|
return null;
|
|
}
|
|
})
|
|
.filter(line => line);
|
|
console.log(coloredLines.join('\n'));
|
|
return patch;
|
|
};
|
|
|
|
// Convert an array param (expected format "--foo bar baz")
|
|
// to also accept comma input (e.g. "--foo bar,baz")
|
|
const splitCommaParams = array => {
|
|
for (let i = array.length - 1; i >= 0; i--) {
|
|
const param = array[i];
|
|
if (param.includes(',')) {
|
|
array.splice(i, 1, ...param.split(','));
|
|
}
|
|
}
|
|
};
|
|
|
|
// This method is used by both local Node release scripts and Circle CI bash scripts.
|
|
// It updates version numbers in package JSONs (both the version field and dependencies),
|
|
// As well as the embedded renderer version in "packages/shared/ReactVersion".
|
|
// Canaries version numbers use the format of 0.0.0-<sha> to be easily recognized (e.g. 0.0.0-57239eac8).
|
|
// A separate "React version" is used for the embedded renderer version to support DevTools,
|
|
// since it needs to distinguish between different version ranges of React.
|
|
// It is based on the version of React in the local package.json (e.g. 16.6.1-canary-57239eac8).
|
|
// Both numbers will be replaced if the canary is promoted to a stable release.
|
|
const updateVersionsForCanary = async (cwd, reactVersion, version) => {
|
|
const packages = getPublicPackages(join(cwd, 'packages'));
|
|
const packagesDir = join(cwd, 'packages');
|
|
|
|
// Update the shared React version source file.
|
|
// This is bundled into built renderers.
|
|
// The promote script will replace this with a final version later.
|
|
const sourceReactVersionPath = join(cwd, 'packages/shared/ReactVersion.js');
|
|
const sourceReactVersion = readFileSync(
|
|
sourceReactVersionPath,
|
|
'utf8'
|
|
).replace(
|
|
/module\.exports = '[^']+';/,
|
|
`module.exports = '${reactVersion}';`
|
|
);
|
|
writeFileSync(sourceReactVersionPath, sourceReactVersion);
|
|
|
|
// Update the root package.json.
|
|
// This is required to pass a later version check script.
|
|
{
|
|
const packageJSONPath = join(cwd, 'package.json');
|
|
const packageJSON = await readJson(packageJSONPath);
|
|
packageJSON.version = version;
|
|
await writeJson(packageJSONPath, packageJSON, {spaces: 2});
|
|
}
|
|
|
|
for (let i = 0; i < packages.length; i++) {
|
|
const packageName = packages[i];
|
|
const packagePath = join(packagesDir, packageName);
|
|
|
|
// Update version numbers in package JSONs
|
|
const packageJSONPath = join(packagePath, 'package.json');
|
|
const packageJSON = await readJson(packageJSONPath);
|
|
packageJSON.version = version;
|
|
|
|
// Also update inter-package dependencies.
|
|
// Canary releases always have exact version matches.
|
|
// The promote script may later relax these (e.g. "^x.x.x") based on source package JSONs.
|
|
const {dependencies, peerDependencies} = packageJSON;
|
|
for (let j = 0; j < packages.length; j++) {
|
|
const dependencyName = packages[j];
|
|
if (dependencies && dependencies[dependencyName]) {
|
|
dependencies[dependencyName] = version;
|
|
}
|
|
if (peerDependencies && peerDependencies[dependencyName]) {
|
|
peerDependencies[dependencyName] = version;
|
|
}
|
|
}
|
|
|
|
await writeJson(packageJSONPath, packageJSON, {spaces: 2});
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
confirm,
|
|
execRead,
|
|
getArtifactsList,
|
|
getBuildInfo,
|
|
getChecksumForCurrentRevision,
|
|
getPublicPackages,
|
|
handleError,
|
|
logPromise,
|
|
printDiff,
|
|
splitCommaParams,
|
|
theme,
|
|
updateVersionsForCanary,
|
|
};
|