Skip to content
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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import writePaths from './writePaths';
import writeMappings from './writeMappings';
import writeAdapterConfig from './writeAdapterConfig';
import writeLocaleData from './writeLocaleData';
import writeRedirects from './writeRedirects';
import { BuildTask, BuildConfig } from '../types';

const getBuildTasks = (buildConfig: BuildConfig): BuildTask[] => {
Expand All @@ -13,7 +14,7 @@ const getBuildTasks = (buildConfig: BuildConfig): BuildTask[] => {
if (buildConfig.writeMappings) out.push(writeMappings);
if (buildConfig.writeAdapterConfig) out.push(writeAdapterConfig);
if (buildConfig.writeLocaleData) out.push(writeLocaleData);

if (buildConfig.writeRedirects) out.push(writeRedirects);
return out;
};

Expand Down
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;
Copy link
Contributor

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...


await mkdirIfNotExists(CONTENT_DIR);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const localeSettings: any = await getSettings({
Copy link
Contributor

Choose a reason for hiding this comment

The 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')
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
1 change: 1 addition & 0 deletions packages/lastrev-build-prefetch-content/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const ADAPTER_CONFIG_FILE = join(CONTENT_DIR, 'adapterConfig.js');
export const MAPPING_TEMPLATE = readFileSync(MAPPING_TEMPLATE_FILE, 'utf-8');
export const SETTINGS_TEMPLATE = readFileSync(SETTINGS_TEMPLATE_FILE, 'utf-8');
export const I18N_JSON_FILE = resolve(PROJECT_ROOT, './i18n.json');
export const REDIRECTS_FILE = resolve(PROJECT_ROOT, './_redirects');
export const DEFAULT_LOCALIZATION_LOOKUP_FIELD_NAME = 'localizationLookup';
export const DEFAULT_RAW_PAGES_DIR = 'src/_pages';
export const DEFAULT_LOCALES_OUTPUT_PATH = 'locales';
1 change: 1 addition & 0 deletions packages/lastrev-build-prefetch-content/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type BuildConfig = {
writeMappings?: boolean;
writeAdapterConfig?: boolean;
writeLocaleData?: boolean;
writeRedirects?: boolean;
};

export type BuildTask = (buildConfig: BuildConfig, other: Record<string, unknown>) => Promise<void>;