Skip to content

Commit

Permalink
feat: use @internal/utils pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
error-four-o-four committed Aug 12, 2024
1 parent a6b89e7 commit 922fbaf
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 81 deletions.
2 changes: 1 addition & 1 deletion dist/views/foo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="icon" type="image/svg+xml" href="/sketches/static/favicon.svg">
<link rel="stylesheet" href="/sketches/static/global.css">
<link rel="stylesheet" href="/sketches/static/default.css">
<title>Foo</title>
<title>Sketch Template</title>
<script type="module" crossorigin src="/sketches/views/foo/index-DPL9cTha.js"></script>
</head>
<body>
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

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

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 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;
"use strict";var w=Object.defineProperty;var n=(i,t)=>w(i,"name",{value:t,configurable:!0});var h=require("node:path"),f=require("node:fs"),l=require("@internal/utils");const g="libs.json",p={base:"https://cdnjs.cloudflare.com/ajax/libs",api:"https://api.cdnjs.com/libraries"},b={initial:["name","version","filename","sri"],version:["name","sri"]};function v(i){const t=Object.entries(i),e=new Set;return t.forEach(([s,a])=>{const{filename:o}=a,r=Object.keys(a.versions);for(const c of r){const d=`/${s}/${c}/${o}`;e.add(d)}}),e}n(v,"createLibraryKeys");async function u(i,t){const e=t?`${i}/${t}`:i,s=t?b.version:b.initial,a=`${p.api}/${e}?fields=${s.join(",")}`;console.log("Fetching data from %o ...",a);const o=await j(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(u,"fetchLibraryData");async function j(i){try{return await(await fetch(i)).json()}catch(t){l.logError(t)}return null}n(j,"fetchRequest");function y(i,t,e){if(e="filename"in i?i.filename:e,!e)throw new Error("Filename of %o is required!");return{url:`${p.base}/${i.name}/${t}/${e}`,sri:typeof i.sri=="string"?i.sri:i.sri[e]}}n(y,"parseFetchedData");class ${static{n(this,"LibHandler")}path;file;data;keys;constructor(t){this.path=t,this.file=h.join(t,g),this.data=l.parseJson(this.file)??{},this.keys=v(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 s=this.data[t],a=new Date(s.updatedAt).valueOf(),o=Date.now()-a>1e3*60*60*24*31;if(!e&&!o&&s.latest&&(console.log("Latest version %o of %o has already been fetched",s.latest,t),process.exit(0)),!e&&o){const r=await u(t,null);r.version===s.latest&&r.version in s.versions&&(console.log("Latest version %o of %o has already been fetched",s.latest,t),process.exit(0)),s.latest=r.version,s.versions[r.version]=y(r,r.version),s.updatedAt=new Date().toISOString(),this.data[t]=s;return}if(e){Object.keys(s.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,s.filename);return}console.log("Something went wrong ..."),process.exit(1)}async download(t,e,s){const a=h.join(this.path,t,e),o=h.resolve(a,s);f.existsSync(o)&&(console.log("Version %o of %o has already been downloaded",e,t),process.exit(0)),f.mkdirSync(a,{recursive:!0});try{const r=this.data[t].versions[e].url,d=await(await fetch(r)).text();f.writeFileSync(o,d,"utf-8")}catch(r){l.logError(r),process.exit(1)}}async fetch(t,e){Object.hasOwn(this.data,t)||await this.createLibraryData(t),e&&await this.updateLibraryData(t,e),l.writeJson(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,s]=t.slice(1).split("/"),a=e in this.data?this.data[e]:null;return a?a.versions[s]??null:null}}exports.LibHandler=$;
6 changes: 1 addition & 5 deletions packages/cdn/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ declare class LibHandler {
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 };
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=(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};
var b=Object.defineProperty;var n=(i,t)=>b(i,"name",{value:t,configurable:!0});import d from"node:path";import f from"node:fs";import{logError as y,parseJson as g,writeJson as j}from"@internal/utils";const v="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 x(i){const t=Object.entries(i),s=new Set;return t.forEach(([e,o])=>{const{filename:a}=o,r=Object.keys(o.versions);for(const c of r){const l=`/${e}/${c}/${a}`;s.add(l)}}),s}n(x,"createLibraryKeys");async function h(i,t){const s=t?`${i}/${t}`:i,e=t?w.version:w.initial,o=`${p.api}/${s}?fields=${e.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}n(h,"fetchLibraryData");async function $(i){try{return await(await fetch(i)).json()}catch(t){y(t)}return null}n($,"fetchRequest");function u(i,t,s){if(s="filename"in i?i.filename:s,!s)throw new Error("Filename of %o is required!");return{url:`${p.base}/${i.name}/${t}/${s}`,sri:typeof i.sri=="string"?i.sri:i.sri[s]}}n(u,"parseFetchedData");class D{static{n(this,"LibHandler")}path;file;data;keys;constructor(t){this.path=t,this.file=d.join(t,v),this.data=g(this.file)??{},this.keys=x(this.data)}async createLibraryData(t){const s=await h(t,null);this.data[t]={latest:s.version,filename:s.filename,versions:{[s.version]:u(s,s.version)},updatedAt:new Date().toISOString()}}async updateLibraryData(t,s){const e=this.data[t],o=new Date(e.updatedAt).valueOf(),a=Date.now()-o>1e3*60*60*24*31;if(!s&&!a&&e.latest&&(console.log("Latest version %o of %o has already been fetched",e.latest,t),process.exit(0)),!s&&a){const r=await h(t,null);r.version===e.latest&&r.version in e.versions&&(console.log("Latest version %o of %o has already been fetched",e.latest,t),process.exit(0)),e.latest=r.version,e.versions[r.version]=u(r,r.version),e.updatedAt=new Date().toISOString(),this.data[t]=e;return}if(s){Object.keys(e.versions).includes(s)&&(console.log("Version %o of %o has already been fetched",s,t),process.exit(0));const c=await h(t,s);this.data[t].versions[s]=u(c,s,e.filename);return}console.log("Something went wrong ..."),process.exit(1)}async download(t,s,e){const o=d.join(this.path,t,s),a=d.resolve(o,e);f.existsSync(a)&&(console.log("Version %o of %o has already been downloaded",s,t),process.exit(0)),f.mkdirSync(o,{recursive:!0});try{const r=this.data[t].versions[s].url,l=await(await fetch(r)).text();f.writeFileSync(a,l,"utf-8")}catch(r){y(r),process.exit(1)}}async fetch(t,s){Object.hasOwn(this.data,t)||await this.createLibraryData(t),s&&await this.updateLibraryData(t,s),j(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,e]=t.slice(1).split("/"),o=s in this.data?this.data[s]:null;return o?o.versions[e]??null:null}}export{D as LibHandler};
3 changes: 3 additions & 0 deletions packages/cdn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"@internal/cli": "^0.0.1",
"pkgroll": "^2.4.2",
"prettier": "^3.3.3"
},
"dependencies": {
"@internal/utils": "file:../utils"
}
}
4 changes: 2 additions & 2 deletions packages/cdn/src/class.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import np from 'node:path';
import nfs from 'node:fs';

import { filename } from './config.js';
import { parseJson, writeJson, logError } from '@internal/utils';

import { parseJson, writeJson, logError } from './utils.js';
import { filename } from './config.js';

import {
createLibraryKeys,
Expand Down
14 changes: 13 additions & 1 deletion packages/cdn/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logError } from '@internal/utils';

import { fields, url } from './config.js';
import { fetchRequest } from './utils.js';

import type {
Libraries,
Expand Down Expand Up @@ -53,6 +54,17 @@ export async function fetchLibraryData<T>(
return json as T;
}

export async function fetchRequest<T>(request: string) {
try {
const data = await fetch(request);
return (await data.json()) as T;
} catch (error) {
logError(error);
}

return null;
}

export function parseFetchedData(
data: InitialJson | VersionJson,
version: string,
Expand Down
2 changes: 0 additions & 2 deletions packages/cdn/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export { LibHandler } from './class.js';

export { logError, parseJson, writeJson } from './utils.js';

export {
type Libraries,
type LibraryDataItem as LibraryData,
Expand Down
33 changes: 0 additions & 33 deletions packages/cdn/src/utils.ts

This file was deleted.

Loading

0 comments on commit 922fbaf

Please sign in to comment.