Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
error-four-o-four committed Aug 12, 2024
1 parent 6e69bc8 commit 5e43a80
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 116 deletions.
File renamed without changes.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"templates/*"
],
"scripts": {
"format": "prettier --parser typescript \"((packages|sketches|templates)/**/*.ts|*.ts)\"",
"format": "prettier --parser typescript \"((sketches|templates)/**/*.ts|*.ts)\"",
"format:fix": "npm run format -- --write",
"format:fix:all": "npm run format:fix && npm run format:fix -ws --if-present",
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
Expand Down
2 changes: 1 addition & 1 deletion packages/cdn/dist/index.cjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";var v=Object.defineProperty;var r=(e,t)=>v(e,"name",{value:t,configurable:!0});var f=require("node:path"),c=require("node:fs");const j="libs.json",w={base:"https://cdnjs.cloudflare.com/ajax/libs",api:"https://api.cdnjs.com/libraries"},p={initial:["name","version","filename","sri"],version:["name","sri"]};function b(e){try{const t=c.readFileSync(e,"utf-8");return JSON.parse(t)}catch(t){l(t)}return null}r(b,"parseJson");function g(e,t){c.writeFileSync(e,JSON.stringify(t,null,2))}r(g,"writeJson");async function S(e){try{return await(await fetch(e)).json()}catch(t){l(t)}return null}r(S,"fetchRequest");function l(e){e instanceof Error&&console.log(e.message)}r(l,"logError");function $(e){const t=Object.entries(e),s=new Set;return t.forEach(([i,o])=>{const{filename:n}=o,a=Object.keys(o.versions);for(const y of a){const u=`/${i}/${y}/${n}`;s.add(u)}}),s}r($,"createLibraryKeys");async function O(e){const t=await h(e,null);return{latest:t.version,filename:t.filename,versions:{[t.version]:d(t,t.version)},updatedAt:new Date().toISOString()}}r(O,"createLibraryData");async function D(e,t,s){const i=new Date(e.updatedAt).valueOf(),o=Date.now()-i>1e3*60*60*24*31;if(!s&&!o&&e.latest)return console.log("Latest version %o of %o has already been fetched",e.latest,t),null;if(!s&&o){const n=await h(t,null);return n.version===e.latest&&n.version in e.versions?(console.log("Latest version %o of %o has already been fetched",e.latest,t),null):(e.latest=n.version,e.versions[n.version]=d(n,n.version),e.updatedAt=new Date().toISOString(),e)}if(s){if(Object.keys(e.versions).includes(s))return console.log("Version %o of %o has already been fetched",s,t),null;const a=await h(t,s);return e.versions[s]=d(a,s,e.filename),e}console.log("Something went wrong ..."),process.exit(1)}r(D,"updateLibraryData");async function h(e,t){const s=t?`${e}/${t}`:e,i=t?p.version:p.initial,o=`${w.api}/${s}?fields=${i.join(",")}`;console.log("Fetching data from %o ...",o);const n=await S(o);return n||(console.log("Could not fetch library data!"),process.exit(1)),"error"in n&&(console.log(n.status,n.message),process.exit(1)),n}r(h,"fetchLibraryData");function d(e,t,s){if(s="filename"in e?e.filename:s,!s)throw new Error("Filename of %o is required!");return{url:`${w.base}/${e.name}/${t}/${s}`,sri:typeof e.sri=="string"?e.sri:e.sri[s]}}r(d,"parseFetchedData");class k{static{r(this,"LibHandler")}path;file;data;keys;constructor(t){this.path=t,this.file=f.join(t,j),this.data=b(this.file)??{},this.keys=$(this.data)}async download(t,s,i){const o=f.join(this.path,t,s),n=f.resolve(o,i);c.existsSync(n)&&(console.log("Version %o of %o has already been downloaded",s,t),process.exit(0)),c.mkdirSync(o,{recursive:!0});try{const a=this.data[t].versions[s].url,u=await(await fetch(a)).text();c.writeFileSync(n,u,"utf-8")}catch(a){l(a),process.exit(1)}}async fetch(t,s){if(!Object.hasOwn(this.data,t)){const i=await O(t);i&&(this.data[t]=i)}if(s){const i=await D(this.data[t],t,s);i&&(this.data[t]=i)}g(this.file,this.data),s=s||this.data[t].latest,await this.download(t,s,this.data[t].filename),console.log("\u2705 Done")}has(t){return this.keys.has(t)}get(t){if(!this.keys.has(t))return null;const[s,i]=t.slice(1).split("/"),o=s in this.data?this.data[s]:null;return o?o.versions[i]??null:null}}exports.LibHandler=k,exports.logError=l,exports.parseJson=b,exports.writeJson=g;
"use strict";var v=Object.defineProperty;var n=(s,t)=>v(s,"name",{value:t,configurable:!0});var u=require("node:path"),c=require("node:fs");const j="libs.json",p={base:"https://cdnjs.cloudflare.com/ajax/libs",api:"https://api.cdnjs.com/libraries"},w={initial:["name","version","filename","sri"],version:["name","sri"]};function b(s){try{const t=c.readFileSync(s,"utf-8");return JSON.parse(t)}catch(t){f(t)}return null}n(b,"parseJson");function g(s,t){c.writeFileSync(s,JSON.stringify(t,null,2))}n(g,"writeJson");async function S(s){try{return await(await fetch(s)).json()}catch(t){f(t)}return null}n(S,"fetchRequest");function f(s){s instanceof Error&&console.log(s.message)}n(f,"logError");function $(s){const t=Object.entries(s),e=new Set;return t.forEach(([i,a])=>{const{filename:o}=a,r=Object.keys(a.versions);for(const l of r){const d=`/${i}/${l}/${o}`;e.add(d)}}),e}n($,"createLibraryKeys");async function h(s,t){const e=t?`${s}/${t}`:s,i=t?w.version:w.initial,a=`${p.api}/${e}?fields=${i.join(",")}`;console.log("Fetching data from %o ...",a);const o=await S(a);return o||(console.log("Could not fetch library data!"),process.exit(1)),"error"in o&&(console.log(o.status,o.message),process.exit(1)),o}n(h,"fetchLibraryData");function y(s,t,e){if(e="filename"in s?s.filename:e,!e)throw new Error("Filename of %o is required!");return{url:`${p.base}/${s.name}/${t}/${e}`,sri:typeof s.sri=="string"?s.sri:s.sri[e]}}n(y,"parseFetchedData");class x{static{n(this,"LibHandler")}path;file;data;keys;constructor(t){this.path=t,this.file=u.join(t,j),this.data=b(this.file)??{},this.keys=$(this.data)}async createLibraryData(t){const e=await h(t,null);this.data[t]={latest:e.version,filename:e.filename,versions:{[e.version]:y(e,e.version)},updatedAt:new Date().toISOString()}}async updateLibraryData(t,e){const i=this.data[t],a=new Date(i.updatedAt).valueOf(),o=Date.now()-a>1e3*60*60*24*31;if(!e&&!o&&i.latest&&(console.log("Latest version %o of %o has already been fetched",i.latest,t),process.exit(0)),!e&&o){const r=await h(t,null);r.version===i.latest&&r.version in i.versions&&(console.log("Latest version %o of %o has already been fetched",i.latest,t),process.exit(0)),i.latest=r.version,i.versions[r.version]=y(r,r.version),i.updatedAt=new Date().toISOString(),this.data[t]=i;return}if(e){Object.keys(i.versions).includes(e)&&(console.log("Version %o of %o has already been fetched",e,t),process.exit(0));const l=await h(t,e);this.data[t].versions[e]=y(l,e,i.filename);return}console.log("Something went wrong ..."),process.exit(1)}async download(t,e,i){const a=u.join(this.path,t,e),o=u.resolve(a,i);c.existsSync(o)&&(console.log("Version %o of %o has already been downloaded",e,t),process.exit(0)),c.mkdirSync(a,{recursive:!0});try{const r=this.data[t].versions[e].url,d=await(await fetch(r)).text();c.writeFileSync(o,d,"utf-8")}catch(r){f(r),process.exit(1)}}async fetch(t,e){Object.hasOwn(this.data,t)||await this.createLibraryData(t),e&&await this.updateLibraryData(t,e),g(this.file,this.data),e=e||this.data[t].latest,await this.download(t,e,this.data[t].filename),console.log("\u2705 Done")}has(t){return this.keys.has(t)}get(t){if(!this.keys.has(t))return null;const[e,i]=t.slice(1).split("/"),a=e in this.data?this.data[e]:null;return a?a.versions[i]??null:null}}exports.LibHandler=x,exports.logError=f,exports.parseJson=b,exports.writeJson=g;
45 changes: 20 additions & 25 deletions packages/cdn/dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
declare const url: {
readonly base: 'https://cdnjs.cloudflare.com/ajax/libs';
readonly api: 'https://api.cdnjs.com/libraries';
readonly base: "https://cdnjs.cloudflare.com/ajax/libs";
readonly api: "https://api.cdnjs.com/libraries";
};

type Libraries = Record<string, LibraryData>;
type LibraryData = {
latest: string;
versions: Record<string, LibraryDataItem>;
filename: string;
updatedAt: string;
latest: string;
versions: Record<string, LibraryDataItem>;
filename: string;
updatedAt: string;
};
type LibraryDataItem = {
url: LibraryUrl;
sri: string;
url: LibraryUrl;
sri: string;
};
type LibrarySrc = `/${string}/${string}/${string}`;
type LibraryUrl = `${typeof url.base}${LibrarySrc}`;

declare class LibHandler {
private path;
private file;
private data;
private keys;
constructor(path: string);
private download;
fetch(name: string, version: string | null): Promise<void>;
has(src: string): boolean;
get(src: string): LibraryDataItem | null;
private path;
private file;
private data;
private keys;
constructor(path: string);
private createLibraryData;
private updateLibraryData;
private download;
fetch(name: string, version: string | null): Promise<void>;
has(src: string): boolean;
get(src: string): LibraryDataItem | null;
}

declare function parseJson(path: string): any;
declare function writeJson(path: string, data: unknown): void;
declare function logError(error: unknown): void;

export {
LibHandler,
type Libraries,
type LibraryDataItem as LibraryData,
logError,
parseJson,
writeJson,
};
export { LibHandler, type Libraries, type LibraryDataItem as LibraryData, logError, parseJson, writeJson };
2 changes: 1 addition & 1 deletion packages/cdn/dist/index.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var j=Object.defineProperty;var r=(e,t)=>j(e,"name",{value:t,configurable:!0});import f from"node:path";import u,{readFileSync as v,writeFileSync as S}from"node:fs";const $="libs.json",p={base:"https://cdnjs.cloudflare.com/ajax/libs",api:"https://api.cdnjs.com/libraries"},w={initial:["name","version","filename","sri"],version:["name","sri"]};function b(e){try{const t=v(e,"utf-8");return JSON.parse(t)}catch(t){c(t)}return null}r(b,"parseJson");function g(e,t){S(e,JSON.stringify(t,null,2))}r(g,"writeJson");async function O(e){try{return await(await fetch(e)).json()}catch(t){c(t)}return null}r(O,"fetchRequest");function c(e){e instanceof Error&&console.log(e.message)}r(c,"logError");function x(e){const t=Object.entries(e),s=new Set;return t.forEach(([o,i])=>{const{filename:n}=i,a=Object.keys(i.versions);for(const y of a){const l=`/${o}/${y}/${n}`;s.add(l)}}),s}r(x,"createLibraryKeys");async function D(e){const t=await h(e,null);return{latest:t.version,filename:t.filename,versions:{[t.version]:d(t,t.version)},updatedAt:new Date().toISOString()}}r(D,"createLibraryData");async function k(e,t,s){const o=new Date(e.updatedAt).valueOf(),i=Date.now()-o>1e3*60*60*24*31;if(!s&&!i&&e.latest)return console.log("Latest version %o of %o has already been fetched",e.latest,t),null;if(!s&&i){const n=await h(t,null);return n.version===e.latest&&n.version in e.versions?(console.log("Latest version %o of %o has already been fetched",e.latest,t),null):(e.latest=n.version,e.versions[n.version]=d(n,n.version),e.updatedAt=new Date().toISOString(),e)}if(s){if(Object.keys(e.versions).includes(s))return console.log("Version %o of %o has already been fetched",s,t),null;const a=await h(t,s);return e.versions[s]=d(a,s,e.filename),e}console.log("Something went wrong ..."),process.exit(1)}r(k,"updateLibraryData");async function h(e,t){const s=t?`${e}/${t}`:e,o=t?w.version:w.initial,i=`${p.api}/${s}?fields=${o.join(",")}`;console.log("Fetching data from %o ...",i);const n=await O(i);return n||(console.log("Could not fetch library data!"),process.exit(1)),"error"in n&&(console.log(n.status,n.message),process.exit(1)),n}r(h,"fetchLibraryData");function d(e,t,s){if(s="filename"in e?e.filename:s,!s)throw new Error("Filename of %o is required!");return{url:`${p.base}/${e.name}/${t}/${s}`,sri:typeof e.sri=="string"?e.sri:e.sri[s]}}r(d,"parseFetchedData");class L{static{r(this,"LibHandler")}path;file;data;keys;constructor(t){this.path=t,this.file=f.join(t,$),this.data=b(this.file)??{},this.keys=x(this.data)}async download(t,s,o){const i=f.join(this.path,t,s),n=f.resolve(i,o);u.existsSync(n)&&(console.log("Version %o of %o has already been downloaded",s,t),process.exit(0)),u.mkdirSync(i,{recursive:!0});try{const a=this.data[t].versions[s].url,l=await(await fetch(a)).text();u.writeFileSync(n,l,"utf-8")}catch(a){c(a),process.exit(1)}}async fetch(t,s){if(!Object.hasOwn(this.data,t)){const o=await D(t);o&&(this.data[t]=o)}if(s){const o=await k(this.data[t],t,s);o&&(this.data[t]=o)}g(this.file,this.data),s=s||this.data[t].latest,await this.download(t,s,this.data[t].filename),console.log("\u2705 Done")}has(t){return this.keys.has(t)}get(t){if(!this.keys.has(t))return null;const[s,o]=t.slice(1).split("/"),i=s in this.data?this.data[s]:null;return i?i.versions[o]??null:null}}export{L as LibHandler,c as logError,b as parseJson,g as writeJson};
var j=Object.defineProperty;var r=(s,t)=>j(s,"name",{value:t,configurable:!0});import d from"node:path";import h,{readFileSync as v,writeFileSync as S}from"node:fs";const x="libs.json",p={base:"https://cdnjs.cloudflare.com/ajax/libs",api:"https://api.cdnjs.com/libraries"},w={initial:["name","version","filename","sri"],version:["name","sri"]};function b(s){try{const t=v(s,"utf-8");return JSON.parse(t)}catch(t){l(t)}return null}r(b,"parseJson");function g(s,t){S(s,JSON.stringify(t,null,2))}r(g,"writeJson");async function $(s){try{return await(await fetch(s)).json()}catch(t){l(t)}return null}r($,"fetchRequest");function l(s){s instanceof Error&&console.log(s.message)}r(l,"logError");function D(s){const t=Object.entries(s),e=new Set;return t.forEach(([i,o])=>{const{filename:a}=o,n=Object.keys(o.versions);for(const c of n){const f=`/${i}/${c}/${a}`;e.add(f)}}),e}r(D,"createLibraryKeys");async function u(s,t){const e=t?`${s}/${t}`:s,i=t?w.version:w.initial,o=`${p.api}/${e}?fields=${i.join(",")}`;console.log("Fetching data from %o ...",o);const a=await $(o);return a||(console.log("Could not fetch library data!"),process.exit(1)),"error"in a&&(console.log(a.status,a.message),process.exit(1)),a}r(u,"fetchLibraryData");function y(s,t,e){if(e="filename"in s?s.filename:e,!e)throw new Error("Filename of %o is required!");return{url:`${p.base}/${s.name}/${t}/${e}`,sri:typeof s.sri=="string"?s.sri:s.sri[e]}}r(y,"parseFetchedData");class O{static{r(this,"LibHandler")}path;file;data;keys;constructor(t){this.path=t,this.file=d.join(t,x),this.data=b(this.file)??{},this.keys=D(this.data)}async createLibraryData(t){const e=await u(t,null);this.data[t]={latest:e.version,filename:e.filename,versions:{[e.version]:y(e,e.version)},updatedAt:new Date().toISOString()}}async updateLibraryData(t,e){const i=this.data[t],o=new Date(i.updatedAt).valueOf(),a=Date.now()-o>1e3*60*60*24*31;if(!e&&!a&&i.latest&&(console.log("Latest version %o of %o has already been fetched",i.latest,t),process.exit(0)),!e&&a){const n=await u(t,null);n.version===i.latest&&n.version in i.versions&&(console.log("Latest version %o of %o has already been fetched",i.latest,t),process.exit(0)),i.latest=n.version,i.versions[n.version]=y(n,n.version),i.updatedAt=new Date().toISOString(),this.data[t]=i;return}if(e){Object.keys(i.versions).includes(e)&&(console.log("Version %o of %o has already been fetched",e,t),process.exit(0));const c=await u(t,e);this.data[t].versions[e]=y(c,e,i.filename);return}console.log("Something went wrong ..."),process.exit(1)}async download(t,e,i){const o=d.join(this.path,t,e),a=d.resolve(o,i);h.existsSync(a)&&(console.log("Version %o of %o has already been downloaded",e,t),process.exit(0)),h.mkdirSync(o,{recursive:!0});try{const n=this.data[t].versions[e].url,f=await(await fetch(n)).text();h.writeFileSync(a,f,"utf-8")}catch(n){l(n),process.exit(1)}}async fetch(t,e){Object.hasOwn(this.data,t)||await this.createLibraryData(t),e&&await this.updateLibraryData(t,e),g(this.file,this.data),e=e||this.data[t].latest,await this.download(t,e,this.data[t].filename),console.log("\u2705 Done")}has(t){return this.keys.has(t)}get(t){if(!this.keys.has(t))return null;const[e,i]=t.slice(1).split("/"),o=e in this.data?this.data[e]:null;return o?o.versions[i]??null:null}}export{O as LibHandler,l as logError,b as parseJson,g as writeJson};
6 changes: 5 additions & 1 deletion packages/cdn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"license": "MIT",
"private": true,
"scripts": {
"format": "prettier --parser typescript \"src/**/*.ts\"",
"format:fix": "npm run format -- --write",
"dev": "pkgroll --watch",
"build": "pkgroll --clean-dist --minify"
},
Expand All @@ -16,6 +18,8 @@
}
},
"devDependencies": {
"pkgroll": "^2.4.2"
"@internal/cli": "^0.0.1",
"pkgroll": "^2.4.2",
"prettier": "^3.3.3"
}
}
92 changes: 81 additions & 11 deletions packages/cdn/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import { filename } from './config.js';
import { parseJson, writeJson, logError } from './utils.js';

