Skip to content

Commit

Permalink
Merge branch 'main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins authored Dec 16, 2023
2 parents 5d91282 + 2b8257f commit f7ca823
Show file tree
Hide file tree
Showing 81 changed files with 6,766 additions and 14,225 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.svelte-kit
/build
/node_modules
43 changes: 33 additions & 10 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
module.exports = {
// @ts-check

/** @type {import('eslint').Linter.Config} */
const config = {
root: true,
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/stylistic',
'plugin:svelte/recommended',
'prettier'
],
plugins: ['@typescript-eslint', 'svelte'],
parser: '@typescript-eslint/parser',
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
plugins: ['svelte3', '@typescript-eslint'],
ignorePatterns: ['*.cjs'],
overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }],
settings: {
'svelte3/typescript': () => require('typescript')
},
parserOptions: {
project: true,
sourceType: 'module',
ecmaVersion: 2019
ecmaVersion: 2022,
extraFileExtensions: ['.svelte']
},
env: {
browser: true,
es2017: true,
es2022: true,
node: true
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser'
}
}
],
rules: {
'@typescript-eslint/array-type': 'off',
'@typescript-eslint/consistent-type-definitions': 'off',
'svelte/no-at-html-tags': 'off',
'svelte/valid-compile': 'off'
}
};

module.exports = config;
9 changes: 9 additions & 0 deletions .github/actions/update-stars/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Update stars
description: Update Github stars count
inputs:
token:
description: GitHub token for GraphQL calls
required: false
runs:
using: node20
main: 'main.js'
190 changes: 190 additions & 0 deletions .github/actions/update-stars/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import core from '@actions/core';
import { readFileSync, writeFileSync } from 'node:fs';
import { fetch } from 'undici';

const ghGraphQlUrl = 'https://api.github.com/graphql';
const gitlabGraphQlUrl = 'https://gitlab.com/api/graphql';
const githubNameRegexp = new RegExp(
'https://github.com/([a-zA-Z0-9][a-zA-Z0-9-]{0,38}/[a-zA-Z0-9._-]{1,100})'
);
const gitlabNameRegExp = new RegExp('https://gitlab.com/([\\w-]+/[\\w-]+)');

async function doGraphQlQuery(url, query, headers = {}) {
try {
let fetchResponse = await fetch(url, {
body: JSON.stringify({ query }),
method: 'POST',
headers: {
'content-type': 'application/json',
...headers
}
});
let data = await fetchResponse.json();
return Object.values(data.data || {});
} catch (e) {
console.error(e.message);
}
return [];
}

function gatherUrls() {
let components = JSON.parse(readFileSync('src/routes/components/components.json'));
let tools = JSON.parse(readFileSync('src/routes/tools/tools.json'));
let templates = JSON.parse(readFileSync('src/routes/templates/templates.json'));

return [
...components.map((component) => component.repository),
...tools.map((tool) => tool.repository),
...templates.map((template) => template.repository)
];
}

// Github

/**
* Get all GitHub repositories
* @returns {Array<{owner: string, repo: string}>}
*/
function getAllGHRepos() {
return gatherUrls()
.filter((url) => url !== false && githubNameRegexp.test(url))
.map((gitHubUrl) => gitHubUrl.match(githubNameRegexp)[1].toLowerCase())
.map((validName) => ({ owner: validName.split('/')[0], repo: validName.split('/')[1] }));
}

function ghRepoGraphQl({ owner, repo }) {
let identifier = owner + '_' + repo + '_' + Math.random() + '';
identifier = identifier.replace(/[^a-zA-Z0-9_]/g, '_');
identifier = identifier.replace(/^[0-9]/g, '_');
return `${identifier}: repository(name: "${repo}", owner: "${owner}"){nameWithOwner stargazerCount}`;
}

/**
* Divide an array into multiple smaller array
* @param {Array} input
* @param {number} size
* @return {Array<Array>}
*/
function chunk(input, size) {
size = size < 1 ? 10 : size;
const pages = Math.ceil(input.length / size);
const final = [];
for (let index = 0; index < pages; index++) {
final.push(input.slice(index * size, (index + 1) * size));
}
return final;
}

/**
* Get the number of stars for all GitHub repositories.
* The result is a Map where the key the repo name and the value is the number of stars.
* @returns {Promise<Record<string, number>>}
*/
async function getGHStars() {
const repoData = getAllGHRepos();
core.info('Found ' + repoData.length + ' repositories');
const pagedRepoData = chunk(repoData, 100);
const pageCount = pagedRepoData.length;
core.debug('Divide the repositories into ' + pageCount + ' pages (of 100 repositories)');
let lines = [];
for (let index = 0; index < pageCount; index++) {
const page = pagedRepoData[index];
core.debug('Running GraphQL for page ' + (index + 1) + '/' + pageCount);
let body =
'query{' + '\n' + page.map((repoInfo) => ghRepoGraphQl(repoInfo)).join('\n') + '\n' + '}';
lines = [
...lines,
...(await doGraphQlQuery(ghGraphQlUrl, body, {
authorization:
'Bearer ' +
core.getInput('token', {
// required: true,
trimWhitespace: true
})
}))
];
}
return Object.fromEntries(
lines
.filter((line) => line?.nameWithOwner)
.map((line) => [line.nameWithOwner.toLowerCase(), line.stargazerCount])
.sort()
);
}

