-
Notifications
You must be signed in to change notification settings - Fork 448
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 non-studio app template #8394
Merged
+641
−154
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d59295d
feat(cli): add non-studio app template and non-studio dev options
cngonzalez 9773a36
refactor: align core-app structure and start-up more closely with studio
cngonzalez 7851cad
fix: forego dep check and remove framework parameter
cngonzalez a428dd6
refactor: switch ternary to isCoreApp
cngonzalez 63b3ca3
feat: add subgroup commands
cngonzalez 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
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
23 changes: 23 additions & 0 deletions
23
packages/@sanity/cli/src/actions/init-project/createCoreAppCliConfig.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,23 @@ | ||
import {processTemplate} from './processTemplate' | ||
|
||
const defaultCoreAppTemplate = ` | ||
import {defineCliConfig} from 'sanity/cli' | ||
|
||
export default defineCliConfig({ | ||
__experimental_coreAppConfiguration: { | ||
appLocation: '%appLocation%' | ||
}, | ||
}) | ||
` | ||
|
||
export interface GenerateCliConfigOptions { | ||
organizationId?: string | ||
appLocation: string | ||
} | ||
|
||
export function createCoreAppCliConfig(options: GenerateCliConfigOptions): string { | ||
return processTemplate({ | ||
template: defaultCoreAppTemplate, | ||
variables: options, | ||
}) | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/@sanity/cli/src/actions/init-project/determineCoreAppTemplate.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,13 @@ | ||
const coreAppTemplates = ['core-app'] | ||
|
||
/** | ||
* Determine if a given template is a studio template. | ||
* This function may need to be more robust once we | ||
* introduce remote templates, for example. | ||
* | ||
* @param templateName - Name of the template | ||
* @returns boolean indicating if the template is a studio template | ||
*/ | ||
export function determineCoreAppTemplate(templateName: string): boolean { | ||
return coreAppTemplates.includes(templateName) | ||
} |
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
55 changes: 55 additions & 0 deletions
55
packages/@sanity/cli/src/actions/init-project/processTemplate.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,55 @@ | ||
import traverse from '@babel/traverse' | ||
import {parse, print} from 'recast' | ||
import * as parser from 'recast/parsers/typescript' | ||
|
||
interface TemplateOptions<T> { | ||
template: string | ||
variables: T | ||
includeBooleanTransform?: boolean | ||
} | ||
|
||
export function processTemplate<T extends object>(options: TemplateOptions<T>): string { | ||
const {template, variables, includeBooleanTransform = false} = options | ||
const ast = parse(template.trimStart(), {parser}) | ||
|
||
traverse(ast, { | ||
StringLiteral: { | ||
enter({node}) { | ||
const value = node.value | ||
if (!value.startsWith('%') || !value.endsWith('%')) { | ||
return | ||
} | ||
const variableName = value.slice(1, -1) as keyof T | ||
if (!(variableName in variables)) { | ||
throw new Error(`Template variable '${value}' not defined`) | ||
} | ||
const newValue = variables[variableName] | ||
/* | ||
* although there are valid non-strings in our config, | ||
* they're not in StringLiteral nodes, so assume undefined | ||
*/ | ||
node.value = typeof newValue === 'string' ? newValue : '' | ||
}, | ||
}, | ||
...(includeBooleanTransform && { | ||
Identifier: { | ||
enter(path) { | ||
if (!path.node.name.startsWith('__BOOL__')) { | ||
return | ||
} | ||
const variableName = path.node.name.replace(/^__BOOL__(.+?)__$/, '$1') as keyof T | ||
if (!(variableName in variables)) { | ||
throw new Error(`Template variable '${variableName.toString()}' not defined`) | ||
} | ||
const value = variables[variableName] | ||
if (typeof value !== 'boolean') { | ||
throw new Error(`Expected boolean value for '${variableName.toString()}'`) | ||
} | ||
path.replaceWith({type: 'BooleanLiteral', value}) | ||
}, | ||
}, | ||
}), | ||
}) | ||
|
||
return print(ast, {quote: 'single'}).code | ||
} |
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.
📝 Confirm this is the key we want to use