-
Notifications
You must be signed in to change notification settings - Fork 451
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
feat(cli): add support for remote templates with --template
#7867
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3834567
fix: remove trailing commas when sanitizing
RostiMelk 9611d2b
feat: remote template support
RostiMelk 1e4a4f2
feat: add support for @ in dirs
RostiMelk c8cf1a0
feat: change to env local file
RostiMelk 346653b
feat: remove old remix template
RostiMelk ebcf536
feat: security token error msg
RostiMelk 7cb9de8
feat: remove isRoot
RostiMelk 604a8a1
feat: security token error msg
RostiMelk 8200cb0
feat: remove apiVersion replacer
RostiMelk 73c0d5a
feat: sync
RostiMelk d49a1dc
feat: sync
RostiMelk 1fd64a5
feat: fix: wrong dep -op
RostiMelk 7512bd2
feat: sync
RostiMelk 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 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 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
69 changes: 69 additions & 0 deletions
69
packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts
This file contains 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,69 @@ | ||
import {mkdir} from 'node:fs/promises' | ||
import {join} from 'node:path' | ||
|
||
import {debug} from '../../debug' | ||
import {type CliCommandContext} from '../../types' | ||
import { | ||
applyEnvVariables, | ||
downloadAndExtractRepo, | ||
generateSanityApiReadToken, | ||
getMonoRepo, | ||
isNextJsTemplate, | ||
type RepoInfo, | ||
tryApplyPackageName, | ||
validateRemoteTemplate, | ||
} from '../../util/remoteTemplate' | ||
import {type GenerateConfigOptions} from './createStudioConfig' | ||
import {tryGitInit} from './git' | ||
import {updateInitialTemplateMetadata} from './updateInitialTemplateMetadata' | ||
|
||
export interface BootstrapRemoteOptions { | ||
outputPath: string | ||
repoInfo: RepoInfo | ||
bearerToken?: string | ||
packageName: string | ||
variables: GenerateConfigOptions['variables'] | ||
} | ||
|
||
const INITIAL_COMMIT_MESSAGE = 'Initial commit from Sanity CLI' | ||
|
||
export async function bootstrapRemoteTemplate( | ||
opts: BootstrapRemoteOptions, | ||
context: CliCommandContext, | ||
): Promise<void> { | ||
const {outputPath, repoInfo, bearerToken, variables, packageName} = opts | ||
const {output, apiClient} = context | ||
const name = [repoInfo.username, repoInfo.name, repoInfo.filePath].filter(Boolean).join('/') | ||
const spinner = output.spinner(`Bootstrapping files from template "${name}"`).start() | ||
|
||
debug('Validating remote template') | ||
const packages = await getMonoRepo(repoInfo, bearerToken) | ||
await validateRemoteTemplate(repoInfo, packages, bearerToken) | ||
|
||
debug('Create new directory "%s"', outputPath) | ||
await mkdir(outputPath, {recursive: true}) | ||
|
||
debug('Downloading and extracting repo to %s', outputPath) | ||
await downloadAndExtractRepo(outputPath, repoInfo, bearerToken) | ||
|
||
debug('Applying environment variables') | ||
const readToken = await generateSanityApiReadToken(variables.projectId, apiClient) | ||
const isNext = await isNextJsTemplate(outputPath) | ||
const envName = isNext ? '.env.local' : '.env' | ||
|
||
for (const folder of packages ?? ['']) { | ||
const path = join(outputPath, folder) | ||
await applyEnvVariables(path, {...variables, readToken}, envName) | ||
} | ||
|
||
debug('Setting package name to %s', packageName) | ||
await tryApplyPackageName(outputPath, packageName) | ||
|
||
debug('Initializing git repository') | ||
tryGitInit(outputPath, INITIAL_COMMIT_MESSAGE) | ||
|
||
debug('Updating initial template metadata') | ||
await updateInitialTemplateMetadata(apiClient, variables.projectId, `external-${name}`) | ||
|
||
spinner.succeed() | ||
} |
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.
Clearly seeing the pipeline of things that need to be done serially here but unsure how failure of this half-way would look. We have a pattern of showing the error messages in the cli but if this partially completes, it might be confusing to users to know how to proceed if they have the repo down but were unable to have any of the files edited. Perhaps some try/catch around groups of this where the errors are still thrown to be printed on the cli but have some surrounding context like "Unable to edit newly downloaded files in [folder]/. See documentation on updating files from a template: some-docs link" or something else could be helpful in getting people un-stuck at certain points.
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.
If the template does not pass validation, we will just abort, and you should get a message with why it did not pass. This is fine imo.
There are a new other steps it could fail in after we've downloaded files over to the users computer.