Skip to content

Commit

Permalink
WIP: got most resources creating. Just a few more kinks to work out
Browse files Browse the repository at this point in the history
  • Loading branch information
davidthor committed Dec 14, 2023
1 parent 41ecac5 commit 8f95a5c
Show file tree
Hide file tree
Showing 91 changed files with 6,227 additions and 6,280 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"prettier.enable": false,
"deno.documentPreloadLimit": 0,
Expand Down
3 changes: 2 additions & 1 deletion bin/component-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ for (const version of all_versions) {
args: [
'run',
'--allow-read',
'npm:ts-json-schema-generator',
'npm:ts-json-schema-generator@1.5.0',
'--path',
path.join(build_dir, 'src/components', version, 'index.ts'),
'--expose',
Expand All @@ -68,6 +68,7 @@ for (const version of all_versions) {
stdout: 'piped',
});
const type_schema = JSON.parse(type_schema_string);
type_schema.$schema = 'https://json-schema.org/draft/2019-09/schema';
await Deno.writeTextFile(path.join(components_dir, version, './schema.json'), JSON.stringify(type_schema, null, 2));
}

Expand Down
6 changes: 4 additions & 2 deletions bin/datacenter-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ for (const version of all_versions) {
stdout: 'piped',
});
const type_schema = JSON.parse(type_schema_string);
type_schema.$schema = 'https://json-schema.org/draft/2019-09/schema';
await Deno.writeTextFile(path.join(datacenters_dir, version, './schema.json'), JSON.stringify(type_schema, null, 2));
}

