-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add non-studio app template (#8394)
- Loading branch information
1 parent
a6bd3af
commit 0d4e40d
Showing
32 changed files
with
641 additions
and
154 deletions.
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.