-
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.
refactor: align core-app structure and start-up more closely with studio
- Loading branch information
1 parent
56cee8b
commit a8cf34e
Showing
26 changed files
with
390 additions
and
265 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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
import {processTemplate} from './processTemplate' | ||
|
||
const defaultCoreAppTemplate = ` | ||
import {defineCliConfig} from 'sanity/cli' | ||
export default defineCliConfig({ | ||
__experimental_coreAppConfiguration: { | ||
framework: 'vite', | ||
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
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 | ||
} |
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
Oops, something went wrong.