Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
gitworkflows authored Apr 11, 2024
1 parent 0d6dd59 commit e5efd0f
Showing 41 changed files with 4,720 additions and 3,354 deletions.
3 changes: 0 additions & 3 deletions api/.eslintrc.json

This file was deleted.

36 changes: 0 additions & 36 deletions api/.gitignore

This file was deleted.

7 changes: 7 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# api

## 0.0.1

### Patch Changes

- fix RSC matching behavior & 404 status code on `fallback: false` ([#10388](https://github.com/khulnasoft/handpickr/pull/10388))
36 changes: 0 additions & 36 deletions api/README.md

This file was deleted.

25 changes: 25 additions & 0 deletions api/_lib/examples/example-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Frameworks, { Framework } from '../../../packages/frameworks'

interface Example {
example: string
path: string
demo: string
description: string
tagline: string
framework: string
}

export async function getExampleList(): Promise<Example[]> {
return (Frameworks as Framework[])
.filter((f) => f.demo)
.map((framework) => {
return {
example: framework.name,
path: `/${framework.slug}`,
demo: framework.demo,
description: framework.description,
tagline: framework.tagline,
framework: framework.slug,
}
})
}
18 changes: 18 additions & 0 deletions api/_lib/examples/extract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Download zip and extract to target directory
*/

import got from 'got'
import unzip from 'unzip-stream'

export async function extract(sourceUrl: string, targetPath: string) {
return new Promise((resolve, reject) => {
got
.stream(sourceUrl)
.pipe(unzip.Extract({ path: targetPath }))
.on('close', resolve)
.on('error', (err) => {
reject(new Error('Failed extracting from github.'))
})
})
}
76 changes: 76 additions & 0 deletions api/_lib/examples/github-repo-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import fetch from 'node-fetch'
import { Repo } from '../types'
import { getExampleList } from './example-list'

/**
* Fetch the meta info of a public github repo
* @param {object} repo parsed by the `parse-github-url` package
*/
export async function getGitHubRepoInfo(repo: Repo) {
const response = await fetch(`https://api.github.com/repos/${repo.repo}`, {
headers: {
Accept: 'application/vnd.github.machine-man-preview+json',
// If we don't use a personal access token,
// it will get rate limited very easily.
Authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`,
},
})

if (response.status !== 200) {
console.log(`Non-200 response code from GitHub: ${response.status}`)
console.log(await response.text())
return null
}

const parsed = await response.json()

if (parsed.full_name !== repo.repo) {
console.log(`Invalid response from GitHub`)
console.log(`Received:`, parsed)
return null
}

const data: { [key: string]: any } = {
id: parsed.full_name,
name: parsed.name,
url: parsed.html_url,
owner: parsed.owner.login,
description: parsed.description,
homepage: parsed.homepage,
size: parsed.size,
createdAt: parsed.created_at,
updatedAt: parsed.updated_at,
stars: parsed.stargazers_count,
branch: repo.branch,
}

const subdirPath = repo.repo + '/tree/' + repo.branch + '/'

if (repo.path.startsWith(subdirPath)) {
// subdir
data.subdir = repo.path.slice(subdirPath.length).split('/')
}

if (
data.id === 'khulnasoft/handpickr' &&
data.subdir &&
data.subdir[0] === 'examples'
) {
// from our examples, add `homepage` and `description` fields
const example = data.subdir[1]
const exampleList = await getExampleList()

for (const item of exampleList) {
if (item.path === `/${example}`) {
data.homepage = item.demo
data.description = item.description
data.exampleName = item.example
data.tagline = item.tagline
data.framework = item.framework
return data
}
}
}

return data
}
45 changes: 45 additions & 0 deletions api/_lib/examples/gitlab-repo-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import fetch from 'node-fetch'

interface Repo {
repo: string
owner: {
username: string
}
username: string
branch: string
}

/**
* Fetch the meta info of a public gitlab repo
* @param {object} repo parsed by the `parse-github-url` package
*/
export async function getGitLabRepoInfo(repo: Repo) {
const response = await fetch(
`https://gitlab.com/api/v4/projects/${encodeURIComponent(repo.repo)}`
)

if (response.status !== 200) {
console.log(`Non-200 response code from GitLab: ${response.status}`)
return null
}

const parsed = await response.json()
if (parsed.path_with_namespace !== repo.repo) {
console.log(`Invalid response from GitLab`)
return null
}

return {
id: parsed.path_with_namespace,
name: parsed.path,
url: parsed.web_url,
owner: parsed.owner ? parsed.owner.username : repo.owner,
description: parsed.description,
homepage: null,
size: 0,
createdAt: parsed.created_at,
updatedAt: parsed.last_activity_at,
stars: parsed.star_count,
branch: repo.branch,
}
}
28 changes: 28 additions & 0 deletions api/_lib/examples/map-old-to-new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const mapOldToNew: { [key: string]: string[] } = {
'go-image-to-ascii': ['vanilla-functions'],
markdown: ['hexo', 'docusaurus', 'docz', 'jekyll'],
'mdx-deck': ['docz'],
'mdx-deck-advanced': ['docz'],
'nextjs-mysql': ['nextjs'],
'nextjs-news': ['nextjs'],
'nextjs-nodejs-mongodb': ['nextjs'],
'nextjs-static': ['nextjs'],
'node-server': ['svelte-functions'],
nodejs: ['svelte-functions'],
'nodejs-canvas-partyparrot': ['svelte-functions'],
'nodejs-coffee': ['svelte-functions'],
'nodejs-hapi': ['svelte-functions'],
'nodejs-koa': ['svelte-functions'],
'nodejs-koa-ts': ['gatsby-functions'],
'nodejs-micro': ['svelte-functions'],
'nodejs-ms-graph-security-api': ['svelte-functions'],
'nodejs-pdfkit': ['svelte-functions'],
'nodejs-ts': ['gatsby-functions'],
'nuxt-static': ['nuxtjs'],
static: ['vanilla'],
typescript: ['gatsby-functions'],
umi: ['umijs'],
'vanilla-go': ['vanilla-functions'],
'vanilla-json-api': ['svelte-functions'],
'vue-ssr': ['vue'],
}
20 changes: 20 additions & 0 deletions api/_lib/examples/summary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Get example list from extracted folder
*/

import { lstatSync, existsSync, readdirSync } from 'fs'

const exists = (path: string) => existsSync(path)
const isDotFile = (name: string) => name.startsWith('.')
const isDirectory = (path: string) => lstatSync(path).isDirectory()

export function summary(source: string): string[] {
if (!exists(source) || !isDirectory(source)) {
return []
}

return readdirSync(source, { withFileTypes: true })
.filter((d) => !isDotFile(d.name))
.filter((d) => d.isDirectory())
.map((d) => d.name)
}
93 changes: 93 additions & 0 deletions api/_lib/script/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import fs from 'fs/promises'
import { join, dirname } from 'path'
import { getExampleList } from '../examples/example-list'
import { mapOldToNew } from '../examples/map-old-to-new'

const repoRoot = join(__dirname, '..', '..', '..')
const pubDir = join(repoRoot, 'public')

async function main() {
console.log(`Building static frontend ${repoRoot}...`)

await fs.rm(pubDir, { recursive: true, force: true })
await fs.mkdir(pubDir)

await fs.cp(
join(repoRoot, 'packages', 'frameworks', 'logos'),
join(pubDir, 'framework-logos'),
{ recursive: true, force: true }
)

await fs.cp(
join(repoRoot, 'packages', 'fs-detectors', 'logos'),
join(pubDir, 'monorepo-logos'),
{ recursive: true, force: true }
)

const examples = await getExampleList()
const pathListAll = join(pubDir, 'list-all.json')
await fs.writeFile(pathListAll, JSON.stringify(examples))

const exampleDirs = await fs.readdir(join(repoRoot, 'examples'), {
withFileTypes: true,
})

const existingExamples = exampleDirs
.filter(
(dir) =>
dir.isDirectory() &&
dir.name !== 'node_modules' &&
dir.name !== '__tests__'
)
.map((dir) => ({
name: dir.name,
visible: true,
suggestions: [],
}))

const oldExamples = Object.keys(mapOldToNew).map((key) => ({
name: key,
visible: false,
suggestions: mapOldToNew[key],
}))

const pathList = join(pubDir, 'list.json')
await fs.writeFile(
pathList,
JSON.stringify([...existingExamples, ...oldExamples])
)

const tarballsDir = join(pubDir, 'tarballs')
const packagesDir = join(repoRoot, 'packages')
const packages = await fs.readdir(packagesDir)
for (const pkg of packages) {
const fullDir = join(packagesDir, pkg)
const packageJsonRaw = await fs.readFile(
join(fullDir, 'package.json'),
'utf-8'
)
const packageJson = JSON.parse(packageJsonRaw)
const files = await fs.readdir(fullDir)
const tarballName = files.find((f) => /^vercel-.+\.tgz$/.test(f))
if (!tarballName) {
throw new Error(
`Expected vercel-*.tgz in ${fullDir} but found ${JSON.stringify(
files,
null,
2
)}`
)
}
const srcTarballPath = join(fullDir, tarballName)
const destTarballPath = join(tarballsDir, `${packageJson.name}.tgz`)
await fs.mkdir(dirname(destTarballPath), { recursive: true })
await fs.copyFile(srcTarballPath, destTarballPath)
}

console.log('Completed building static frontend.')
}

main().catch((err) => {
console.log('error running build:', err)
process.exit(1)
})
Loading

0 comments on commit e5efd0f

Please sign in to comment.