Skip to content

Instantly share code, notes, and snippets.

@marcveens
Last active February 28, 2023 12:09
Show Gist options
  • Select an option

  • Save marcveens/c723f6ce70f7f231ebde323b71ce7954 to your computer and use it in GitHub Desktop.

Select an option

Save marcveens/c723f6ce70f7f231ebde323b71ce7954 to your computer and use it in GitHub Desktop.
Typescript used to generate a Netlify CMS config.yml
// See https://www.marcveens.nl/netlify-cms-generate-config-yml
import { NetlifyCmsConfig, NetlifyCmsCollection, NetlifyCmsField } from './types/NetlifyCmsConfig';
import * as YAML from 'yaml';
import * as fs from 'fs';
import * as path from 'path';
const defaultMetaFields: NetlifyCmsField[] = [
{ label: 'Title', name: 'title', widget: 'string' },
{ label: 'Page URL', name: 'url', widget: 'string' },
{
label: 'Meta', name: 'meta', widget: 'object', fields: [
{ label: 'Title', name: 'title', widget: 'string' },
{ label: 'Description', name: 'description', widget: 'string' }
]
}
];
const demoPage: NetlifyCmsCollection = {
name: 'demopage',
label: 'Demo page',
folder: 'src/pages/demopage',
create: true,
delete: true,
extension: 'md',
slug: '{{url}}',
fields: [
{ label: 'Template Key', name: 'templateKey', widget: 'hidden', default: 'demoPage' },
...defaultMetaFields,
{ label: 'Body', name: 'body', widget: 'markdown' },
]
};
const config: NetlifyCmsConfig = {
backend: {
name: 'git-gateway',
branch: 'master'
},
media_folder: 'static/img',
public_folder: '/img',
publish_mode: 'editorial_workflow',
logo_url: 'https://example.com/logo.png',
collections: [
demoPage
]
};
// To generate the end file
const configDisclaimer = '# This file is generated\n# Only edit the config.src.ts file\n\n';
const configString = `${configDisclaimer}${YAML.stringify(config)}`;
fs.writeFile(path.resolve(__dirname, 'config.yml'), configString, (err) => {
if (err) console.log(err);
console.log('Generated the config.yml');
});
// See https://www.marcveens.nl/netlify-cms-generate-config-yml
// https://www.netlifycms.org/docs/configuration-options/
type PublishMode = 'simple' | 'editorial_workflow';
type ExtensionType = 'yml' | 'yaml' | 'toml' | 'json' | 'md' | 'markdown' | 'html';
type FormatType = 'yml' | 'yaml' | 'toml' | 'json' | 'frontmatter' | 'yaml-frontmatter' | 'toml-frontmatter' | 'json-frontmatter';
type WidgetType = 'boolean' | 'date' | 'datetime' | 'file' | 'hidden' | 'image'
| 'list' | 'map' | 'markdown' | 'number' | 'object' | 'relation'
| 'select' | 'string' | 'text' | string;
type MapType = 'Point' | 'LineString' | 'Polygon';
type MarkdownButtonType = 'bold' | 'italic' | 'code' | 'link' | 'heading-one' | 'heading-two'
| 'quote' | 'code-block' | 'bulleted-list' | 'numbered-list';
type ValueType = 'int' | 'float';
export type NetlifyCmsField = {
name: string;
label?: string;
widget: WidgetType;
default?: string | string[] | number;
required?: boolean;
hint?: string;
pattern?: string;
// date | datetime
format?: string;
dateFormat?: boolean | string;
timeFormat?: boolean | string;
// file | image
media_library?: {
config: {
multiple?: boolean;
}
};
// list | object
allow_add?: boolean;
field?: NetlifyCmsField;
fields?: NetlifyCmsField[]; // actually required in case of object
// map
type?: MapType;
// markdown
buttons?: MarkdownButtonType[];
// number
valueType?: ValueType | string;
min?: number;
max?: number;
step?: number;
// relation
collection?: string;
displayFields?: string[];
searchFields?: string;
valueField?: string;
multiple?: boolean;
// select
options?: string[] | { label: string, value: string }[];
};
export type NetlifyCmsCollection = {
name: string;
identifier_field?: string;
label?: string;
label_singular?: string;
description?: string;
files?: string;
folder?: string;
filter?: string;
create?: boolean;
delete?: boolean;
extension?: ExtensionType;
format?: FormatType;
frontmatter_delimiter?: string | string[];
slug?: string;
preview_path?: string;
fields: NetlifyCmsField[];
editor?: boolean;
summary?: string;
}
export type NetlifyCmsConfig = {
backend: {
name: string;
repo?: string;
accept_roles?: string[];
branch?: string;
api_root?: string;
site_domain?: string;
base_url?: string;
auth_endpoint?: string;
};
publish_mode?: PublishMode;
media_folder: string;
public_folder?: string;
media_library?: {
name: string;
config?: {
publicKey?: string;
}
};
site_url?: string;
display_url?: string;
logo_url?: string;
show_preview_links?: boolean;
slug?: {
encoding?: string;
clean_accents?: boolean;
sanitize_replacement?: string;
};
collections: NetlifyCmsCollection[];
}
{
"scripts": {
"generate-config-yml": "ts-node static/admin/config.src.ts"
}
}
@Sacramentix
Copy link

Sacramentix commented Nov 10, 2022

Hey thanks, you already made what i was looking for!

Meaby we could upload it to npm or on https://github.com/DefinitelyTyped/DefinitelyTyped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment