2023-03-01 13:26:56 +05:30
|
|
|
|
const fs = require( 'fs' );
|
|
|
|
|
const crypto = require( 'crypto' );
|
2022-10-03 12:44:45 -03:00
|
|
|
|
|
2023-03-01 13:26:56 +05:30
|
|
|
|
const API_URL = 'https://www.googleapis.com/webfonts/v1/webfonts?key=';
|
2022-10-03 12:44:45 -03:00
|
|
|
|
const API_KEY = process.env.GOOGLE_FONTS_API_KEY;
|
|
|
|
|
|
2023-03-01 13:26:56 +05:30
|
|
|
|
function calculateHash( somestring ) {
|
|
|
|
|
return crypto
|
|
|
|
|
.createHash( 'md5' )
|
|
|
|
|
.update( somestring )
|
|
|
|
|
.digest( 'hex' )
|
|
|
|
|
.toString();
|
2022-10-03 12:44:45 -03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 13:26:56 +05:30
|
|
|
|
async function updateFiles() {
|
|
|
|
|
let newApiData;
|
|
|
|
|
let newData;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
newApiData = await fetch( `${ API_URL }${ API_KEY }` );
|
|
|
|
|
newData = await newApiData.json();
|
|
|
|
|
} catch ( error ) {
|
2023-03-01 15:57:56 +05:30
|
|
|
|
// TODO: show in UI and remove console statement
|
|
|
|
|
// eslint-disable-next-line
|
2023-03-01 13:26:56 +05:30
|
|
|
|
console.error( '❎ Error fetching the Google Fonts API:', error );
|
|
|
|
|
process.exit( 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( newData.items ) {
|
|
|
|
|
const newDataString = JSON.stringify( newData, null, 2 );
|
|
|
|
|
|
|
|
|
|
const oldFileData = fs.readFileSync(
|
|
|
|
|
'./assets/google-fonts/fallback-fonts-list.json',
|
|
|
|
|
'utf8'
|
|
|
|
|
);
|
|
|
|
|
const oldData = JSON.parse( oldFileData );
|
|
|
|
|
const oldDataString = JSON.stringify( oldData, null, 2 );
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
calculateHash( newDataString ) !== calculateHash( oldDataString )
|
|
|
|
|
) {
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
|
'./assets/google-fonts/fallback-fonts-list.json',
|
|
|
|
|
newDataString
|
|
|
|
|
);
|
2023-03-01 15:57:56 +05:30
|
|
|
|
// TODO: show in UI and remove console statement
|
|
|
|
|
// eslint-disable-next-line
|
2023-03-01 13:26:56 +05:30
|
|
|
|
console.info( '✅ Google Fonts JSON file updated' );
|
|
|
|
|
} else {
|
2023-03-01 15:57:56 +05:30
|
|
|
|
// TODO: show in UI and remove console statement
|
|
|
|
|
// eslint-disable-next-line
|
2023-03-01 13:26:56 +05:30
|
|
|
|
console.info( 'ℹ️ Google Fonts JSON file is up to date' );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-03-01 15:57:56 +05:30
|
|
|
|
// TODO: show in UI and remove console statement
|
|
|
|
|
// eslint-disable-next-line
|
2023-03-01 13:26:56 +05:30
|
|
|
|
console.error(
|
|
|
|
|
'❎ No new data to check. Check the Google Fonts API key.'
|
|
|
|
|
);
|
|
|
|
|
process.exit( 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-03 12:44:45 -03:00
|
|
|
|
|
2023-03-01 13:26:56 +05:30
|
|
|
|
updateFiles();
|