Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
chore(Typescript bindings): Fix packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jul 25, 2019
1 parent d04a423 commit 395f3b6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3,024 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.DS_Store
/.mypy_cache
*.out.*
/types.ts
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"scripts": {
"format": "npx prettier --write './**/*.{js,json,md,ts,yaml}'",
"lint": "npx eslint './**/*.{js,ts}' --fix --env node --env es6 --env jest",
"test": "jest",
"test": "npm run build:jsonschema && jest",
"build": "npm run build:ts",
"build:jsonschema": "gulp jsonschema",
"build:jsonschema": "ts-node src/schema.ts",
"build:jsonld": "gulp jsonld",
"build:ts": "ts-node src/typescript.ts",
"build:ts": "ts-node src/typescript.ts && tsc && cp built/*.schema.json dist",
"build:py": "ts-node src/python.ts",
"build:r": "ts-node src/r.ts",
"docs": "npm run docs:readme && npm run docs:build",
Expand Down
9 changes: 7 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ const processSchema = (schemas: Map<string, Schema>, schema: Schema): void => {

// Add to parent's children
parent.children =
parent.children === undefined ? [title] : [...parent.children, title].sort()
parent.children === undefined
? [title]
: [...parent.children, title].sort()

// Add to all ancestors' descendants and type enum
let ancestor: Schema | null = parent
Expand All @@ -167,7 +169,10 @@ const processSchema = (schemas: Map<string, Schema>, schema: Schema): void => {
ancestor.properties.type !== undefined &&
ancestor.properties.type.enum !== undefined
) {
ancestor.properties.type.enum = [ancestor.title, ...ancestor.descendants]
ancestor.properties.type.enum = [
ancestor.title,
...ancestor.descendants
]
}
ancestor = parentSchema(schemas, ancestor)
}
Expand Down
85 changes: 0 additions & 85 deletions src/typescript-jstt.ts

This file was deleted.

30 changes: 20 additions & 10 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'path'
import { props, read, Schema, types, unions } from './bindings'

/**
* Generate `dist/types.d.ts` from schemas.
* Generate `.../types.ts` from schemas.
*/
export const build = async (): Promise<string> => {
const schemas = await read()
Expand All @@ -19,22 +19,24 @@ export const build = async (): Promise<string> => {
.map(unionGenerator)
.join('')

const code = `
const code = `/* eslint-disable */
/**
* This file was automatically generated by ${path.basename(__filename)}.
* Do not modify it by hand. Instead, modify the source \`.schema.yaml\` files
* in the \`schema\` directory and run \`npm run build:ts\` to regenerate this file.
*/
${typesInterface(schemas)}
${typesCode}
${unionsCode}
`
const dist = path.join(__dirname, '..', 'dist')
await fs.ensureDir(dist)

const file = path.join(dist, 'types.ts')
const file = path.join(__dirname, '..', 'types.ts')
await fs.writeFile(file, code)

// Create an index.js for require.resolve('@stencila/schema') to work
// properly in Encoda
// TODO This won't be necessary when using tsc to compile an index.js
await fs.writeFile(path.join(dist, 'index.js'), '\n')

return file
}

Expand All @@ -44,6 +46,14 @@ ${unionsCode}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
if (module.parent === null) build()

/**
* Generate a `interface Types`, that maps all types
* and can be used to get a type from its name at compile time.
*/
export const typesInterface = (schemas: Schema[]): string => {
return `export interface Types {\n${schemas.map(({title}) => ` ${title}: ${title}`).join('\n')}\n}`
}

/**
* Generate a `interface` and a factory function for each type.
*/
Expand Down
Loading

0 comments on commit 395f3b6

Please sign in to comment.