Skip to content

Commit e93f568

Browse files
authored
patch: Fix for window (#11)
* ➕ Add new function * ➗ New version * ➖ Delete comment
1 parent 08578ff commit e93f568

File tree

2 files changed

+76
-74
lines changed

2 files changed

+76
-74
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "agoda-graphql-csharp-generator",
3-
"version": "1.0.9",
3+
"version": "1.1.0",
44
"main": "index.js",
55
"files": [
66
"dist"

src/generate.ts

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,132 @@
11
#!/usr/bin/env node
22

3-
import { promises as fs } from 'fs';
4-
import * as path from 'path';
5-
import { exec } from 'child_process';
3+
import { promises as fs } from 'fs'
4+
import * as path from 'path'
5+
import { exec } from 'child_process'
66

77
// Helper to find the gql-gen binary
88
const getGqlGenCommand = (): string => {
99
try {
1010
// Find the graphql-code-generator package that provides gql-gen
11-
const gqlGenPath = require.resolve('graphql-code-generator/dist/cli.js');
12-
return `node "${gqlGenPath}"`;
11+
const gqlGenPath = require.resolve('graphql-code-generator/dist/cli.js')
12+
return `node "${gqlGenPath}"`
1313
} catch (error) {
1414
// Fallback to npx with correct package name
15-
console.warn('Could not resolve gql-gen binary, falling back to npx');
16-
return 'npx graphql-code-generator';
15+
console.warn('Could not resolve gql-gen binary, falling back to npx')
16+
return 'npx graphql-code-generator'
1717
}
18-
};
18+
}
1919

2020
// Helper to get argument value from CLI
2121
const getArgValue = (argName: string): string | undefined => {
22-
const args = process.argv.slice(2);
23-
const index = args.indexOf(`--${argName}`);
22+
const args = process.argv.slice(2)
23+
const index = args.indexOf(`--${argName}`)
2424
if (index !== -1 && args[index + 1]) {
25-
return args[index + 1];
25+
let value = args[index + 1]
26+
if ((value.startsWith("'") && value.endsWith("'")) || (value.startsWith('"') && value.endsWith('"'))) {
27+
value = value.slice(1, -1)
28+
}
29+
return value
2630
}
27-
return undefined;
28-
};
31+
return undefined
32+
}
2933

3034
// Delete files asynchronously
3135
const deleteFiles = async (listFiles: string[]): Promise<void> => {
32-
await Promise.all(listFiles.map(async (file) => {
33-
try {
34-
await fs.unlink(file);
35-
console.log(`Deleted: ${file}`);
36-
} catch (err) {
37-
console.error(`Failed to delete ${file}:`, err);
38-
}
39-
}));
40-
};
36+
await Promise.all(
37+
listFiles.map(async (file) => {
38+
try {
39+
await fs.unlink(file)
40+
console.log(`Deleted: ${file}`)
41+
} catch (err) {
42+
console.error(`Failed to delete ${file}:`, err)
43+
}
44+
})
45+
)
46+
}
4147

4248
// Remove input types region and update namespace/usings
4349
async function removeInputTypesRegion(filePath: string): Promise<void> {
44-
const fileName = path.parse(filePath).name.replace('.generated', '');
45-
const folder = path.dirname(filePath).replace(/\//g, '.');
46-
const namespace = `${folder}.${fileName}`;
50+
const fileName = path.parse(filePath).name.replace('.generated', '')
51+
const folder = path.dirname(filePath).replace(/\//g, '.')
52+
const namespace = `${folder}.${fileName}`
4753

48-
let content = await fs.readFile(filePath, 'utf8');
49-
content = content.replace(/#region input types[\s\S]*?#endregion[^\n]*\n?/g, '');
50-
content = content.replace(/using Agoda\.CodeGen\.GraphQL/g, 'using Agoda.Graphql');
51-
content = content.replace(`namespace Generated.${fileName}`, `namespace ${namespace}`);
52-
content = content.replace(/sumary/g, "summary");
54+
let content = await fs.readFile(filePath, 'utf8')
55+
content = content.replace(/#region input types[\s\S]*?#endregion[^\n]*\n?/g, '')
56+
content = content.replace(/using Agoda\.CodeGen\.GraphQL/g, 'using Agoda.Graphql')
57+
content = content.replace(`namespace Generated.${fileName}`, `namespace ${namespace}`)
58+
content = content.replace(/sumary/g, 'summary')
5359

54-
await fs.writeFile(filePath, content, 'utf8');
60+
await fs.writeFile(filePath, content, 'utf8')
5561
}
5662

5763
// Generate GraphQL code and post-process
5864
const generateGraphql = async (schemaUrl: string, filePath: string): Promise<void> => {
59-
const folder = path.dirname(filePath);
60-
const gqlGenCommand = getGqlGenCommand();
65+
const folder = path.dirname(filePath)
66+
const gqlGenCommand = getGqlGenCommand()
6167

6268
await new Promise<void>((resolve, reject) => {
6369
exec(
64-
`${gqlGenCommand} --schema '${schemaUrl}' --template agoda-graphql-codegen-csharp --out ${folder} ${filePath}`,
70+
`${gqlGenCommand} --schema "${schemaUrl}" --template agoda-graphql-codegen-csharp --out "${folder}" "${filePath}"`,
6571
(error, stdout, stderr) => {
6672
if (error) {
67-
console.error(`Error: ${error.message}`);
68-
reject(error);
69-
return;
73+
console.error(`Error: ${error.message}`)
74+
reject(error)
75+
return
7076
}
71-
resolve();
77+
resolve()
7278
}
73-
);
74-
});
79+
)
80+
})
7581

76-
const oldPath = path.join(folder, 'Classes.cs');
77-
const fileName = `${path.parse(filePath).name}.generated.cs`;
78-
const newPath = path.join(folder, fileName);
82+
const oldPath = path.join(folder, 'Classes.cs')
83+
const fileName = `${path.parse(filePath).name}.generated.cs`
84+
const newPath = path.join(folder, fileName)
7985

80-
await fs.rename(oldPath, newPath);
81-
await removeInputTypesRegion(newPath);
82-
console.log('generated: ', newPath);
83-
};
86+
await fs.rename(oldPath, newPath)
87+
await removeInputTypesRegion(newPath)
88+
console.log('generated: ', newPath)
89+
}
8490

8591
// Recursively get files with a specific extension
86-
const getGraphqlFiles = async (
87-
dir: string,
88-
extensionName: string,
89-
files: string[] = []
90-
): Promise<string[]> => {
91-
const entries = await fs.readdir(dir, { withFileTypes: true });
92+
const getGraphqlFiles = async (dir: string, extensionName: string, files: string[] = []): Promise<string[]> => {
93+
const entries = await fs.readdir(dir, { withFileTypes: true })
9294
for (const entry of entries) {
93-
const fullPath = path.join(dir, entry.name);
95+
const fullPath = path.join(dir, entry.name)
9496
if (entry.isDirectory()) {
95-
await getGraphqlFiles(fullPath, extensionName, files);
97+
await getGraphqlFiles(fullPath, extensionName, files)
9698
} else if (entry.isFile() && entry.name.endsWith(extensionName)) {
97-
files.push(fullPath);
99+
files.push(fullPath)
98100
}
99101
}
100-
return files;
101-
};
102+
return files
103+
}
102104

103105
// Main runner
104106
const run = async (): Promise<void> => {
105-
const rawGraphqlDirectory = getArgValue('graphql-dir');
106-
const schemaUrl = getArgValue('schema-url');
107+
const rawGraphqlDirectory = getArgValue('graphql-dir')
108+
const schemaUrl = getArgValue('schema-url')
107109

108110
if (!rawGraphqlDirectory || !schemaUrl) {
109-
console.error('Usage: script --graphql-dir <dir> --schema-url <url>');
110-
process.exit(1);
111+
console.error('Usage: script --graphql-dir <dir> --schema-url <url>')
112+
process.exit(1)
111113
}
112114

113-
console.log('raw graphql directory: ', rawGraphqlDirectory);
114-
console.log('graphql schema url: ', schemaUrl);
115+
console.log('raw graphql directory: ', rawGraphqlDirectory)
116+
console.log('graphql schema url: ', schemaUrl)
115117

116-
const oldGeneratedFiles = await getGraphqlFiles(rawGraphqlDirectory, '.generated.cs');
117-
console.log('removing generated files...');
118-
await deleteFiles(oldGeneratedFiles);
118+
const oldGeneratedFiles = await getGraphqlFiles(rawGraphqlDirectory, '.generated.cs')
119+
console.log('removing generated files...')
120+
await deleteFiles(oldGeneratedFiles)
119121

120-
const graphqlFiles = await getGraphqlFiles(rawGraphqlDirectory, '.graphql');
122+
const graphqlFiles = await getGraphqlFiles(rawGraphqlDirectory, '.graphql')
121123

122124
for (const f of graphqlFiles) {
123-
await generateGraphql(schemaUrl, f);
125+
await generateGraphql(schemaUrl, f)
124126
}
125-
};
127+
}
126128

127129
run().catch((err) => {
128-
console.error('Fatal error:', err);
129-
process.exit(1);
130-
});
130+
console.error('Fatal error:', err)
131+
process.exit(1)
132+
})

0 commit comments

Comments
 (0)