Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Npm packages and update diff checker #326

Merged
merged 6 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Fetch Latest packages

on:
schedule:
- cron: "0 0 * * *"

jobs:
fetch-news:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: automation-scripts/npm-packages
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: "18.16.1"

- name: Install Yarn
run: |
corepack enable
corepack prepare yarn@stable --activate

- name: Install dependencies
run: yarn install --immutable

- name: Run
run: yarn start

- name: Move packages.json
run: |-
git add news.json
git mv -f news.json ../../api/packages.json

- name: Commit and push if changed
run: |-
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
git commit -m "Update packages.json" || true
git push
1 change: 0 additions & 1 deletion api/newsfeed/README.md

This file was deleted.

5 changes: 2 additions & 3 deletions api/newsfeed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const app = express();

app.use((req, res, next) => {
const environment = process.env.NODE_ENV || "development";
let origin =
environment === "development" ? "*" : "https://binarytree.dev";
let origin = environment === "development" ? "*" : "https://binarytree.dev";
res.header("Access-Control-Allow-Origin", origin);
next();
});
Expand All @@ -32,7 +31,7 @@ app.get("/rss", async (req, res) => {
throw new Error("Failed to fetch the Medium RSS feed");
}

res.setHeader('Cache-Control', 's-max-age=86400, stale-while-revalidate');
res.setHeader("Cache-Control", "s-max-age=86400, stale-while-revalidate");
res.set("Content-Type", "application/xml");

res.send(response.data);
Expand Down
3 changes: 3 additions & 0 deletions automation-scripts/News/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
# Documentation here: https://yarnpkg.com/features/zero-installs
!.yarn/cache
#.pnp.*
.yarn

node_modules
1 change: 0 additions & 1 deletion automation-scripts/README.md

This file was deleted.

15 changes: 15 additions & 0 deletions automation-scripts/npm-packages/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you don't wish to use zero-installs
# Documentation here: https://yarnpkg.com/features/zero-installs
!.yarn/cache
#.pnp.*
.yarn

node_modules

1 change: 1 addition & 0 deletions automation-scripts/npm-packages/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
17 changes: 16 additions & 1 deletion automation-scripts/npm-packages/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# TODO
```shell
yarn init -2

yarn add --dev typescript
yarn add --dev ts-node
yarn add --dev nodemon
yarn add fs

mkdir src
cd src
touch index.ts
```

```shell
yarn start
```
21 changes: 21 additions & 0 deletions automation-scripts/npm-packages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "npm-packages",
"packageManager": "[email protected]",
"scripts": {
"start": "ts-node src/index.ts",
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
"build": "tsc",
"watch": "tsc -w"
},
"dependencies": {
"fs": "^0.0.1-security",
"semver": "^7.5.4"
},
"devDependencies": {
"@types/node": "^20.6.3",
"@types/semver": "^7.5.2",
"nodemon": "^3.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
98 changes: 98 additions & 0 deletions automation-scripts/npm-packages/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import fs from "fs";
import semver from "semver";

interface PackageType {
key: string;
url: string;
version: string;
new: boolean;
}

interface ResponseType {
packages: PackageType[];
lastDate: number;
}

async function fetchData(url: string) {
try {
const response = await fetch(url);
const data = await response.json();

return data;
} catch (error) {
console.log("something went wrong");
}
}

function compareMajorVersion(latestVersion: string, previousVesrion: string) {
const latestMajor = semver.major(latestVersion);
const prevMajor = semver.major(previousVesrion);

return latestMajor > prevMajor;
}

async function fetchPackages(packageNames: string[]) {
try {
const packageList: PackageType[] = [];
let prevData = (await fetchData(
"https://raw.githubusercontent.com/lifeparticle/binarytree/main/api/packages.json"
)) as ResponseType;

for (const packageName of packageNames) {
const packageUrl = `https://registry.npmjs.org/${packageName}/latest`;
const latestPackage = await fetchData(packageUrl);

if (latestPackage) {
const prevPackage = prevData?.packages.find(
(info) => info.key === packageName
);

const isNew =
!prevPackage ||
compareMajorVersion(latestPackage.version, prevPackage.version);

const result = {
key: packageName,
url: `https://www.npmjs.com/package/${packageName}`,
version: latestPackage.version,
new: isNew,
};

packageList.push(result);
}
}

return {
packages: packageList,
lastDate: Date.now(),
};
} catch (error) {
console.log(error);
}
}

async function saveDataToFile(data: any, filename: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.writeFile(filename, JSON.stringify(data), (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

async function main() {
const packages = ["react", "svelte", "remix", "gatsby", "next", "vue"];
if (!packages) {
throw new Error("No package found");
}
const articles = await fetchPackages(packages);
await saveDataToFile(articles, "packages.json");
console.log("Data saved to packages.json");
}

main().catch((err) => {
console.error(err);
});
9 changes: 9 additions & 0 deletions automation-scripts/npm-packages/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
},
"types": ["node"]
}
Loading