Skip to content

Commit

Permalink
lint-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Aug 27, 2023
1 parent 56b6397 commit e58750b
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 135 deletions.
46 changes: 23 additions & 23 deletions examples/cms-sitecore-xmcloud/scripts/generate-component-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@ const componentRootPath = 'src/components'

const isWatch = process.argv.some((arg) => arg === '--watch')

;(isWatch ? watchComponentFactory : writeComponentFactory)()

/**
* Watches component directory for changes. When files are added or deleted, the component factory
* file (componentFactory.ts) is regenerated. This is used during `jss start` to pick up new or
* removed components at runtime.
*/
function watchComponentFactory() {
console.log(
`Watching for changes to component factory sources in ${componentRootPath}...`
)
function getComponentList(path: string): (PackageDefinition | ComponentFile)[] {
const components = getItems<PackageDefinition | ComponentFile>({
path,
resolveItem: (path, name) => ({
path: `${path}/${name}`,
componentName: name,
moduleName: name.replace(/[^\w]+/g, ''),
}),
cb: (name) => console.debug(`Registering JSS component ${name}`),
})

watchItems(componentRootPath, writeComponentFactory)
return components
}

/**
Expand Down Expand Up @@ -82,16 +81,17 @@ function writeComponentFactory() {
})
}

function getComponentList(path: string): (PackageDefinition | ComponentFile)[] {
const components = getItems<PackageDefinition | ComponentFile>({
path,
resolveItem: (path, name) => ({
path: `${path}/${name}`,
componentName: name,
moduleName: name.replace(/[^\w]+/g, ''),
}),
cb: (name) => console.debug(`Registering JSS component ${name}`),
})
/**
* Watches component directory for changes. When files are added or deleted, the component factory
* file (componentFactory.ts) is regenerated. This is used during `jss start` to pick up new or
* removed components at runtime.
*/
function watchComponentFactory() {
console.log(
`Watching for changes to component factory sources in ${componentRootPath}...`
)

return components
watchItems(componentRootPath, writeComponentFactory)
}

;(isWatch ? watchComponentFactory : writeComponentFactory)()
40 changes: 20 additions & 20 deletions examples/cms-sitecore-xmcloud/scripts/generate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,6 @@ const defaultConfig: JssConfig = {
graphQLEndpoint: process.env[`${constantCase('graphQLEndpoint')}`],
}

generateConfig(defaultConfig)

/**
* Generates the JSS config based on config plugins (under ./config/plugins)
* and then writes the config to disk.
* @param {JssConfig} defaultConfig Default configuration.
*/
function generateConfig(defaultConfig: JssConfig): void {
jssConfigFactory
.create(defaultConfig)
.then((config) => {
writeConfig(config)
})
.catch((e) => {
console.error('Error generating config')
console.error(e)
process.exit(1)
})
}

/**
* Writes the config object to disk with support for environment variables.
* @param {JssConfig} config JSS configuration to write.
Expand All @@ -61,3 +41,23 @@ const config = {};\n`
console.log(`Writing runtime config to ${configPath}`)
fs.writeFileSync(configPath, configText, { encoding: 'utf8' })
}

/**
* Generates the JSS config based on config plugins (under ./config/plugins)
* and then writes the config to disk.
* @param {JssConfig} defaultConfig Default configuration.
*/
function generateConfig(defaultConfig: JssConfig): void {
jssConfigFactory
.create(defaultConfig)
.then((config) => {
writeConfig(config)
})
.catch((e) => {
console.error('Error generating config')
console.error(e)
process.exit(1)
})
}

generateConfig(defaultConfig)
38 changes: 19 additions & 19 deletions examples/cms-sitecore-xmcloud/scripts/generate-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ const pluginDefinitions = [
},
]

run(pluginDefinitions)

function run(definitions: PluginDefinition[]) {
definitions.forEach((definition) => {
writePlugins(
definition.listPath,
definition.rootPath,
definition.moduleType
)
function getPluginList(path: string, pluginName: string): PluginFile[] {
const plugins = getItems<PluginFile>({
path,
resolveItem: (path, name) => ({
path: `${path}/${name}`,
name: `${name.replace(/-./g, (x) => x[1].toUpperCase())}Plugin`,
}),
cb: (name) => console.debug(`Registering ${pluginName} plugin ${name}`),
})

return plugins
}

/**
Expand Down Expand Up @@ -124,15 +125,14 @@ function writePlugins(
})
}

function getPluginList(path: string, pluginName: string): PluginFile[] {
const plugins = getItems<PluginFile>({
path,
resolveItem: (path, name) => ({
path: `${path}/${name}`,
name: `${name.replace(/-./g, (x) => x[1].toUpperCase())}Plugin`,
}),
cb: (name) => console.debug(`Registering ${pluginName} plugin ${name}`),
function run(definitions: PluginDefinition[]) {
definitions.forEach((definition) => {
writePlugins(
definition.listPath,
definition.rootPath,
definition.moduleType
)
})

return plugins
}

run(pluginDefinitions)
36 changes: 18 additions & 18 deletions examples/cms-sitecore-xmcloud/scripts/scaffold-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ const componentPath = regExResult[1]
const componentName = regExResult[2]
const filename = `${componentName}.tsx`

const componentOutputPath = scaffoldFile(
componentRootPath,
generateComponentSrc(componentName),
filename
)

console.log(
chalk.green(`
Scaffolding of ${componentName} complete.
Next steps:`)
)

if (componentOutputPath) {
console.log(
`* Implement the React component in ${chalk.green(componentOutputPath)}`
)
}

/**
* Force to use `crlf` line endings, we are using `crlf` across the project.
* Replace: `lf` (\n), `cr` (\r)
Expand Down Expand Up @@ -93,3 +75,21 @@ function scaffoldFile(
console.log(chalk.green(`File ${outputFile} has been scaffolded.`))
return outputFile
}

const componentOutputPath = scaffoldFile(
componentRootPath,
generateComponentSrc(componentName),
filename
)

console.log(
chalk.green(`
Scaffolding of ${componentName} complete.
Next steps:`)
)

if (componentOutputPath) {
console.log(
`* Implement the React component in ${chalk.green(componentOutputPath)}`
)
}
6 changes: 3 additions & 3 deletions examples/cms-sitecore-xmcloud/src/components/LinkList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ type LinkListItemProps = {

const LinkListItem = (props: LinkListItemProps) => {
let className = `item${props.index}`
className += (props.index + 1) % 2 == 0 ? ' even' : ' odd'
if (props.index == 0) {
className += (props.index + 1) % 2 === 0 ? ' even' : ' odd'
if (props.index === 0) {
className += ' first'
}
if (props.index + 1 == props.total) {
if (props.index + 1 === props.total) {
className += ' last'
}
return (
Expand Down
100 changes: 50 additions & 50 deletions examples/cms-sitecore-xmcloud/src/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ const getNavigationText = function (
return text
}

const getLinkTitle = (props: NavigationProps): string | undefined => {
let title
if (props.fields.NavigationTitle?.value) {
title = props.fields.NavigationTitle.value.toString()
} else if (props.fields.Title?.value) {
title = props.fields.Title.value.toString()
} else {
title = props.fields.DisplayName
}

return title
}

const getLinkField = (props: NavigationProps): LinkField => ({
value: {
href: props.fields.Href,
Expand All @@ -49,6 +62,43 @@ const getLinkField = (props: NavigationProps): LinkField => ({
},
})

const NavigationList = (props: NavigationProps) => {
const { sitecoreContext } = useSitecoreContext()

let children: JSX.Element[] = []
if (props.fields.Children && props.fields.Children.length) {
children = props.fields.Children.map((element: Fields, index: number) => (
<NavigationList
key={`${index}${element.Id}`}
fields={element}
handleClick={props.handleClick}
relativeLevel={props.relativeLevel + 1}
/>
))
}

return (
<li
className={props.fields.Styles.concat(
'rel-level' + props.relativeLevel
).join(' ')}
key={props.fields.Id}
tabIndex={0}
>
<div className="navigation-title">
<Link
field={getLinkField(props)}
editable={sitecoreContext.pageEditing}
onClick={props.handleClick}
>
{getNavigationText(props)}
</Link>
</div>
{children.length > 0 ? <ul className="clearfix">{children}</ul> : null}
</li>
)
}

export const Default = (props: NavigationProps): JSX.Element => {
const [isOpenMenu, openMenu] = useState(false)
const { sitecoreContext } = useSitecoreContext()
Expand Down Expand Up @@ -118,53 +168,3 @@ export const Default = (props: NavigationProps): JSX.Element => {
</div>
)
}

const NavigationList = (props: NavigationProps) => {
const { sitecoreContext } = useSitecoreContext()

let children: JSX.Element[] = []
if (props.fields.Children && props.fields.Children.length) {
children = props.fields.Children.map((element: Fields, index: number) => (
<NavigationList
key={`${index}${element.Id}`}
fields={element}
handleClick={props.handleClick}
relativeLevel={props.relativeLevel + 1}
/>
))
}

return (
<li
className={props.fields.Styles.concat(
'rel-level' + props.relativeLevel
).join(' ')}
key={props.fields.Id}
tabIndex={0}
>
<div className="navigation-title">
<Link
field={getLinkField(props)}
editable={sitecoreContext.pageEditing}
onClick={props.handleClick}
>
{getNavigationText(props)}
</Link>
</div>
{children.length > 0 ? <ul className="clearfix">{children}</ul> : null}
</li>
)
}

const getLinkTitle = (props: NavigationProps): string | undefined => {
let title
if (props.fields.NavigationTitle?.value) {
title = props.fields.NavigationTitle.value.toString()
} else if (props.fields.Title?.value) {
title = props.fields.Title.value.toString()
} else {
title = props.fields.DisplayName
}

return title
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as plugins from 'temp/page-props-factory-plugins'
export const isServerSidePropsContext = function (
context: GetServerSidePropsContext | GetStaticPropsContext
): context is GetServerSidePropsContext {
return (<GetServerSidePropsContext>context).req !== undefined
return (context as GetServerSidePropsContext).req !== undefined
}

export interface Plugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class NormalModePlugin implements Plugin {
props.layoutData = await layoutService.fetchLayoutData(
path,
props.locale,
// eslint-disable-next-line prettier/prettier
isServerSidePropsContext(context)
? (context as GetServerSidePropsContext).req
: undefined,
Expand Down

0 comments on commit e58750b

Please sign in to comment.