@@ -3,15 +3,19 @@ import checkExists from "./checkExists";
3
3
import SemanticReleaseError from "./semanticReleaseError" ;
4
4
import errors from "./definitions/errors" ;
5
5
import JsonSchemaToTypes from "@etclabscore/json-schema-to-types" ;
6
+ import { camelCase } from "lodash" ;
6
7
import * as fs from "fs" ;
7
8
import { promisify } from "util" ;
8
9
const readFile = promisify ( fs . readFile ) ;
9
10
const writeFile = promisify ( fs . writeFile ) ;
11
+ const cp = promisify ( fs . copyFile ) ;
12
+ const mkdir = promisify ( fs . mkdir ) ;
10
13
14
+ const tsc = require ( "node-typescript-compiler" ) ; //eslint-disable-line
11
15
12
16
interface PluginConfig {
13
- schemaLocation : string | string [ ] ;
14
- outputName ? : string ;
17
+ outpath : string ;
18
+ schemaLocation : string ;
15
19
languages ?: {
16
20
ts ?: boolean ;
17
21
go ?: boolean ;
@@ -32,29 +36,18 @@ let verified = false;
32
36
33
37
export const verifyConditions : PluginFunction = async ( pluginConfig , context ) : Promise < boolean > => {
34
38
const cwd = process . cwd ( ) ;
35
- const config : PluginConfig = {
36
- schemaLocation : pluginConfig . schemaLocation ,
37
- } ;
38
39
39
- const paths = [ ] ;
40
- if ( config . schemaLocation === undefined ) {
40
+ if ( pluginConfig . schemaLocation === undefined ) {
41
41
throw new SemanticReleaseError ( "You must provide a schema location" , "123321" , "json-schema-tools transpiler requires a schema" ) ;
42
- } else if ( config . schemaLocation instanceof Array ) {
43
- config . schemaLocation . forEach ( ( loc ) => paths . push ( path . resolve ( cwd , loc ) ) )
44
- } else {
45
- paths . push ( path . resolve ( cwd , config . schemaLocation ) )
46
42
}
47
43
48
- const existsPromises = paths . map ( async ( p ) => {
49
- const exists = await checkExists ( p ) ;
50
- if ( ! exists ) {
51
- const noDocumentError = errors . ENODOCUMENT ( ) ;
52
- throw new SemanticReleaseError ( noDocumentError . message , noDocumentError . code , noDocumentError . details ) ;
53
- }
54
- return exists ;
55
- } ) ;
44
+ const sPath = path . resolve ( cwd , pluginConfig . schemaLocation ) ;
56
45
57
- await Promise . all ( existsPromises ) ;
46
+ const exists = await checkExists ( sPath ) ;
47
+ if ( ! exists ) {
48
+ const noDocumentError = errors . ENODOCUMENT ( ) ;
49
+ throw new SemanticReleaseError ( noDocumentError . message , noDocumentError . code , noDocumentError . details ) ;
50
+ }
58
51
59
52
verified = true ;
60
53
return verified ;
@@ -68,19 +61,44 @@ export const prepare: PluginFunction = async (pluginConfig, context): Promise<bo
68
61
throw new SemanticReleaseError ( "No nextRelease version" , "ENOVERSION" , "Something went wrong and there is no next release version" ) ; //tslint:disable-line
69
62
}
70
63
71
- const cwd = process . cwd ( ) ;
72
- const schemas = [ ] ;
73
- if ( pluginConfig . schemaLocation instanceof Array ) {
74
- pluginConfig . schemaLocation . forEach ( async ( loc ) => schemas . push ( JSON . parse ( await readFile ( path . resolve ( cwd , loc ) , "utf8" ) ) ) ) ;
75
- } else {
76
- schemas . push ( JSON . parse ( await readFile ( path . resolve ( cwd , pluginConfig . schemaLocation ) , "utf8" ) ) )
64
+ const outpath = pluginConfig . outpath || process . cwd ( ) ;
65
+ await mkdir ( `./${ outpath } /src` , { recursive : true } ) ;
66
+
67
+ const schemaPath = path . resolve ( process . cwd ( ) , pluginConfig . schemaLocation ) ;
68
+ await cp ( schemaPath , `${ outpath } /src/schema.json` ) ;
69
+
70
+ const schema = JSON . parse ( await readFile ( schemaPath , "utf8" ) ) ;
71
+
72
+ if ( ! schema . title ) {
73
+ throw new SemanticReleaseError ( "The schema must have a title" , "ENOTITLE" , "Schema requires a title" ) ;
77
74
}
78
75
79
- const transpiler = new JsonSchemaToTypes ( schemas ) ;
80
- const outTS = path . resolve ( cwd , pluginConfig . outputName || " generated-typings") ;
76
+ const transpiler = new JsonSchemaToTypes ( schema ) ;
77
+ const outTS = path . resolve ( outpath , "src/ generated-typings") ;
81
78
82
79
if ( ! pluginConfig . languages || pluginConfig . languages . ts ) {
83
80
await writeFile ( `${ outTS } .ts` , transpiler . toTs ( ) ) ;
81
+
82
+ const indexTS = path . resolve ( outpath , "src/index.ts" ) ;
83
+ const regularName = camelCase ( schema . title ) ;
84
+ const ts = [
85
+ `import * as Generated from "./generated-typings";` ,
86
+ `import ${ regularName } from "${ schemaPath } "` ,
87
+ `export default { ${ regularName } , ...Generated };`
88
+ ] . join ( "\n" ) ;
89
+ await writeFile ( indexTS , ts )
90
+ await tsc . compile ( {
91
+ "target" : "es6" ,
92
+ "module" : "commonjs" ,
93
+ "lib" : [
94
+ "es2015" ,
95
+ ] ,
96
+ "declaration" : true ,
97
+ "outDir" : "./build" ,
98
+ "strict" : true ,
99
+ "esModuleInterop" : true ,
100
+ "resolveJsonModule" : true
101
+ } ) ;
84
102
}
85
103
if ( ! pluginConfig . languages || pluginConfig . languages . go ) {
86
104
await writeFile ( `${ outTS } .go` , transpiler . toGo ( ) ) ;
0 commit comments