mirror of
https://github.com/discordeno/discordeno.git
synced 2026-07-21 21:52:52 +00:00
formatter: Use semicolons (#4686)
I prefer semicolors, they also help avoiding certain pitfalls in JavaScript/TypeScript, such as the following code sample: ```js const xyz = "test" (something.else as string) = "another" ``` This results in a TypeError: "test" is not a function, this is because js thinks we are trying to call the string "test" as a function. To fix this it requires a `;` somewhere before the `(`, such as `;(something ... ` which in my opinion is ugly and less clean overall.
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
import { execSync } from 'node:child_process'
|
||||
import { readFile, writeFile } from 'node:fs/promises'
|
||||
import { execSync } from 'node:child_process';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
|
||||
const packageName = process.argv[2]
|
||||
const packageName = process.argv[2];
|
||||
|
||||
if (!packageName) {
|
||||
throw new Error('No package name specified')
|
||||
throw new Error('No package name specified');
|
||||
}
|
||||
|
||||
const commitHash = execSync('git rev-parse HEAD').toString().slice(0, 7)
|
||||
const commitHash = execSync('git rev-parse HEAD').toString().slice(0, 7);
|
||||
|
||||
const file = JSON.parse(await readFile(`packages/${packageName}/package.json`, 'utf-8'))
|
||||
const file = JSON.parse(await readFile(`packages/${packageName}/package.json`, 'utf-8'));
|
||||
|
||||
const version = file.version.split('-')[0]
|
||||
const version = file.version.split('-')[0];
|
||||
|
||||
file.version = `${bumpPatch(version)}-next.${commitHash}`
|
||||
file.version = `${bumpPatch(version)}-next.${commitHash}`;
|
||||
|
||||
if (file.dependencies) {
|
||||
Object.keys(file.dependencies).forEach((dependency) => {
|
||||
if (dependency.startsWith('@discordeno/')) file.dependencies[dependency] = file.version
|
||||
})
|
||||
if (dependency.startsWith('@discordeno/')) file.dependencies[dependency] = file.version;
|
||||
});
|
||||
}
|
||||
|
||||
await writeFile(`packages/${packageName}/package.json`, JSON.stringify(file, null, 2))
|
||||
await writeFile(`packages/${packageName}/package.json`, JSON.stringify(file, null, 2));
|
||||
|
||||
console.log(`Bumped ${packageName} to ${file.version}`)
|
||||
console.log(`Bumped ${packageName} to ${file.version}`);
|
||||
|
||||
function bumpPatch(version) {
|
||||
const parts = version.split('.').map(Number)
|
||||
const parts = version.split('.').map(Number);
|
||||
if (parts.length !== 3 || parts.some(isNaN)) {
|
||||
throw new Error('Invalid semver format. Expected format "MAJOR.MINOR.PATCH"')
|
||||
throw new Error('Invalid semver format. Expected format "MAJOR.MINOR.PATCH"');
|
||||
}
|
||||
|
||||
parts[2] += 1
|
||||
return parts.join('.')
|
||||
parts[2] += 1;
|
||||
return parts.join('.');
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
|
||||
const hostCpu = os.cpus()[0].model.trim()
|
||||
const hostCpu = os.cpus()[0].model.trim();
|
||||
const targetCpu = await fetch('https://raw.githubusercontent.com/discordeno/discordeno/benchies/cpu')
|
||||
.then(async (res) => await res.text())
|
||||
.then((text) => text.slice(0, -1))
|
||||
console.dir(`host cpu: ${hostCpu} target cpu: ${targetCpu}`)
|
||||
.then((text) => text.slice(0, -1));
|
||||
console.dir(`host cpu: ${hostCpu} target cpu: ${targetCpu}`);
|
||||
|
||||
let outputFile = fs.readFileSync(process.env.GITHUB_OUTPUT, 'utf8')
|
||||
outputFile += `\nmatch=${hostCpu === targetCpu}`
|
||||
fs.writeFileSync(process.env.GITHUB_OUTPUT, outputFile, 'utf8')
|
||||
let outputFile = fs.readFileSync(process.env.GITHUB_OUTPUT, 'utf8');
|
||||
outputFile += `\nmatch=${hostCpu === targetCpu}`;
|
||||
fs.writeFileSync(process.env.GITHUB_OUTPUT, outputFile, 'utf8');
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
import { readFileSync, writeFileSync } from 'node:fs'
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
|
||||
writeFileSync('./coverage/lcov.info', readFileSync('./coverage/lcov.info', 'utf-8').replace(/SF:src/g, `SF:packages/${process.argv[2]}/src`))
|
||||
writeFileSync('./coverage/lcov.info', readFileSync('./coverage/lcov.info', 'utf-8').replace(/SF:src/g, `SF:packages/${process.argv[2]}/src`));
|
||||
|
||||
+27
-27
@@ -1,56 +1,56 @@
|
||||
import fs from 'node:fs/promises'
|
||||
import fs from 'node:fs/promises';
|
||||
|
||||
const benchmarkData = await fetch(`https://raw.githubusercontent.com/discordeno/discordeno/benchies/benchmarksResult/data.js`)
|
||||
.then(async (res) => await res.text())
|
||||
.then((text) => JSON.parse(text.slice(24)))
|
||||
.then((text) => JSON.parse(text.slice(24)));
|
||||
// const commitSha = await fs.readFile('./sha', 'utf-8')
|
||||
const results = JSON.parse(await fs.readFile('./data.json', 'utf-8'))
|
||||
const benchmarks = results.entries.Benchmark
|
||||
benchmarks.reverse()
|
||||
const compareWithHead = {}
|
||||
const latestBaseBenchmarks = benchmarkData.entries.Benchmark.slice(-1)[0]
|
||||
const results = JSON.parse(await fs.readFile('./data.json', 'utf-8'));
|
||||
const benchmarks = results.entries.Benchmark;
|
||||
benchmarks.reverse();
|
||||
const compareWithHead = {};
|
||||
const latestBaseBenchmarks = benchmarkData.entries.Benchmark.slice(-1)[0];
|
||||
for (const benchmark of latestBaseBenchmarks.benches) {
|
||||
compareWithHead[benchmark.name] = {
|
||||
[latestBaseBenchmarks.commit.id]: benchmark,
|
||||
}
|
||||
};
|
||||
}
|
||||
for (let i = benchmarks.length - 1; i >= 0; i--) {
|
||||
for (const bench of benchmarks[i].benches) {
|
||||
if (compareWithHead[bench.name]) {
|
||||
compareWithHead[bench.name][benchmarks[i].commit.id] = bench
|
||||
compareWithHead[bench.name][benchmarks[i].commit.id] = bench;
|
||||
} else {
|
||||
compareWithHead[bench.name] = {
|
||||
[benchmarks[i].commit.id]: bench,
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
let message = '<!-- benchmark comment by ci -->\n'
|
||||
message += `## Benchmark\n\n`
|
||||
message += '<details><summary>Detail results of benchmarks</summary>\n\n'
|
||||
let header1 = `| Benchmark suite | Base (${latestBaseBenchmarks.commit.id}) |`
|
||||
let header2 = `|-|-|`
|
||||
const commitIds = benchmarks.map((benchmark) => benchmark.commit.id)
|
||||
const uniqueCommitIds = commitIds.filter((benchmarkCommitId, index) => commitIds.indexOf(benchmarkCommitId) === index)
|
||||
let message = '<!-- benchmark comment by ci -->\n';
|
||||
message += `## Benchmark\n\n`;
|
||||
message += '<details><summary>Detail results of benchmarks</summary>\n\n';
|
||||
let header1 = `| Benchmark suite | Base (${latestBaseBenchmarks.commit.id}) |`;
|
||||
let header2 = `|-|-|`;
|
||||
const commitIds = benchmarks.map((benchmark) => benchmark.commit.id);
|
||||
const uniqueCommitIds = commitIds.filter((benchmarkCommitId, index) => commitIds.indexOf(benchmarkCommitId) === index);
|
||||
for (const [index, commitId] of uniqueCommitIds.entries()) {
|
||||
header1 += index === 0 ? ` Latest Head (${commitId}) |` : ` ${commitId} |`
|
||||
header2 += '-|'
|
||||
header1 += index === 0 ? ` Latest Head (${commitId}) |` : ` ${commitId} |`;
|
||||
header2 += '-|';
|
||||
}
|
||||
message += `${header1}\n`
|
||||
message += `${header2}\n`
|
||||
message += `${header1}\n`;
|
||||
message += `${header2}\n`;
|
||||
for (const benchName of Object.keys(compareWithHead)) {
|
||||
let benchData = `| ${benchName} |`
|
||||
let benchData = `| ${benchName} |`;
|
||||
benchData += compareWithHead[benchName][latestBaseBenchmarks.commit.id]
|
||||
? ` ${`\`${compareWithHead[benchName][latestBaseBenchmarks.commit.id].value}\` ${
|
||||
compareWithHead[benchName][latestBaseBenchmarks.commit.id].unit
|
||||
} \`${compareWithHead[benchName][latestBaseBenchmarks.commit.id].range}\``} |`
|
||||
: '|'
|
||||
: '|';
|
||||
for (const commitId of uniqueCommitIds) {
|
||||
benchData += compareWithHead[benchName][commitId]
|
||||
? ` \`${compareWithHead[benchName][commitId].value}\` ${compareWithHead[benchName][commitId].unit} \`${compareWithHead[benchName][commitId].range}\`|`
|
||||
: '|'
|
||||
: '|';
|
||||
}
|
||||
message += `${benchData}\n`
|
||||
message += `${benchData}\n`;
|
||||
}
|
||||
message += '</details>\n\n'
|
||||
console.log(message.replaceAll('`', '\\`'))
|
||||
message += '</details>\n\n';
|
||||
console.log(message.replaceAll('`', '\\`'));
|
||||
|
||||
+36
-36
@@ -1,58 +1,58 @@
|
||||
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||
|
||||
import fs from 'fs'
|
||||
import fs from 'fs';
|
||||
|
||||
// TODO: replace with fetch of live spec
|
||||
const DISCORD_SPEC = {}
|
||||
const DISCORD_SPEC = {};
|
||||
|
||||
function schemaRefToName(ref) {
|
||||
if (!ref) return ''
|
||||
return `Discord${ref?.substring(ref.lastIndexOf('/') + 1)}`
|
||||
if (!ref) return '';
|
||||
return `Discord${ref?.substring(ref.lastIndexOf('/') + 1)}`;
|
||||
}
|
||||
|
||||
function snakeToPascalCase(str) {
|
||||
if (!str.includes('_')) return str
|
||||
if (!str.includes('_')) return str;
|
||||
|
||||
let result = ''
|
||||
let result = '';
|
||||
for (let i = 0, len = str.length; i < len; ++i) {
|
||||
if (str[i] === '_') {
|
||||
result += str[++i].toUpperCase()
|
||||
result += str[++i].toUpperCase();
|
||||
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
result += str[i]
|
||||
result += str[i];
|
||||
}
|
||||
|
||||
return result
|
||||
return result;
|
||||
}
|
||||
|
||||
function generateFromSpec() {
|
||||
const finalTypings = []
|
||||
const finalTypings = [];
|
||||
|
||||
for (const [key, schema] of Object.entries(DISCORD_SPEC.components.schemas)) {
|
||||
const interfacey = [`export interface Discord${key} {`]
|
||||
const interfacey = [`export interface Discord${key} {`];
|
||||
|
||||
if (schema.properties) {
|
||||
for (const [name, property] of Object.entries(schema.properties)) {
|
||||
const type = Array.isArray(property.type) ? property.type : property.type ? [property.type] : undefined
|
||||
let ref = property.$ref && schemaRefToName(property.$ref)
|
||||
let allOf = property.allOf?.find((a) => a.$ref)
|
||||
const type = Array.isArray(property.type) ? property.type : property.type ? [property.type] : undefined;
|
||||
let ref = property.$ref && schemaRefToName(property.$ref);
|
||||
let allOf = property.allOf?.find((a) => a.$ref);
|
||||
if (allOf) {
|
||||
allOf = schemaRefToName(allOf.$ref)
|
||||
allOf = schemaRefToName(allOf.$ref);
|
||||
}
|
||||
|
||||
const isArray = property.type === 'array'
|
||||
const isArray = property.type === 'array';
|
||||
if (isArray) {
|
||||
if (property.items.$ref) ref = schemaRefToName(property.items.$ref)
|
||||
if (property.items.$ref) ref = schemaRefToName(property.items.$ref);
|
||||
else if (Array.isArray(property.oneOf)) {
|
||||
ref = property.oneOf.map((o) => schemaRefToName(o.$ref)).join(' | ')
|
||||
ref = property.oneOf.map((o) => schemaRefToName(o.$ref)).join(' | ');
|
||||
}
|
||||
}
|
||||
|
||||
if (property.description) {
|
||||
interfacey.push(` /** ${property.description} */`)
|
||||
interfacey.push(` /** ${property.description} */`);
|
||||
}
|
||||
|
||||
let cleanType =
|
||||
@@ -83,17 +83,17 @@ function generateFromSpec() {
|
||||
: undefined) ?? schemaRefToName(o.$ref),
|
||||
)
|
||||
.join(' | ') ??
|
||||
'unknown'
|
||||
'unknown';
|
||||
|
||||
if (cleanType.startsWith('null | ')) cleanType = cleanType.substring(cleanType.indexOf('|') + 2) + ' | null'
|
||||
interfacey.push(` ${name}${schema.required?.includes(name) ? ':' : '?:'} ${cleanType}`)
|
||||
if (cleanType.startsWith('null | ')) cleanType = cleanType.substring(cleanType.indexOf('|') + 2) + ' | null';
|
||||
interfacey.push(` ${name}${schema.required?.includes(name) ? ':' : '?:'} ${cleanType}`);
|
||||
}
|
||||
} else {
|
||||
// No properties so is enum
|
||||
if (['integer', 'string'].includes(schema.type) && Array.isArray(schema.oneOf)) {
|
||||
const enumm = [`export enum Discord${key} {`]
|
||||
const enumm = [`export enum Discord${key} {`];
|
||||
for (const possible of schema.oneOf) {
|
||||
if (possible.description) enumm.push(` /** ${possible.description} */`)
|
||||
if (possible.description) enumm.push(` /** ${possible.description} */`);
|
||||
// TODO: camel case the title
|
||||
enumm.push(
|
||||
` ${possible.title.includes('-') ? '"' : ''}${snakeToPascalCase(
|
||||
@@ -101,26 +101,26 @@ function generateFromSpec() {
|
||||
)}${possible.title.includes('-') ? '"' : ''} = ${schema.type === 'string' ? '"' : ''}${possible.const}${
|
||||
schema.type === 'string' ? '"' : ''
|
||||
},`,
|
||||
)
|
||||
);
|
||||
}
|
||||
enumm.push('}')
|
||||
enumm.push('}');
|
||||
|
||||
finalTypings.push(enumm.join('\n'))
|
||||
continue
|
||||
finalTypings.push(enumm.join('\n'));
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log('NO PROPERTIES', key, schema)
|
||||
continue
|
||||
console.log('NO PROPERTIES', key, schema);
|
||||
continue;
|
||||
}
|
||||
|
||||
interfacey.push('}')
|
||||
interfacey.push('}');
|
||||
|
||||
finalTypings.push(interfacey.join('\n'))
|
||||
finalTypings.push(interfacey.join('\n'));
|
||||
}
|
||||
|
||||
fs.writeFileSync('./packages/types/src/discord.ts', finalTypings.join('\n\n'), function (err, _result) {
|
||||
if (err) throw err
|
||||
})
|
||||
if (err) throw err;
|
||||
});
|
||||
}
|
||||
|
||||
generateFromSpec()
|
||||
generateFromSpec();
|
||||
|
||||
Reference in New Issue
Block a user