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 (
15
+ "p" ,
16
+ "List of protocols to copy e.g. awsJson1,restXml, default all, list: \n" +
17
+ `awsJson1_1,
18
+ restJson1,
19
+ awsJson1_0,
20
+ awsQuery,
21
+ restXml,
22
+ ec2Query`
23
+ )
13
24
. help ( ) . argv ;
14
25
26
+ const getSdkId = ( model ) => {
27
+ const { shapes } = model ;
28
+ const service = Object . values ( shapes ) . find ( ( shape ) => shape . type === "service" ) || { } ;
29
+ const { traits } = service ;
30
+ for ( const [ trait , value ] of Object . entries ( traits ) ) {
31
+ if ( trait === "aws.api#service" ) {
32
+ return value . sdkId ;
33
+ }
34
+ }
35
+ return "unknown" ;
36
+ } ;
37
+
38
+ const getWireProtocol = ( model ) => {
39
+ const { shapes } = model ;
40
+ const service = Object . values ( shapes ) . find ( ( shape ) => shape . type === "service" ) || { } ;
41
+ for ( const trait of Object . keys ( service . traits || { } ) ) {
42
+ if ( trait . startsWith ( "aws.protocols#" ) ) {
43
+ return trait . split ( "aws.protocols#" ) . pop ( ) || "unknown" ;
44
+ }
45
+ }
46
+ return "unknown" ;
47
+ } ;
48
+
15
49
( async ( ) => {
16
50
const OUTPUT_DIR = join ( __dirname , ".." , ".." , "codegen" , "sdk-codegen" , "aws-models" ) ;
17
51
@@ -23,34 +57,42 @@ const { models } = yargs(process.argv.slice(2))
23
57
. filter ( ( file ) => file . isDirectory ( ) )
24
58
. map ( ( dir ) => join ( models . toString ( ) , dir . name , `smithy/model.json` ) ) ;
25
59
60
+ const observedProtocols = new Set ( ) ;
61
+
26
62
for ( const smithyModelsFile of smithyModelsFiles ) {
27
63
try {
28
64
// Test if file exists.
29
65
await fsPromises . stat ( smithyModelsFile ) ;
30
66
// File exists, copy it.
67
+ const absolutePath = resolve ( smithyModelsFile ) ;
68
+
31
69
try {
32
- const fileContent = ( await fsPromises . readFile ( smithyModelsFile ) ) . toString ( ) ;
70
+ const model = require ( absolutePath ) ;
71
+ const sdkId = getSdkId ( model ) . toLowerCase ( ) . replace ( / \s / g, "-" ) ;
72
+ const protocol = getWireProtocol ( model ) ;
33
73
34
- const sdkIdRE = / " s d k I d " : " ( [ ^ " ] * ) " / ;
35
- const sdkId = fileContent . match ( sdkIdRE ) [ 1 ] . toLowerCase ( ) . replace ( / \s / g, "-" ) ;
74
+ observedProtocols . add ( protocol ) ;
36
75
37
- // Copy file.
38
- const outputFile = join ( OUTPUT_DIR , `${ sdkId } .json` ) ;
39
- await fsPromises . writeFile ( outputFile , fileContent ) ;
76
+ if ( ! protocols || protocols . split ( "," ) . includes ( protocol ) ) {
77
+ // Copy file.
78
+ const outputFile = join ( OUTPUT_DIR , `${ sdkId } .json` ) ;
79
+ await fsPromises . writeFile ( outputFile , JSON . stringify ( model , null , 2 ) ) ;
80
+ console . log ( "Copied" , outputFile ) ;
81
+ }
40
82
} catch ( e ) {
41
83
// Copy failed, log.
42
- console . log ( smithyModelsFile ) ;
84
+ console . log ( "Failed to copy" , absolutePath ) ;
43
85
console . log ( e . message ) ;
44
86
}
45
87
} catch ( e ) {
46
88
// File doesn't exist, ignore.
47
- console . log ( e . message ) ;
89
+ console . log ( "File not found" , e . message ) ;
48
90
}
49
91
}
50
92
51
- // Prettify copied models
52
- await spawnProcess ( join ( __dirname , ".." , ".." , "node_modules" , ".bin" , "prettier" ) , [
53
- "--write" ,
54
- ` ${ OUTPUT_DIR } /*.json` ,
55
- ] ) ;
93
+ console . log ( "args:" , {
94
+ models ,
95
+ protocols ,
96
+ observedProtocols ,
97
+ } ) ;
56
98
} ) ( ) ;
0 commit comments