import {
createLibraryData,
createLibraryKeys,
updateLibraryData,
fetchLibraryData,
parseFetchedData,
} from './helpers.js';

import type { Libraries, LibrarySrc } from './types.js';
import type {
Libraries,
LibrarySrc,
InitialJson,
VersionJson,
} from './types.js';

export class LibHandler {
private path: string;
Expand All @@ -26,6 +31,77 @@ export class LibHandler {
this.keys = createLibraryKeys(this.data);
}

private async createLibraryData(name: string) {
const fetched = await fetchLibraryData<InitialJson>(name, null);

this.data[name] = {
latest: fetched.version,
filename: fetched.filename,
versions: {
[fetched.version]: parseFetchedData(fetched, fetched.version),
},
updatedAt: new Date().toISOString(),
};
}

private async updateLibraryData(name: string, version: string | null) {
const lib = this.data[name];
const updatedAt = new Date(lib.updatedAt).valueOf();
const outdated = Date.now() - updatedAt > 1000 * 60 * 60 * 24 * 31;

if (!version && !outdated && lib.latest) {
console.log(
'Latest version %o of %o has already been fetched',
lib.latest,
name
);
process.exit(0);
}

if (!version && outdated) {
const fetched = await fetchLibraryData<InitialJson>(name, null);

if (fetched.version === lib.latest && fetched.version in lib.versions) {
console.log(
'Latest version %o of %o has already been fetched',
lib.latest,
name
);
process.exit(0);
}

/** @todo test! */
lib.latest = fetched.version;
lib.versions[fetched.version] = parseFetchedData(
fetched,
fetched.version
);
lib.updatedAt = new Date().toISOString();
this.data[name] = lib;
return;
}

if (version) {
const versions = Object.keys(lib.versions);

if (versions.includes(version)) {
console.log('Version %o of %o has already been fetched', version, name);
process.exit(0);
}

const fetched = await fetchLibraryData<VersionJson>(name, version);
this.data[name].versions[version] = parseFetchedData(
fetched,
version,
lib.filename
);
return;
}

console.log('Something went wrong ...');
process.exit(1);
}

private async download(name: string, version: string, filename: string) {
const path = np.join(this.path, name, version);
const file = np.resolve(path, filename);
Expand Down Expand Up @@ -54,17 +130,11 @@ export class LibHandler {

public async fetch(name: string, version: string | null) {
if (!Object.hasOwn(this.data, name)) {
const result = await createLibraryData(name);
if (result) {
this.data[name] = result;
}
await this.createLibraryData(name);
}

if (version) {
const result = await updateLibraryData(this.data[name], name, version);
if (result) {
this.data[name] = result;
}
await this.updateLibraryData(name, version);
}

writeJson(this.file, this.data);
Expand Down
Loading

0 comments on commit 5e43a80

Please sign in to comment.