Skip to content

Commit

Permalink
ci: Improving schema build script
Browse files Browse the repository at this point in the history
  • Loading branch information
steilerDev committed Sep 1, 2023
1 parent f47ec90 commit 0200173
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 32 deletions.
27 changes: 0 additions & 27 deletions app/build/schema.sh

This file was deleted.

79 changes: 79 additions & 0 deletions app/build/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as tsj from "ts-json-schema-generator";
import path from "path";
import fs from "fs/promises";

const tsConfigPath = "tsconfig.json";
const targetDir = "src/lib/resources/schemas/";
const schemaList = [
{
"typeName": "ResourceFile",
"srcPath": "src/lib/resources/resource-types.ts",
"allowAdditionalProperties": false
},{
"typeName": "SigninResponse",
"srcPath": "src/lib/resources/network-types.ts",
"allowAdditionalProperties": true
}, {
"typeName": "ResendMFADeviceResponse",
"srcPath": "src/lib/resources/network-types.ts",
"allowAdditionalProperties": true
}, {
"typeName": "ResendMFAPhoneResponse",
"srcPath": "src/lib/resources/network-types.ts",
"allowAdditionalProperties": true
}, {
"typeName": "TrustResponse",
"srcPath": "src/lib/resources/network-types.ts",
"allowAdditionalProperties": true
}, {
"typeName": "SetupResponse",
"srcPath": "src/lib/resources/network-types.ts",
"allowAdditionalProperties": true
}, {
"typeName": "PhotosSetupResponse",
"srcPath": "src/lib/resources/network-types.ts",
"allowAdditionalProperties": true
},
]

await fs.mkdir(targetDir, {recursive: true});

await Promise.all(schemaList.map(async (schemaConfig) => {
const outputFilename = schemaConfig.typeName
.replace(/(?<!^)[A-Z][a-z]/g, (str) => `-${str}`) // Match big to small transitions, except at the beginning of the string and prefix them with a dash
.replace(/[a-z][A-Z]/g, (str) => `${str.charAt(0)}-${str.charAt(1)}`) // Match small to big transitions and insert a dash between them
.toLowerCase() + ".json"; // Make everything lower case and append file extension

const outputPath = path.join(targetDir, outputFilename);

const srcStat = await fs.stat(schemaConfig.srcPath)

try {
const outputStat = await fs.stat(outputPath)
if (srcStat.mtimeMs < outputStat.mtimeMs) {
console.log(`Skipping ${schemaConfig.srcPath} as it is older than ${outputPath}`);
return;
}
} catch (err) {
console.log(`No existing schema for ${schemaConfig.typeName} found at ${outputPath}`);
}

const config: tsj.Config = {
path: schemaConfig.srcPath,
tsconfig: tsConfigPath,
type: schemaConfig.typeName,
additionalProperties: schemaConfig.allowAdditionalProperties,
jsDoc: "extended",
discriminatorType: "json-schema",
skipTypeCheck: true,
};

const schema = tsj.createGenerator(config).createSchema(config.type);

await fs.writeFile(outputPath,
JSON.stringify(schema, null, 2),
{encoding: "utf-8"}
);

console.log(`Wrote schema for type ${schemaConfig.typeName} to ${outputPath}`);
}));
12 changes: 7 additions & 5 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"README.md"
],
"scripts": {
"clean": "rm -rf LICENSE README.md build/out/ bin/ coverage/ doc/ node_modules/ package-lock.json src/lib/resource-manager/schemas/",
"clean": "rm -rf LICENSE README.md build/out/ bin/ coverage/ doc/ node_modules/ package-lock.json src/lib/resources/schemas/",
"prebuild:lint": "npx eslint -c eslint.config.json --fix --ext .ts ./src ./test",
"prebuild:knip": "npx knip --no-exit-code --config knip.config.jsonc",
"prebuild": "npm run prebuild:lint && npm run prebuild:knip",
"build:schema": "./build/schema.sh",
"build:schema": "npx ts-node-esm build/schema.ts",
"build:typescript": "npx tsc",
"build:typedoc": "npx typedoc --plugin typedoc-plugin-markdown --plugin typedoc-github-wiki-theme",
"build": "npm run build:schema && npm run build:typescript",
Expand All @@ -29,8 +29,8 @@
"test": "NODE_NO_WARNINGS=1 NODE_OPTIONS='--experimental-vm-modules' npx jest --config jest.config.json",
"test:unit": "npm run test test/unit/",
"test:api": "npm run test test/api/",
"execute": "node bin/main.js",
"execute:debug": "node --inspect=0.0.0.0:9229 bin/main.js daemon"
"execute": "NODE_NO_WARNINGS=1 node build/out/src/main.js",
"execute:debug": "NODE_NO_WARNINGS=1 node --inspect=0.0.0.0:9229 build/out/src/main.js daemon"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -71,6 +71,7 @@
"eslint-plugin-tsdoc": "^0.2.17",
"jest": "^29.5.0",
"knip": "^2.16.0",
"liquidjs": "^10.9.2",
"mock-fs": "^5.2.0",
"ts-jest": "^29.1.1",
"ts-json-schema-generator": "^1.2.0",
Expand All @@ -90,6 +91,7 @@
"croner": "^6.0.3",
"p-event": "^5.0.1",
"p-queue": "^7.2.0",
"tough-cookie": "^4.0.0"
"tough-cookie": "^4.0.0",
"ts-node": "^10.9.1"
}
}

0 comments on commit 0200173

Please sign in to comment.