Skip to content

Commit

Permalink
fix(icons): use new api on icon downloader (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
deini authored Sep 17, 2019
1 parent 08b951c commit 6bae4ae
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions packages/big-design-icons/scripts/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ const inquirerAutocomplete = require('inquirer-autocomplete-prompt');

inquirer.registerPrompt('autocomplete', inquirerAutocomplete);

const BASE_URL = 'https://material.io/tools/icons/static';
const DEST_PATH = join(__dirname, '..', 'svgs', 'material');

async function downloadIcon(iconName) {
console.log(`Downloading icon: ${iconName}`);
async function downloadIcon(icon) {
console.log(`Downloading icon: ${icon.name}`);

const fileName = `round-${iconName}-24px.svg`;
const response = await fetch(`${BASE_URL}/icons/${fileName}`);
const response = await fetch(
`https://fonts.gstatic.com/s/i/materialiconsround/${icon.name}/v${icon.version}/24px.svg`,
);

if (response.status !== 200) {
throw new Error(`Error status: ${response.status}`);
}

const fileContent = await response.text();

await outputFile(join(DEST_PATH, `${iconName}.svg`), fileContent);
await outputFile(join(DEST_PATH, `${icon.name}.svg`), fileContent);
}

function iconExists(iconName) {
const iconFilePath = join(DEST_PATH, `${iconName}.svg`);
function iconExists(icon) {
const iconFilePath = join(DEST_PATH, `${icon.name}.svg`);

return pathExists(iconFilePath);
}

async function fetchIconList() {
const response = await fetch(`${BASE_URL}/data.json`);
const data = await response.json();
const icons = data.categories.reduce((acc, item) => acc.concat(item.icons), []);
const response = await fetch('https://fonts.google.com/metadata/icons');
const text = await response.text();
const { icons } = JSON.parse(text.replace(")]}'", ''));

return icons.map(({ id }) => id);
return icons;
}

(async () => {
Expand All @@ -45,14 +45,20 @@ async function fetchIconList() {
type: 'autocomplete',
name: 'icon',
message: 'Select an icon to download',
source: (_answersSoFar, input = '') => Promise.resolve(iconList.filter(icon => icon.startsWith(input)).sort()),
source: (_answersSoFar, input = '') =>
Promise.resolve(
iconList
.filter(icon => icon.name.startsWith(input))
.sort()
.map(icon => ({ name: icon.name, value: icon })),
),
},
]);

const iconAlreadyExists = await iconExists(icon);

if (iconAlreadyExists) {
console.log(`Icon "${icon}" already exists.`);
console.log(`Icon "${icon.name}" already exists.`);
} else {
await downloadIcon(icon);
}
Expand Down

0 comments on commit 6bae4ae

Please sign in to comment.