@@ -57,16 +57,18 @@ function getIdentifier(node: recast.types.namedTypes.Node): string | null {
5757function objectProperty (
5858 b : recast . types . builders ,
5959 capability : string ,
60- info : MetaPackage | Package
60+ info : MetaPackage | Package ,
61+ comments : recast . types . namedTypes . ObjectProperty [ "comments" ] = null
6162) : recast . types . namedTypes . ObjectProperty {
62- return b . objectProperty (
63- b . stringLiteral ( capability ) ,
64- b . objectExpression (
63+ return b . objectProperty . from ( {
64+ comments,
65+ key : b . stringLiteral ( capability ) ,
66+ value : b . objectExpression (
6567 Object . entries ( info ) . map ( ( [ key , value ] ) =>
6668 b . objectProperty ( b . identifier ( key ) , valueLiteral ( b , value ) )
6769 )
68- )
69- ) ;
70+ ) ,
71+ } ) ;
7072}
7173
7274function valueLiteral ( b : recast . types . builders , value : unknown ) {
@@ -94,14 +96,16 @@ async function main() {
9496 throw new Error(` Profile file not found : ${profilePath } `);
9597 }
9698
97- const content = fs . readFileSync ( profilePath , "utf-8" ) ;
98- const ast = recast . parse ( content , {
99+ const options = {
99100 parser: typescript,
100101 lineTerminator: "\n",
101102 sourceFileName: profilePath,
102103 trailingComma: true,
103104 tokens: false,
104- } ) ;
105+ };
106+
107+ const content = fs.readFileSync(profilePath, "utf-8");
108+ const ast = recast.parse(content, options);
105109
106110 const exportedProfile = ast.program.body.find(
107111 (node) =>
@@ -121,7 +125,8 @@ async function main() {
121125 for (let i = 0; i < numProperties; ++i) {
122126 const id = getIdentifier(properties[i]);
123127 if (isValidIdentifier(id) && id in modules) {
124- properties [ i ] = objectProperty ( b , id , modules [ id ] ) ;
128+ const comments = properties[i].comments;
129+ properties[i] = objectProperty(b, id, modules[id], comments);
125130 delete modules[id];
126131 }
127132 }
@@ -134,7 +139,7 @@ async function main() {
134139 properties.push(objectProperty(b, capability, info));
135140 }
136141
137- fs . writeFileSync ( profilePath , recast . print ( ast ) . code , "utf-8" ) ;
142+ fs.writeFileSync(profilePath, recast.print(ast, options ).code, "utf-8");
138143 info(` Updated profile : ${profilePath } `) ;
139144 }
140145}
0 commit comments