// Gitlab

/**
* Get all GitLab repositories path (relative to GitLab root)
* @returns {Array<string>}
*/
function getAllGitlabRepos() {
return gatherUrls()
.filter((url) => url !== false && gitlabNameRegExp.test(url))
.map((url) => url.match(gitlabNameRegExp)[1]);
}

function gitlabRepoGraphQl(name) {
let identifier = name + '_' + Math.random() + '';
identifier = identifier.replace(/[^a-zA-Z0-9_]+/g, '_');
identifier = identifier.replace(/^[0-9]/g, '_');
return `${identifier}: project(fullPath: "${name}"){starCount fullPath}`;
}

/**
* Get the number of stars for all Gitlab repositories.
* The result is a Map where the key the repo name and the value is the number of stars.
* @returns {Promise<Record<string, number>>}
*/
async function getGitlabStars() {
const repoData = getAllGitlabRepos();
core.info('Found ' + repoData.length + ' repositories');
const pagedRepoData = chunk(repoData, 100);
const pageCount = pagedRepoData.length;
core.debug('Divide the repositories into ' + pageCount + ' pages (of 100 repositories)');
let lines = [];
for (let index = 0; index < pageCount; index++) {
const page = pagedRepoData[index];
core.debug('Running GraphQL for page ' + (index + 1) + '/' + pageCount);
const body =
'query{' + '\n' + page.map((repoInfo) => gitlabRepoGraphQl(repoInfo)).join('\n') + '\n' + '}';
lines = [...lines, ...(await doGraphQlQuery(gitlabGraphQlUrl, body))];
}
return Object.fromEntries(
lines
.filter((line) => line?.fullPath)
.map((line) => [line.fullPath.toLowerCase(), line.starCount])
.sort()
);
}

async function main() {
core.startGroup('GitHub');
const github = await getGHStars();
core.endGroup();

core.startGroup('GitLab');
const gitlab = await getGitlabStars();
core.endGroup();

core.info(
`\tGithub: ${Object.keys(github).length} repositories (${Object.values(github).reduce(
(count, item) => count + item,
0
)} stars)`
);
core.info(
`\tGitlab: ${Object.keys(gitlab).length} repositories (${Object.values(gitlab).reduce(
(count, item) => count + item,
0
)} stars)`
);
writeFileSync('src/lib/stars.json', JSON.stringify({ github, gitlab }));
}

try {
core.info('Start');
main().then(() => core.info('Done'));
} catch (error) {
core.setFailed(error);
}
20 changes: 12 additions & 8 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ name: Source code lint

on:
pull_request:
branches: [staging]
branches: [main]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup node with version 16
uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
version: 8
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
cache: 'pnpm'
- name: Install dependencies
run: npm ci
run: pnpm install --frozen-lockfile
- name: Run sync
run: pnpm run sync
- name: Run lint
run: npm run lint
run: pnpm run lint
37 changes: 37 additions & 0 deletions .github/workflows/update-stars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Update stars count

on:
schedule:
- cron: '0 0 * * 0' # At 00:00 on Sunday.
workflow_dispatch:

jobs:
update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
- name: Run script
uses: ./.github/actions/update-stars
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Run format
run: pnpm run format
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
commit-message: "(AUTO) Update stars"
title: "🤖 Update stars"
body: Update all stars count from Github and Gitlab
branch: ci-update-stars
add-paths: src/lib/stars.json
delete-branch: true
token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
12 changes: 0 additions & 12 deletions .prettierrc

This file was deleted.

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Svelte Society 2021 Website
# Svelte Society Website

Using SvelteKit!

Expand All @@ -7,19 +7,19 @@ Using SvelteKit!
In order to start a development server:

```bash
npm install
npm run dev
pnpm install
pnpm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
pnpm run start
```

## Building

Before creating a production version of your app, install an [adapter](https://kit.svelte.dev/docs#adapters) for your target environment. Then:

```bash
npm run build
pnpm run build
```

> You can preview the built app with `npm run preview`, regardless of whether you installed an adapter. This should _not_ be used to serve your app in production.
> You can preview the built app with `pnpm run preview`, regardless of whether you installed an adapter. This should _not_ be used to serve your app in production.
5 changes: 0 additions & 5 deletions jest.config.cjs

This file was deleted.

15 changes: 0 additions & 15 deletions jsconfig.json

This file was deleted.

Loading

0 comments on commit f7ca823

Please sign in to comment.