More async, remove node-fetch

pull/8873/head
Puru Vijay 3 years ago
parent 490ef83adc
commit a894dd939c

@ -217,9 +217,6 @@ importers:
marked:
specifier: ^5.1.0
version: 5.1.0
node-fetch:
specifier: ^3.3.1
version: 3.3.1
prettier:
specifier: ^2.8.8
version: 2.8.8
@ -3000,11 +2997,6 @@ packages:
type: 1.2.0
dev: false
/data-uri-to-buffer@4.0.1:
resolution: {integrity: sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==}
engines: {node: '>= 12'}
dev: true
/data-urls@4.0.0:
resolution: {integrity: sha512-/mMTei/JXPqvFqQtfyTowxmJVwr2PVAeCcDxyFf6LhoOu/09TX2OX3kb2wzi4DMXcfj4OItwDOnhl5oziPnT6g==}
engines: {node: '>=14'}
@ -3737,14 +3729,6 @@ packages:
engines: {node: '>=12'}
dev: true
/fetch-blob@3.2.0:
resolution: {integrity: sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==}
engines: {node: ^12.20 || >= 14.13}
dependencies:
node-domexception: 1.0.0
web-streams-polyfill: 3.2.1
dev: true
/fflate@0.7.4:
resolution: {integrity: sha512-5u2V/CDW15QM1XbbgS+0DfPxVB+jUKhWEKuuFuHncbk3tEEqzmoXL+2KyOFuKGqOnmdIy0/davWF1CkuwtibCw==}
dev: true
@ -3834,13 +3818,6 @@ packages:
mime-types: 2.1.35
dev: true
/formdata-polyfill@4.0.10:
resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==}
engines: {node: '>=12.20.0'}
dependencies:
fetch-blob: 3.2.0
dev: true
/fs-constants@1.0.0:
resolution: {integrity: sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==}
dev: true
@ -4939,11 +4916,6 @@ packages:
resolution: {integrity: sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==}
dev: true
/node-domexception@1.0.0:
resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==}
engines: {node: '>=10.5.0'}
dev: true
/node-fetch@2.6.11:
resolution: {integrity: sha512-4I6pdBY1EthSqDmJkiNk3JIT8cswwR9nfeW/cPdUagJYEQG7R95WRH74wpz7ma8Gh/9dI9FP+OU+0E4FvtA55w==}
engines: {node: 4.x || >=6.0.0}
@ -4955,15 +4927,6 @@ packages:
dependencies:
whatwg-url: 5.0.0
/node-fetch@3.3.1:
resolution: {integrity: sha512-cRVc/kyto/7E5shrWca1Wsea4y6tL9iYJE5FBCius3JQfb/4P4I295PfhgbJQBLTx6lATE4z+wK0rPM4VS2uow==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
dependencies:
data-uri-to-buffer: 4.0.1
fetch-blob: 3.2.0
formdata-polyfill: 4.0.10
dev: true
/node-gyp-build@4.6.0:
resolution: {integrity: sha512-NTZVKn9IylLwUzaKjkas1e4u2DLNcV4rdYagA4PWdPwW87Bi7z+BznyKSRwS/761tV/lzCGXplWsiaMjLqP2zQ==}
hasBin: true
@ -6766,11 +6729,6 @@ packages:
defaults: 1.0.4
dev: true
/web-streams-polyfill@3.2.1:
resolution: {integrity: sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==}
engines: {node: '>= 8'}
dev: true
/webidl-conversions@3.0.1:
resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==}

@ -41,7 +41,6 @@
"jimp": "^0.22.8",
"magic-string": "^0.30.1",
"marked": "^5.1.0",
"node-fetch": "^3.3.1",
"prettier": "^2.8.8",
"prettier-plugin-svelte": "^2.10.1",
"sass": "^1.63.6",

