-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_schema.js
executable file
·85 lines (70 loc) · 2.81 KB
/
generate_schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Helper function to read JSON file
const readJSONFile = (filePath) => {
try {
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
} catch (error) {
console.error(`Failed to read or parse JSON file at ${filePath}:`, error);
process.exit(1);
}
};
// Check if a file path was provided
if (process.argv.length < 3) {
console.error('Usage: npx mylibrary <path_to_json_file>');
process.exit(1); // Exit the script if no file path is provided
}
// Get the JSON file path from the command-line arguments
const jsonFilePath = path.resolve(process.argv[2]);
// Read and parse the JSON file
const jsonData = readJSONFile(jsonFilePath);
// Destructure necessary data from the parsed JSON
const { endpoint, headers, types, esm } = jsonData;
// Read the version of @gqlts/cli from the library's package.json
const packageJSON = readJSONFile(path.resolve(__dirname, 'package.json'));
const gqltsCliVersion = packageJSON.devDependencies['@gqlts/cli'];
// Detect the package manager by checking the existence of lock files
let packageManager = 'npm';
if (fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'))) {
packageManager = 'yarn';
} else if (fs.existsSync(path.resolve(process.cwd(), 'pnpm-lock.yaml'))) {
packageManager = 'pnpm';
} else if (fs.existsSync(path.resolve(process.cwd(), 'bun.lockb'))) {
packageManager = 'bun';
}
// Check if @gqlts/cli is installed
let gqltsCliInstalled = false;
try {
require.resolve('@gqlts/cli');
gqltsCliInstalled = true;
} catch (error) {
console.log('@gqlts/cli is not installed. Installing...');
}
// Install @gqlts/cli if it's not installed
if (!gqltsCliInstalled) {
const installCommand = {
npm: `npm install --save-dev @gqlts/cli@${gqltsCliVersion}`,
yarn: `yarn add --dev @gqlts/cli@${gqltsCliVersion}`,
pnpm: `pnpm add --save-dev @gqlts/cli@${gqltsCliVersion}`,
bun: `bun add --dev @gqlts/cli@${gqltsCliVersion}`
}[packageManager];
try {
console.log(`Running "${installCommand}" using ${packageManager}`);
execSync(installCommand, { stdio: 'inherit' });
console.log('@gqlts/cli installed successfully.');
} catch (error) {
console.error(`Failed to install @gqlts/cli using ${packageManager}:`, error);
process.exit(1);
}
}
const output_path = path.resolve(__dirname, './src/generated');
// Execute the constructed command
try {
const typegenCommand = `./node_modules/.bin/gqlts -S ${types.map(type => `${type.uuid}:string`).join(' ')} ${esm ? '--esm' : ''} --endpoint ${endpoint} --output ${output_path} -H ${headers}`;
execSync(typegenCommand, { stdio: 'inherit' });
console.log('Type generation completed successfully.');
} catch (error) {
console.error('An error occurred during type generation:', error);
}