Skip to content

Instantly share code, notes, and snippets.

@panoply
Created April 28, 2019 08:05
Show Gist options
  • Save panoply/3ec057644891f8ef45fb9d915e5c8a73 to your computer and use it in GitHub Desktop.
Save panoply/3ec057644891f8ef45fb9d915e5c8a73 to your computer and use it in GitHub Desktop.
DatoCMS Icons SVG Select values

DatoCMS SVG Icons

This will add all .svg icons to a Single-line string field using the Accept only specified values required validator. The script will read all SVG files from with a directory and then update the correspondind model field with the values, you can then use them to select inline icons within model content.

require('dotenv').config() // read the `.env` file for API token
const fs = require('fs')
const path = require('path')
const { SiteClient } = require('datocms-client')
const field = '296844'
const client = new SiteClient(process.env.DATO_API_TOKEN)
const icons = fs
.readdirSync('./source/icons')
.map(i => path.extname(i) === '.svg' && i.substring(0, i.length - 4))
client.fields
.update(field, {
validators: {
enum: {
values: icons
}
}
})
.then(field => {
console.log(field)
})
.catch(error => {
console.log(error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment