-
Notifications
You must be signed in to change notification settings - Fork 1
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
Create write redirects build task #4
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import _ from 'lodash'; | ||
import Contentful, { getGlobalSettings } from '@last-rev/integration-contentful'; | ||
|
||
import writeFile from '../helpers/writeFile'; | ||
import { CONTENT_DIR, REDIRECTS_FILE } from '../constants'; | ||
import mkdirIfNotExists from '../helpers/mkDirIfNotExists'; | ||
import { BuildTask } from '../types'; | ||
|
||
type Redirect = { | ||
from: string; | ||
to: string; | ||
status?: number; | ||
force?: boolean; | ||
}; | ||
|
||
const writeRedirectFile = async (redirects: string): Promise<void> => { | ||
await writeFile(REDIRECTS_FILE, redirects); | ||
}; | ||
|
||
const writeRedirects: BuildTask = async (buildConfig, { adapterConfig }): Promise<void> => { | ||
const getSettings = buildConfig.useAdapter ? Contentful(adapterConfig).getGlobalSettings : getGlobalSettings; | ||
|
||
await mkdirIfNotExists(CONTENT_DIR); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const localeSettings: any = await getSettings({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to have anything to do specifically with locales. Maybe call this variable globalSettings, or just settings? |
||
include: _.get(buildConfig, 'settingsInclude'), | ||
contentTypeId: _.get(buildConfig, 'settingsContentType') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to make this more efficient, you should add a select argument: select: 'fields.redirects'. |
||
}); | ||
|
||
const { redirects }: { redirects: Array<Redirect> } = localeSettings; | ||
|
||
const result: Array<string> = []; | ||
|
||
if (_.isArray(redirects)) { | ||
_.each(redirects, (redirect: Redirect) => { | ||
let record = `${redirect.from} ${redirect.to}`; | ||
if (redirect.status) record = `${record} ${redirect.status}`; | ||
if (redirect.status && redirect.force) record = `${record}!`; | ||
result.push(record); | ||
}); | ||
} | ||
|
||
await writeRedirectFile(result.join('\n')); | ||
}; | ||
|
||
export default writeRedirects; |
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.
Since we are just parsing the response, I don't think we need to have both of these. If we don't need the adapter, we can just use the standard globalSettings request and parse it accordingly ({ fields : { redirects } }). Either way, whether you use the adapter or not, I would just stick to a single retrieval method. Because the line below (32) looks to me like it would fail if useAdapter != true...