const { stdout: type_schema_string, stderr: err } = await exec('deno', {
args: [
'run',
'--allow-read',
'npm:ts-json-schema-generator',
'npm:ts-json-schema-generator@1.5.0',
'--path',
path.join(build_dir, 'src', 'datacenters', 'schema.ts'),
'--expose',
Expand All @@ -97,7 +98,7 @@ let type_schema = JSON.parse(type_schema_string);
if (type_schema.definitions.DatacenterSchema.anyOf) {
type_schema = {
oneOf: type_schema.definitions.DatacenterSchema.anyOf,
$schema: type_schema.$schema,
$schema: 'https://json-schema.org/draft/2019-09/schema',
$id: 'https://architect.io/.schemas/datacenter.json',
type: 'object',
required: ['version'],
Expand All @@ -107,6 +108,7 @@ if (type_schema.definitions.DatacenterSchema.anyOf) {
};
} else {
type_schema.$id = 'https://architect.io/.schemas/datacenter.json';
type_schema.$schema = 'https://json-schema.org/draft/2019-09/schema';
}

await Deno.writeTextFile(path.join(datacenters_dir, './datacenter.schema.json'), JSON.stringify(type_schema, null, 2));
Expand Down
5 changes: 3 additions & 2 deletions bin/environment-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ for (const version of all_versions) {
args: [
'run',
'--allow-read',
'npm:ts-json-schema-generator',
'npm:ts-json-schema-generator@1.5.0',
'--path',
path.join(build_dir, 'src/environments', version, 'index.ts'),
'--expose',
Expand All @@ -68,14 +68,15 @@ for (const version of all_versions) {
stdout: 'piped',
});
const type_schema = JSON.parse(type_schema_string);
type_schema.$schema = 'https://json-schema.org/draft/2019-09/schema';
await Deno.writeTextFile(path.join(environments_dir, version, './schema.json'), JSON.stringify(type_schema, null, 2));
}

const { stdout: type_schema_string, stderr: err } = await exec('deno', {
args: [
'run',
'--allow-read',
'npm:ts-json-schema-generator',
'npm:ts-json-schema-generator@1.5.0',
'--path',
path.join(build_dir, 'src/environments/schema.ts'),
'--expose',
Expand Down
126 changes: 126 additions & 0 deletions bin/module-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { build, emptyDir } from 'dnt';
import Mustache from 'npm:mustache';
import * as path from 'std/path/mod.ts';
import { exec } from '../src/utils/command.ts';

const __dirname = new URL('.', import.meta.url).pathname;
const modules_dir = path.join(__dirname, '../src/modules');
const build_dir = path.join(__dirname, 'build');

await emptyDir(build_dir);

const all_versions = [];
for await (const dirEntry of Deno.readDir(modules_dir)) {
if (dirEntry.isDirectory && dirEntry.name !== '__tests__') {
all_versions.push(dirEntry.name);
}
}
all_versions.sort((a, b) => a.localeCompare(b));

// Create the updated schema.ts file for all available schemas.
Deno.writeTextFile(
path.join(modules_dir, 'schema.ts'),
Mustache.render(await Deno.readTextFile(path.join(modules_dir, 'schema.ts.stache')), {
versions: all_versions,
}),
);

// Builds the schema into an npm package. This will convert files to .js and .d.ts with
// Deno shims so that "ts-json-schema-generator" can be run on it and infer types properly.
await build({
typeCheck: false,
test: false,
scriptModule: false,
entryPoints: [path.join(modules_dir, 'schema.ts')],
outDir: build_dir,
compilerOptions: {
lib: ['ES2022'],
target: 'ES2020',
},
shims: {
deno: true,
},
package: {
name: 'datacenter-module-schema',
version: '0.0.1',
description: 'module schema transpilation',
},
});

console.log('Finishing building temp package, generating JSON schema...');

for (const version of all_versions) {
const { stdout: type_schema_string } = await exec('deno', {
args: [
'run',
'--allow-read',
'npm:[email protected]',
'--path',
path.join(build_dir, 'src/modules', version, 'index.ts'),
'--expose',
'none',
'--type',
'DatacenterModule' + version.toUpperCase(),
'--tsconfig',
path.join(__dirname, '../tsconfig.json'),
'--no-type-check',
],
stdout: 'piped',
});
const type_schema = JSON.parse(type_schema_string);
type_schema.$schema = 'https://json-schema.org/draft/2019-09/schema';
await Deno.writeTextFile(path.join(modules_dir, version, './schema.json'), JSON.stringify(type_schema, null, 2));
}

const { stdout: type_schema_string, stderr: err } = await exec('deno', {
args: [
'run',
'--allow-read',
'npm:[email protected]',
'--path',
path.join(build_dir, 'src/modules/schema.ts'),
'--expose',
'none',
'--type',
'DatacenterModuleSchema',
'--tsconfig',
path.join(__dirname, '../tsconfig.json'),
'--no-type-check',
],
});

if (err.length > 0) {
console.error('Failed to generate module schema')
console.error(err);
Deno.exit(1);
}

let type_schema = JSON.parse(type_schema_string);
if (type_schema.definitions.DatacenterModuleSchema.anyOf) {
type_schema = {
oneOf: type_schema.definitions.DatacenterModuleSchema.anyOf,
$schema: 'https://json-schema.org/draft/2019-09/schema',
$id: 'https://architect.io/.schemas/module.json',
type: 'object',
required: ['version'],
discriminator: {
propertyName: 'version',
},
};
} else {
type_schema.$schema = 'https://json-schema.org/draft/2019-09/schema';
type_schema.$id = 'https://architect.io/.schemas/module.json';
}

await Deno.writeTextFile(
path.join(modules_dir, './module.schema.json'),
JSON.stringify(type_schema, null, 2),
);

await Deno.writeTextFile(
path.join(modules_dir, './module-schema.ts'),
`export default ${JSON.stringify(type_schema, null, 2)}`,
);
console.log(`Done! Updated schema is located at ${path.join(modules_dir, './module.schema.json')}`);

Deno.removeSync(build_dir, { recursive: true });
40 changes: 0 additions & 40 deletions bin/provider-generator.ts

This file was deleted.

10 changes: 8 additions & 2 deletions bin/resource-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ for (const type of all_types) {
args: [
'run',
'--allow-read',
'npm:ts-json-schema-generator',
'npm:ts-json-schema-generator@1.5.0',
'--path',
path.join(build_dir, 'src', type.name, 'inputs.ts'),
'--type',
Expand All @@ -70,6 +70,9 @@ for (const type of all_types) {
});

const typeSchema = JSON.parse(typeSchemaString);
typeSchema.$schema = 'https://json-schema.org/draft/2019-09/schema';
typeSchema.$id = 'https://architect.io/.schemas/resources/' + type.name + '/inputs.json';

await Deno.writeTextFile(
path.join(resources_dir, type.name, './inputs.schema.json'),
JSON.stringify(typeSchema, null, 2),
Expand All @@ -84,7 +87,7 @@ for (const type of all_types) {
args: [
'run',
'--allow-read',
'npm:ts-json-schema-generator',
'npm:ts-json-schema-generator@1.5.0',
'--path',
path.join(build_dir, 'src', type.name, 'outputs.ts'),
'--type',
Expand All @@ -96,6 +99,9 @@ for (const type of all_types) {
});

const typeSchema = JSON.parse(typeSchemaString);
typeSchema.$schema = 'https://json-schema.org/draft/2019-09/schema';
typeSchema.$id = 'https://architect.io/.schemas/resources/' + type.name + '/outputs.json';

await Deno.writeTextFile(
path.join(resources_dir, type.name, './outputs.schema.json'),
JSON.stringify(typeSchema, null, 2),
Expand Down
Loading

0 comments on commit 8f95a5c

Please sign in to comment.