2
2
const yargs = require ( "yargs" ) ;
3
3
4
4
const { promises : fsPromises } = require ( "fs" ) ;
5
- const { join } = require ( "path" ) ;
6
- const { spawnProcess } = require ( "../utils/spawn-process" ) ;
5
+ const { join, resolve } = require ( "path" ) ;
7
6
8
- const { models } = yargs ( process . argv . slice ( 2 ) )
7
+ const { models, protocols } = yargs ( process . argv . slice ( 2 ) )
9
8
. alias ( "m" , "models" )
10
9
. string ( "m" )
11
10
. describe ( "m" , "The path to directory with aws-models." )
12
11
. demandOption ( [ "models" ] )
12
+ . alias ( "p" , "protocols" )
13
+ . string ( "p" )
14
+ . describe ( "p" , "List of protocols to copy e.g. awsJson1,restXml, default all" )
13
15
. help ( ) . argv ;
14
16
17
+ const getSdkId = ( model ) => {
18
+ const { shapes } = model ;
19
+ const service = Object . values ( shapes ) . find ( ( shape ) => shape . type === "service" ) || { } ;
20
+ const { traits } = service ;
21
+ for ( const [ trait , value ] of Object . entries ( traits ) ) {
22
+ if ( trait === "aws.api#service" ) {
23
+ return value . sdkId ;
24
+ }
25
+ }
26
+ return "unknown" ;
27
+ } ;
28
+
29
+ const getWireProtocol = ( model ) => {
30
+ const { shapes } = model ;
31
+ const service = Object . values ( shapes ) . find ( ( shape ) => shape . type === "service" ) || { } ;
32
+ for ( const trait of Object . keys ( service . traits || { } ) ) {
33
+ if ( trait . startsWith ( "aws.protocols#" ) ) {
34
+ return trait . split ( "aws.protocols#" ) . pop ( ) || "unknown" ;
35
+ }
36
+ }
37
+ return "unknown" ;
38
+ } ;
39
+
15
40
( async ( ) => {
16
41
const OUTPUT_DIR = join ( __dirname , ".." , ".." , "codegen" , "sdk-codegen" , "aws-models" ) ;
17
42
@@ -28,29 +53,32 @@ const { models } = yargs(process.argv.slice(2))
28
53
// Test if file exists.
29
54
await fsPromises . stat ( smithyModelsFile ) ;
30
55
// File exists, copy it.
31
- try {
32
- const fileContent = ( await fsPromises . readFile ( smithyModelsFile ) ) . toString ( ) ;
56
+ const absolutePath = resolve ( smithyModelsFile ) ;
33
57
34
- const sdkIdRE = / " s d k I d " : " ( [ ^ " ] * ) " / ;
35
- const sdkId = fileContent . match ( sdkIdRE ) [ 1 ] . toLowerCase ( ) . replace ( / \s / g, "-" ) ;
58
+ try {
59
+ const model = require ( absolutePath ) ;
60
+ const sdkId = getSdkId ( model ) . toLowerCase ( ) . replace ( / \s / g, "-" ) ;
61
+ const protocol = getWireProtocol ( model ) ;
36
62
37
- // Copy file.
38
- const outputFile = join ( OUTPUT_DIR , `${ sdkId } .json` ) ;
39
- await fsPromises . writeFile ( outputFile , fileContent ) ;
63
+ if ( ! protocols || protocols . split ( "," ) . includes ( protocol ) ) {
64
+ // Copy file.
65
+ const outputFile = join ( OUTPUT_DIR , `${ sdkId } .json` ) ;
66
+ await fsPromises . writeFile ( outputFile , JSON . stringify ( model , null , 2 ) ) ;
67
+ console . log ( "Copied" , outputFile ) ;
68
+ }
40
69
} catch ( e ) {
41
70
// Copy failed, log.
42
- console . log ( smithyModelsFile ) ;
71
+ console . log ( "Failed to copy" , absolutePath ) ;
43
72
console . log ( e . message ) ;
44
73
}
45
74
} catch ( e ) {
46
75
// File doesn't exist, ignore.
47
- console . log ( e . message ) ;
76
+ console . log ( "File not found" , e . message ) ;
48
77
}
49
78
}
50
79
51
- // Prettify copied models
52
- await spawnProcess ( join ( __dirname , ".." , ".." , "node_modules" , ".bin" , "prettier" ) , [
53
- "--write" ,
54
- `${ OUTPUT_DIR } /*.json` ,
55
- ] ) ;
80
+ console . log ( "args:" , {
81
+ models,
82
+ protocols,
83
+ } ) ;
56
84
} ) ( ) ;
0 commit comments