Files
Alex Hunt 526016d950 Support committing debugger-frontend sync under Git checkouts (#57997)
Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57997

`scripts/debugger-frontend/sync-and-build` with `--create-diff` only worked under fbsource, making commit creation (in particular the generated changes table) inconvenient outside of Meta.

This diff lifts commit creation functionality to both Git and Mercurial (`sl`), and makes writing a local commit the default behaviour.

**Changes**

- Synced `debugger-frontend` artifacts are now always committed, regardless of version control backend. Internal Mercurial behaviour is forked to a `fbsource-backend.fb.js` script.
- `--create-diff` is narrowed to draft Phabricator diff submission (fbsource only).
- The script now aborts if there are any working copy changes.

Changelog: [Internal]

Reviewed By: vzaidman

Differential Revision: D116031207

fbshipit-source-id: 8ef3ed465b91d8f71ffa44fd1cba16f28860e3a9
2026-08-18 07:09:53 -07:00

86 lines
2.4 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
/*::
export type Command = [string, Array<string>];
export type CommitMessageParts = Readonly<{
title: string,
summary: string,
changelogTable: string,
changelogEntry: string,
}>;
// Commands are returned rather than run so that the caller can report them
// consistently with the rest of the sync.
export type CommitBackend = Readonly<{
// Must print nothing when the working directory is clean.
status: Command,
// Joined with blank lines; empty entries are dropped.
messageBlocks: (parts: CommitMessageParts) => ReadonlyArray<string>,
commit: (packagePath: string, messageFile: string) => ReadonlyArray<Command>,
}>;
export type ResolveContext = Readonly<{
createDiff: boolean,
noBuild: boolean,
}>;
*/
const GIT /*: CommitBackend */ = {
status: ['git', ['status', '--porcelain', '--', '.']],
messageBlocks: ({title, summary, changelogTable, changelogEntry}) => [
title,
summary,
changelogTable,
`Changelog: ${changelogEntry}`,
],
commit: (packagePath, messageFile) => [
['git', ['add', '-A', '--', packagePath]],
['git', ['commit', '-F', messageFile, '--', packagePath]],
],
};
function moduleExists(modulePath /*: string */) /*: boolean */ {
try {
require.resolve(modulePath);
return true;
} catch {
return false;
}
}
// Resolved before requiring so that a failure to load the module is not
// mistaken for its absence in the open source repo. The filename is
// deliberately not `commit-backend.fb.js`: Flow and Metro resolve `X.fb.js`
// ahead of `X.js`, but Node - which runs this script - does not.
const resolveFb /*: ?(context: ResolveContext) => Promise<?CommitBackend> */ =
moduleExists('./fbsource-backend.fb.js')
? // $FlowFixMe[cannot-resolve-module] - not resolvable in OSS
require('./fbsource-backend.fb.js')
: null;
async function resolveCommitBackend(
context /*: ResolveContext */,
) /*: Promise<CommitBackend> */ {
const fbBackend = await resolveFb?.(context);
if (fbBackend != null) {
return fbBackend;
}
if (context.createDiff) {
throw new Error('--create-diff requires an fbsource checkout');
}
return GIT;
}
module.exports = {
resolveCommitBackend,
};