Skip to content

Commit

Permalink
Edit stats script
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanqing committed Dec 30, 2023
1 parent 795ac84 commit 61a2f51
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"scrape-plugins": "tsx scripts/scrape/scrape.ts plugin globals/plugins.json 5",
"scrape-widgets": "tsx scripts/scrape/scrape.ts widget globals/widgets.json 5",
"serve": "sirv build --dev --host --port 4242",
"stats-plugins": "tsx scripts/fetch-stats.ts plugin globals/plugins.json",
"stats-widgets": "tsx scripts/fetch-stats.ts widget globals/widgets.json",
"stats-plugins": "tsx scripts/fetch-stats.ts plugin globals/plugins.json scripts/scrape/ignored-ids/plugins.json",
"stats-widgets": "tsx scripts/fetch-stats.ts widget globals/widgets.json scripts/scrape/ignored-ids/widgets.json",
"prewebsite-build": "npm run clean",
"website-build": "build-website --minify",
"prewebsite-watch": "npm run clean",
Expand Down
20 changes: 13 additions & 7 deletions packages/website/scripts/fetch-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ async function main() {
throw new Error('Need a file path')
}
const filePath = args[1]
const ids: Array<string> = JSON.parse(
await fs.readFile(filePath, 'utf8')
).map(function (item: { id: string }) {
return item.id
})
const ignoredIdsFilePath = args[2]
const ids: Array<string> = JSON.parse(await fs.readFile(filePath, 'utf8'))
.map(function (item: { id: string }) {
return item.id
})
.concat(JSON.parse(await fs.readFile(ignoredIdsFilePath, 'utf8')))
const stats = await fetchStatsAsync(ids, type)
printStats(stats, type)
} catch (error: any) {
Expand Down Expand Up @@ -77,8 +78,12 @@ async function fetchStatAsync(
}

function printStats(stats: Array<Stat>, type: 'plugin' | 'widget'): void {
const data = stats.map(function ({ id, name, runCount, likeCount }: Stat) {
const data = stats.map(function (
{ id, name, runCount, likeCount }: Stat,
index: number
) {
return [
new Intl.NumberFormat().format(index + 1),
name.length > NAME_MAX_LENGTH
? `${name.slice(0, NAME_MAX_LENGTH).trim()}…`
: name,
Expand All @@ -87,7 +92,7 @@ function printStats(stats: Array<Stat>, type: 'plugin' | 'widget'): void {
`https://figma.com/community/${type}/${id}`
]
})
data.splice(0, 0, ['name', 'runs', 'likes', 'url'])
data.splice(0, 0, ['', 'name', 'runs', 'likes', 'url'])
const totalRunCount = stats.reduce(function (
total: number,
{ runCount }: Stat
Expand All @@ -101,6 +106,7 @@ function printStats(stats: Array<Stat>, type: 'plugin' | 'widget'): void {
return total + likeCount
}, 0)
data.push([
'',
'',
new Intl.NumberFormat().format(totalRunCount),
new Intl.NumberFormat().format(totalLikeCount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"1196638111314691410",
"1203372504182342503",
"1206354900231909165",
"1206354900231909165",
"1209233052747647603",
"1211504102085754443",
"1212381421658754793",
Expand Down
1 change: 1 addition & 0 deletions packages/website/scripts/scrape/ignored-ids/widgets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
8 changes: 6 additions & 2 deletions packages/website/scripts/scrape/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const CONCURRENCY = 10
const __dirname = dirname(fileURLToPath(import.meta.url))

const invalidIdsFilePath = join(__dirname, 'invalid-ids.json')
const ignoredIdsFilePath = join(__dirname, 'ignored-ids.json')
const ignoredPluginIdsFilePath = join(__dirname, 'ignored-ids', 'plugins.json')
const ignoredWidgetIdsFilePath = join(__dirname, 'ignored-ids', 'widgets.json')

async function main(): Promise<void> {
try {
Expand All @@ -33,7 +34,10 @@ async function main(): Promise<void> {
await fs.readFile(invalidIdsFilePath, 'utf8')
)
const ignoredIds: Array<string> = JSON.parse(
await fs.readFile(ignoredIdsFilePath, 'utf8')
await fs.readFile(
type === 'plugin' ? ignoredPluginIdsFilePath : ignoredWidgetIdsFilePath,
'utf8'
)
)
const data: Array<Record<string, any>> = await fetchDataAsync({
maxPages,
Expand Down

0 comments on commit 61a2f51

Please sign in to comment.