-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Add yarn twenty pull to write an installed application back to source
#25410
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
Open
Weiko
wants to merge
7
commits into
main
Choose a base branch
from
c--pull-3-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86b1d64
Add the pull command that writes an installed application back to source
Weiko 1ef203a
Round-trip objects whose label identifier names an engine-derived field
Weiko c2b4290
Mark pull as experimental and report define files it could not read
Weiko 9164f27
Document the verbose flag on pull
Weiko 24c38f8
Address the pull review: rollback on failure, path reservation, escaping
Weiko 9090708
Use the isDefined guard in the define file writer
Weiko 6040096
Harden the pull writer against unreadable files, case collisions and …
Weiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { appPull } from '@/cli/operations/pull'; | ||
| import { ConfigService } from '@/cli/utilities/config/config-service'; | ||
| import { CURRENT_EXECUTION_DIRECTORY } from '@/cli/utilities/config/current-execution-directory'; | ||
| import { formatPullReport } from '@/cli/utilities/pull/format-pull-report'; | ||
| import { PULL_BASE_FILE_PATH } from '@/cli/utilities/pull/pull-base-file'; | ||
| import { checkSdkVersionCompatibility } from '@/cli/utilities/version/check-sdk-version-compatibility'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| export type AppPullCommandOptions = { | ||
| appPath?: string; | ||
| universalIdentifier?: string; | ||
| verbose?: boolean; | ||
| }; | ||
|
|
||
| export class AppPullCommand { | ||
| async execute(options: AppPullCommandOptions): Promise<void> { | ||
| const appPath = options.appPath ?? CURRENT_EXECUTION_DIRECTORY; | ||
|
|
||
| await checkSdkVersionCompatibility(appPath); | ||
|
|
||
| const remoteName = ConfigService.getActiveRemote(); | ||
|
|
||
| console.log( | ||
| chalk.yellow( | ||
| '⚠ pull is experimental\n' + | ||
| ' It covers only part of an application, and it overwrites the files\n' + | ||
| ' that define what it pulls. Commit your work before running it.\n', | ||
| ), | ||
| ); | ||
| console.log(chalk.blue(`Pulling application from ${remoteName}...`)); | ||
| console.log(chalk.gray(`App path: ${appPath}\n`)); | ||
|
|
||
| const result = await appPull({ | ||
| appPath, | ||
| universalIdentifier: options.universalIdentifier, | ||
| onProgress: (message) => console.log(chalk.gray(message)), | ||
| }); | ||
|
|
||
| if (!result.success) { | ||
| console.error(chalk.red(result.error.message)); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const report = formatPullReport({ | ||
| writes: result.data.writes, | ||
| deletions: result.data.deletions, | ||
| unchangedCount: result.data.unchangedCount, | ||
| skipped: result.data.skipped, | ||
| coverage: result.data.coverage, | ||
| localOnlyRelativePaths: result.data.localOnlyRelativePaths, | ||
| unreadableRelativePaths: result.data.unreadableRelativePaths, | ||
| verbose: options.verbose, | ||
| }); | ||
|
|
||
| console.log(`\n${report}\n`); | ||
|
|
||
| console.log( | ||
| chalk.green( | ||
| `✓ Pulled ${result.data.applicationDisplayName} into ${appPath}`, | ||
| ), | ||
| ); | ||
| console.log(chalk.gray(`Base recorded in ${PULL_BASE_FILE_PATH}`)); | ||
| console.log(chalk.gray('Next: yarn twenty plan --no-delete')); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { APP_ERROR_CODES, type CommandResult } from '@/cli/types'; | ||
| import { ApiService } from '@/cli/utilities/api/api-service'; | ||
| import { promptForReauthentication } from '@/cli/utilities/auth/reauth-helper'; | ||
| import { ManifestEntityKey } from '@/cli/utilities/build/manifest/manifest-extract-config'; | ||
| import { ConfigService } from '@/cli/utilities/config/config-service'; | ||
| import { | ||
| getGraphQLErrorMessage, | ||
| hasGraphQLErrorSubCode, | ||
| } from '@/cli/utilities/error/parse-server-error'; | ||
| import { pathExists } from '@/cli/utilities/file/fs-utils'; | ||
| import { type ApplicationExportCoverageEntry } from '@/cli/utilities/pull/application-export-type'; | ||
| import { applyPullWrites } from '@/cli/utilities/pull/apply-pull-writes'; | ||
| import { type SkippedPullEntity } from '@/cli/utilities/pull/build-pull-entities'; | ||
| import { | ||
| planPullWrites, | ||
| type PullDeletion, | ||
| type PullWrite, | ||
| } from '@/cli/utilities/pull/plan-pull-writes'; | ||
| import { | ||
| readPullBaseManifest, | ||
| writePullBaseManifest, | ||
| } from '@/cli/utilities/pull/pull-base-file'; | ||
| import { scanProjectDefineFiles } from '@/cli/utilities/pull/scan-project-define-files'; | ||
| import { runSafe } from '@/cli/utilities/run-safe'; | ||
| import { join } from 'node:path'; | ||
| import { isDefined } from 'twenty-shared/utils'; | ||
|
|
||
| export type AppPullOptions = { | ||
| appPath: string; | ||
| universalIdentifier?: string; | ||
| onProgress?: (message: string) => void; | ||
| }; | ||
|
|
||
| export type AppPullResult = { | ||
| applicationDisplayName: string; | ||
| applicationUniversalIdentifier: string; | ||
| writes: PullWrite[]; | ||
| deletions: PullDeletion[]; | ||
| unchangedCount: number; | ||
| localOnlyRelativePaths: string[]; | ||
| skipped: SkippedPullEntity[]; | ||
| coverage: ApplicationExportCoverageEntry[]; | ||
| unreadableRelativePaths: string[]; | ||
| hadBase: boolean; | ||
| }; | ||
|
|
||
| const EXPORT_REFUSAL_SUB_CODES = [ | ||
| 'APPLICATION_NOT_EXPORTABLE', | ||
| 'STANDARD_APPLICATION_NOT_EXPORTABLE', | ||
| 'APPLICATION_NOT_FOUND', | ||
| ]; | ||
|
|
||
| const innerAppPull = async ( | ||
| options: AppPullOptions, | ||
| ): Promise<CommandResult<AppPullResult>> => { | ||
| const { appPath, onProgress } = options; | ||
|
|
||
| if (!(await pathExists(join(appPath, 'package.json')))) { | ||
| return { | ||
| success: false, | ||
| error: { | ||
| code: APP_ERROR_CODES.PULL_FAILED, | ||
| message: | ||
| `No package.json found in ${appPath}.\n\n` + | ||
| ' Scaffold a project first, then pull into it:\n' + | ||
| ' npx create-twenty-app@latest my-app', | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| onProgress?.('Checking server...'); | ||
|
|
||
| const apiService = new ApiService({ disableInterceptors: true }); | ||
| const validateAuth = await apiService.validateAuth(); | ||
|
|
||
| if (!validateAuth.serverUp) { | ||
| return { | ||
| success: false, | ||
| error: { | ||
| code: APP_ERROR_CODES.PULL_FAILED, | ||
| message: | ||
| 'Cannot reach Twenty server.\n\n' + | ||
| ' Start a local server:\n' + | ||
| ' yarn twenty docker:start\n\n' + | ||
| ' Check server status:\n' + | ||
| ' yarn twenty docker:status', | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (!validateAuth.authValid) { | ||
| const outcome = await promptForReauthentication( | ||
| ConfigService.getActiveRemote(), | ||
| ); | ||
|
|
||
| if (outcome !== 'reauthenticated') { | ||
| return { | ||
| success: false, | ||
| error: { | ||
| code: APP_ERROR_CODES.PULL_FAILED, | ||
| message: | ||
| 'Authentication failed. Run `yarn twenty remote:add` to authenticate.', | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| onProgress?.('Reading local source files...'); | ||
|
|
||
| const scannedFiles = await scanProjectDefineFiles(appPath); | ||
| const localApplicationUniversalIdentifier = scannedFiles.find( | ||
| (scannedFile) => scannedFile.entityKey === ManifestEntityKey.Application, | ||
| )?.universalIdentifier; | ||
|
|
||
| const universalIdentifier = | ||
| options.universalIdentifier ?? localApplicationUniversalIdentifier; | ||
|
|
||
| if (!isDefined(universalIdentifier)) { | ||
| return { | ||
| success: false, | ||
| error: { | ||
| code: APP_ERROR_CODES.PULL_FAILED, | ||
| message: | ||
| 'Could not tell which application to pull.\n\n' + | ||
| ' Pass the identifier explicitly:\n' + | ||
| ' yarn twenty pull -u <universalIdentifier>', | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| onProgress?.(`Exporting application ${universalIdentifier}...`); | ||
|
|
||
| const exportResult = await apiService.exportApplication(universalIdentifier); | ||
|
|
||
| if (!exportResult.success) { | ||
| const isRefusal = EXPORT_REFUSAL_SUB_CODES.some((subCode) => | ||
| hasGraphQLErrorSubCode(exportResult.error, subCode), | ||
| ); | ||
|
|
||
| return { | ||
| success: false, | ||
| error: { | ||
| code: APP_ERROR_CODES.PULL_FAILED, | ||
| message: isRefusal | ||
| ? (getGraphQLErrorMessage(exportResult.error) ?? | ||
| 'The server refused to export this application') | ||
| : `Export failed: ${getGraphQLErrorMessage(exportResult.error) ?? exportResult.message ?? 'Unknown error'}`, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const applicationExport = exportResult.data; | ||
| const { manifest } = applicationExport; | ||
|
|
||
| onProgress?.('Planning source writes...'); | ||
|
|
||
| const baseManifest = await readPullBaseManifest({ | ||
| appPath, | ||
| applicationUniversalIdentifier: manifest.application.universalIdentifier, | ||
| }); | ||
|
|
||
| const plan = planPullWrites({ manifest, baseManifest, scannedFiles }); | ||
|
|
||
| onProgress?.('Writing source files...'); | ||
|
|
||
| await applyPullWrites({ | ||
| appPath, | ||
| writes: plan.writes, | ||
| deletions: plan.deletions, | ||
| }); | ||
|
|
||
| await writePullBaseManifest({ appPath, manifest }); | ||
|
|
||
| return { | ||
| success: true, | ||
| data: { | ||
| applicationDisplayName: applicationExport.application.displayName, | ||
| applicationUniversalIdentifier: | ||
| applicationExport.application.universalIdentifier, | ||
| writes: plan.writes, | ||
| deletions: plan.deletions, | ||
| unchangedCount: plan.unchanged.length, | ||
| localOnlyRelativePaths: plan.localOnlyRelativePaths, | ||
| skipped: plan.skipped, | ||
| coverage: applicationExport.coverage, | ||
| unreadableRelativePaths: scannedFiles | ||
| .filter((scannedFile) => !scannedFile.isReadable) | ||
| .map((scannedFile) => scannedFile.relativePath), | ||
| hadBase: isDefined(baseManifest), | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export const appPull = ( | ||
| options: AppPullOptions, | ||
| ): Promise<CommandResult<AppPullResult>> => | ||
| runSafe(() => innerAppPull(options), APP_ERROR_CODES.PULL_FAILED); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would deserve a pull-plan mode (ok for doing it in another PR but i would start by this personnally)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, and I would like it too. Two things make me want it as a follow-up rather than here.
It needs the writer to produce content without touching the tree, which is already how it works: the planner returns the full content of every write, and applying it is a separate step. So a plan mode is mostly a flag that stops before
applyPullWritesand prints the same report, plus a diff of each file against what is on disk. That diff is the part worth designing properly rather than bolting on, since the useful output is "this file would change in these ways", not just "this file would be rewritten".The other reason is size: this PR is already flagged as too large to review reliably, so I would rather not add a surface to it.
Happy to take it as the next one if you want it before the remaining entity families.