@ -1,16 +1,16 @@
import { fileURLToPath } from 'node:url';
import { get_examples_data } from '../src/lib/server/examples/index.js';
import fs from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
const examples_data = await get_examples_data(
fileURLToPath(new URL('../../../documentation/examples', import.meta.url))
);
try {
fs.mkdirSync(new URL('../src/lib/generated/', import.meta.url), { recursive: true });
await mkdir(new URL('../src/lib/generated/', import.meta.url), { recursive: true });
} catch {}
fs.writeFileSync(
writeFile(
new URL('../src/lib/generated/examples-data.js', import.meta.url),
`export default ${JSON.stringify(examples_data)}`
);

@ -1,9 +1,9 @@
// @ts-check
import 'dotenv/config';
import fs from 'fs';
import fetch from 'node-fetch';
import Jimp from 'jimp';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { stat, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const force = process.env.FORCE_UPDATE === 'true';
@ -11,7 +11,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
process.chdir(__dirname);
const outputFile = `../src/routes/_components/Supporters/contributors.js`;
if (!force && fs.existsSync(outputFile)) {
if (!force && (await stat(outputFile))) {
console.info(`[update/contributors] ${outputFile} exists. Skipping`);
process.exit(0);
}
@ -22,50 +22,49 @@ const { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } = process.env;
const MAX = 24;
const SIZE = 128;
async function main() {
const contributors = [];
let page = 1;
const contributors = [];
let page = 1;
while (true) {
while (true) {
const res = await fetch(
`${base}?client_id=${GITHUB_CLIENT_ID}&client_secret=${GITHUB_CLIENT_SECRET}&per_page=100&page=${page++}`
);
const list = await res.json();
if (!Array.isArray(list)) throw new Error('Expected an array');
if (list.length === 0) break;
contributors.push(...list);
}
}
const authors = contributors
const authors = contributors
.filter(({ login }) => !login.includes('[bot]'))
.sort((a, b) => b.contributions - a.contributions)
.slice(0, MAX);
const sprite = new Jimp(SIZE * authors.length, SIZE);
const sprite = new Jimp(SIZE * authors.length, SIZE);
for (let i = 0; i < authors.length; i += 1) {
for (let i = 0; i < authors.length; i += 1) {
const author = authors[i];
console.log(`${i + 1} / ${authors.length}: ${author.login}`);
const image_data = await fetch(author.avatar_url);
const buffer = await image_data.arrayBuffer();
// @ts-ignore
const image = await Jimp.read(buffer);
image.resize(SIZE, SIZE);
sprite.composite(image, i * SIZE, 0);
}
}
await sprite.quality(80).write(`../src/routes/_components/Supporters/contributors.jpg`);
// TODO: Optimizing the static/contributors.jpg image should probably get automated as well
console.log(
await sprite.quality(80).write(`../src/routes/_components/Supporters/contributors.jpg`);
// TODO: Optimizing the static/contributors.jpg image should probably get automated as well
console.log(
'remember to additionally optimize the resulting /static/contributors.jpg image file via e.g. https://squoosh.app '
);
const str = `[\n\t${authors.map((a) => `'${a.login}'`).join(',\n\t')}\n]`;
);
fs.writeFileSync(outputFile, `export default ${str};`);
}
const str = `[\n\t${authors.map((a) => `'${a.login}'`).join(',\n\t')}\n]`;
main();
writeFile(outputFile, `export default ${str};`);

@ -1,9 +1,9 @@
// @ts-check
import 'dotenv/config';
import fs from 'fs';
import fetch from 'node-fetch';
import Jimp from 'jimp';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { stat, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const force = process.env.FORCE_UPDATE === 'true';
@ -11,7 +11,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
process.chdir(__dirname);
const outputFile = `../src/routes/_components/Supporters/donors.js`;
if (!force && fs.existsSync(outputFile)) {
if (!force && (await stat(outputFile))) {
console.info(`[update/donors] ${outputFile} exists. Skipping`);
process.exit(0);
}
@ -23,6 +23,8 @@ async function main() {
const res = await fetch('https://opencollective.com/svelte/members/all.json');
const donors = await res.json();
if (!Array.isArray(donors)) throw new Error('Expected an array');
const unique = new Map();
donors.forEach((d) => unique.set(d.profile, d));
@ -39,6 +41,7 @@ async function main() {
try {
const image_data = await fetch(backer.image);
const buffer = await image_data.arrayBuffer();
// @ts-ignore
const image = await Jimp.read(buffer);
image.resize(SIZE, SIZE);
included.push({ backer, image });
@ -60,7 +63,7 @@ async function main() {
const str = `[\n\t${included.map((a) => `${JSON.stringify(a.backer.name)}`).join(',\n\t')}\n]`;
fs.writeFileSync(outputFile, `export default ${str};`);
writeFile(outputFile, `export default ${str};`);
}
main();

@ -1,5 +1,5 @@
// @ts-check
import fs from 'node:fs';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import prettier from 'prettier';
import ts from 'typescript';
@ -236,12 +236,12 @@ function strip_origin(str) {
/**
* @param {string} file
*/
function read_d_ts_file(file) {
async function read_d_ts_file(file) {
const resolved = path.resolve('../../packages/svelte', file);
// We can't use JSDoc comments inside JSDoc, so we would get ts(7031) errors if
// we didn't ignore this error specifically for `/// file:` code examples
const str = fs.readFileSync(resolved, 'utf-8');
const str = await readFile(resolved, 'utf-8');
return str.replace(/(\s*\*\s*)```js([\s\S]+?)```/g, (match, prefix, code) => {
return `${prefix}\`\`\`js${prefix}// @errors: 7031${code}\`\`\``;
@ -249,7 +249,7 @@ function read_d_ts_file(file) {
}
{
const code = read_d_ts_file('types/index.d.ts');
const code = await read_d_ts_file('types/index.d.ts');
const node = ts.createSourceFile('index.d.ts', code, ts.ScriptTarget.Latest, true);
for (const statement of node.statements) {
@ -296,10 +296,10 @@ $: {
}
try {
fs.mkdirSync(new URL('../../src/lib/generated', import.meta.url), { recursive: true });
await mkdir(new URL('../../src/lib/generated', import.meta.url), { recursive: true });
} catch {}
fs.writeFileSync(
writeFile(
new URL('../../src/lib/generated/type-info.js', import.meta.url),
`
/* This file is generated by running \`pnpm generate\`

@ -1,36 +1,45 @@
// @ts-check
import { lstat, readFile, stat, writeFile } from 'node:fs/promises';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import sh from 'shelljs';
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
const force = process.env.FORCE_UPDATE === 'true';
try {
const force = process.env.FORCE_UPDATE === 'true';
const __dirname = dirname(fileURLToPath(import.meta.url));
sh.cd(path.join(__dirname, '..'));
const __dirname = dirname(fileURLToPath(import.meta.url));
sh.cd(path.join(__dirname, '..'));
const outputFile = 'static/svelte-app.json';
if (!force && fs.existsSync(outputFile)) {
const outputFile = 'static/svelte-app.json';
if (!force && (await stat(outputFile))) {
console.info(`[update/template] ${outputFile} exists. Skipping`);
process.exit(0);
}
}
// fetch svelte app
sh.rm('-rf', 'scripts/svelte-app');
sh.exec('npx degit sveltejs/template scripts/svelte-app');
// fetch svelte app
sh.rm('-rf', 'scripts/svelte-app');
sh.exec('npx degit sveltejs/template scripts/svelte-app');
// remove src (will be recreated client-side) and node_modules
sh.rm('-rf', 'scripts/svelte-app/src');
sh.rm('-rf', 'scripts/svelte-app/node_modules');
// remove src (will be recreated client-side) and node_modules
sh.rm('-rf', 'scripts/svelte-app/src');
sh.rm('-rf', 'scripts/svelte-app/node_modules');
// build svelte-app.json
const appPath = 'scripts/svelte-app';
const files = [];
// build svelte-app.json
const appPath = 'scripts/svelte-app';
const files = [];
for (const path of sh.find(appPath)) {
// Skip directories
if (!(await lstat(path)).isFile()) continue;
for (const path of sh.find(appPath).filter((p) => fs.lstatSync(p).isFile())) {
const bytes = fs.readFileSync(path);
const bytes = await readFile(path);
const string = bytes.toString();
const data = bytes.compare(Buffer.from(string)) === 0 ? string : [...bytes];
files.push({ path: path.slice(appPath.length + 1), data });
}
}
fs.writeFileSync(outputFile, JSON.stringify(files));
writeFile(outputFile, JSON.stringify(files));
} catch (e) {
console.error(e);
process.exit(1);
}

@ -3,8 +3,8 @@ import { json } from '@sveltejs/kit';
export const prerender = true;
export function GET() {
export async function GET() {
return json({
blocks: content()
blocks: await content()
});
}

@ -6,8 +6,8 @@ import {
removeMarkdown,
replaceExportTypePlaceholders
} from '@sveltejs/site-kit/markdown';
import fs from 'node:fs';
import glob from 'tiny-glob/sync.js';
import { readFile } from 'node:fs/promises';
import glob from 'tiny-glob';
import { CONTENT_BASE } from '../../constants.js';
const base = CONTENT_BASE;
@ -22,13 +22,13 @@ function path_basename(path) {
return path.split(/[\\/]/).pop();
}
export function content() {
export async function content() {
/** @type {import('@sveltejs/site-kit/search').Block[]} */
const blocks = [];
const breadcrumbs = [];
for (const file of glob('**/*.md', { cwd: `${base}/docs` })) {
for (const file of await glob('**/*.md', { cwd: `${base}/docs` })) {
const basename = path_basename(file);
const match = /\d{2}-(.+)\.md/.exec(basename);
if (!match) continue;
@ -36,8 +36,7 @@ export function content() {
const slug = match[1];
const filepath = `${base}/docs/${file}`;
// const markdown = replace_placeholders(fs.readFileSync(filepath, 'utf-8'));
const markdown = replaceExportTypePlaceholders(fs.readFileSync(filepath, 'utf-8'), modules);
const markdown = replaceExportTypePlaceholders(await readFile(filepath, 'utf-8'), modules);
const { body, metadata } = extractFrontmatter(markdown);

@ -1,5 +1,5 @@
import { sveltekit } from '@sveltejs/kit/vite';
import * as fs from 'fs';
import { readFile } from 'node:fs/promises';
const plugins = [raw(['.ttf']), sveltekit()];
@ -25,9 +25,9 @@ if (!process.versions.webcontainer) {
function raw(ext) {
return {
name: 'vite-plugin-raw',
transform(_, id) {
async transform(_, id) {
if (ext.some((e) => id.endsWith(e))) {
const buffer = fs.readFileSync(id);
const buffer = await readFile(id);
return { code: `export default ${JSON.stringify(buffer)}`, map: null };
}
}

Loading…
Cancel
Save