Skip to content

Commit 6c6227c

Browse files
committed
generate-packages
1 parent 77d4250 commit 6c6227c

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"packageName": "pgsql-deparser",
3+
"versions": {
4+
"13": {
5+
"deparserVersion": "13.17.0",
6+
"typesVersion": "13.11.1",
7+
"pgVersion": "13",
8+
"npmTag": "v13"
9+
},
10+
"14": {
11+
"deparserVersion": "14.0.0",
12+
"typesVersion": "14.1.1",
13+
"pgVersion": "14",
14+
"npmTag": "v14"
15+
},
16+
"15": {
17+
"deparserVersion": "15.0.0",
18+
"typesVersion": "15.1.1",
19+
"pgVersion": "15",
20+
"npmTag": "v15"
21+
},
22+
"16": {
23+
"deparserVersion": "16.0.0",
24+
"typesVersion": "16.1.1",
25+
"pgVersion": "16",
26+
"npmTag": "v16"
27+
}
28+
},
29+
"packageTemplate": {
30+
"author": "Dan Lynch <[email protected]>",
31+
"homepage": "https://github.com/launchql/pgsql-parser",
32+
"license": "MIT",
33+
"publishConfig": {
34+
"access": "public",
35+
"directory": "dist"
36+
},
37+
"repository": {
38+
"type": "git",
39+
"url": "https://github.com/launchql/pgsql-parser"
40+
},
41+
"bugs": {
42+
"url": "https://github.com/launchql/pgsql-parser/issues"
43+
},
44+
"keywords": [
45+
"sql",
46+
"postgres",
47+
"postgresql",
48+
"pg",
49+
"query",
50+
"ast",
51+
"deparser",
52+
"database"
53+
]
54+
}
55+
}

packages/transform/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"strip-deparser-types": "ts-node scripts/strip-deparser-types.ts",
3232
"strip-all-types": "npm run strip-types && npm run strip-direct-types",
3333
"generate-deparsers": "ts-node scripts/generate-version-deparsers.ts",
34+
"generate-packages": "ts-node scripts/generate-version-packages.ts",
3435
"organize-versions": "ts-node scripts/organize-transformers-by-version.ts",
3536
"prepare-versions": "npm run strip-all-types && npm run organize-versions",
3637
"lint": "eslint . --fix",
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
/**
5+
* Script to generate package.json files for each version directory
6+
* Uses version-config.json to map versions to their dependencies
7+
*/
8+
9+
const VERSIONS_DIR = 'versions';
10+
const CONFIG_FILE = 'config/deparser-versions.json';
11+
12+
interface VersionConfig {
13+
deparserVersion: string;
14+
typesVersion: string;
15+
pgVersion: string;
16+
npmTag: string;
17+
}
18+
19+
interface Config {
20+
packageName: string;
21+
versions: Record<string, VersionConfig>;
22+
packageTemplate: Record<string, any>;
23+
}
24+
25+
function loadConfig(): Config {
26+
const configPath = path.join(process.cwd(), CONFIG_FILE);
27+
const configContent = fs.readFileSync(configPath, 'utf-8');
28+
return JSON.parse(configContent);
29+
}
30+
31+
function generatePackageJson(packageName: string, version: string, versionConfig: VersionConfig, template: Record<string, any>): any {
32+
return {
33+
name: packageName,
34+
version: versionConfig.deparserVersion,
35+
description: `PostgreSQL v${version} AST Deparser - Transforms v${version} ASTs to v17 and deparses them`,
36+
main: "index.js",
37+
module: "esm/index.js",
38+
types: "index.d.ts",
39+
...template,
40+
scripts: {
41+
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
42+
"clean": "rimraf dist",
43+
"prepare": "npm run build",
44+
"build": "npm run clean && tsc && tsc -p tsconfig.esm.json && npm run copy"
45+
},
46+
dependencies: {
47+
[`@pgsql/types`]: `^${versionConfig.typesVersion}`
48+
},
49+
keywords: [
50+
...template.keywords,
51+
`v${version}`,
52+
`postgresql-${version}`
53+
]
54+
};
55+
}
56+
57+
function generateTsConfig(): any {
58+
return {
59+
"compilerOptions": {
60+
"target": "es2018",
61+
"module": "commonjs",
62+
"lib": ["es2018"],
63+
"declaration": true,
64+
"outDir": "./dist",
65+
"rootDir": "./",
66+
"strict": false,
67+
"esModuleInterop": true,
68+
"skipLibCheck": true,
69+
"forceConsistentCasingInFileNames": true,
70+
"moduleResolution": "node",
71+
"resolveJsonModule": true
72+
},
73+
"include": [
74+
"**/*.ts"
75+
],
76+
"exclude": [
77+
"node_modules",
78+
"dist",
79+
"**/*.test.ts"
80+
]
81+
};
82+
}
83+
84+
function generateTsConfigEsm(): any {
85+
return {
86+
"extends": "./tsconfig.json",
87+
"compilerOptions": {
88+
"module": "esnext",
89+
"outDir": "./dist/esm",
90+
"declaration": false
91+
}
92+
};
93+
}
94+
95+
function generateVersionPackages(): void {
96+
console.log('Generating package.json files for each version...\n');
97+
98+
const config = loadConfig();
99+
100+
for (const [version, versionConfig] of Object.entries(config.versions)) {
101+
console.log(`Processing version ${version}...`);
102+
103+
const versionDir = path.join(VERSIONS_DIR, version);
104+
105+
if (!fs.existsSync(versionDir)) {
106+
console.error(` ✗ Version directory ${versionDir} does not exist!`);
107+
continue;
108+
}
109+
110+
// Generate package.json
111+
const packageJson = generatePackageJson(config.packageName, version, versionConfig, config.packageTemplate);
112+
const packageJsonPath = path.join(versionDir, 'package.json');
113+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
114+
console.log(` ✓ Created package.json`);
115+
116+
// Generate tsconfig.json
117+
const tsConfig = generateTsConfig();
118+
const tsConfigPath = path.join(versionDir, 'tsconfig.json');
119+
fs.writeFileSync(tsConfigPath, JSON.stringify(tsConfig, null, 2));
120+
console.log(` ✓ Created tsconfig.json`);
121+
122+
// Generate tsconfig.esm.json
123+
const tsConfigEsm = generateTsConfigEsm();
124+
const tsConfigEsmPath = path.join(versionDir, 'tsconfig.esm.json');
125+
fs.writeFileSync(tsConfigEsmPath, JSON.stringify(tsConfigEsm, null, 2));
126+
console.log(` ✓ Created tsconfig.esm.json`);
127+
128+
// Update README with package name and npm tag
129+
updateReadmeWithPackageName(version, config.packageName, versionConfig.npmTag);
130+
131+
console.log('');
132+
}
133+
134+
console.log('Done! Package files have been generated for all versions.');
135+
}
136+
137+
function updateReadmeWithPackageName(version: string, packageName: string, npmTag: string): void {
138+
const versionDir = path.join(VERSIONS_DIR, version);
139+
const readmePath = path.join(versionDir, 'README.md');
140+
141+
if (!fs.existsSync(readmePath)) {
142+
return;
143+
}
144+
145+
let content = fs.readFileSync(readmePath, 'utf-8');
146+
147+
// Remove any existing installation sections
148+
content = content.replace(/## Installation[\s\S]*?(?=##|$)/gm, '');
149+
150+
// Add installation instructions at the beginning
151+
const installSection = `## Installation
152+
153+
\`\`\`bash
154+
# Install specific version using npm tag
155+
npm install ${packageName}@${npmTag}
156+
157+
# Or using yarn
158+
yarn add ${packageName}@${npmTag}
159+
\`\`\`
160+
161+
`;
162+
163+
// Insert after the title
164+
const lines = content.split('\n');
165+
const titleIndex = lines.findIndex(line => line.startsWith('# '));
166+
if (titleIndex !== -1) {
167+
// Find the next non-empty line after title
168+
let insertIndex = titleIndex + 1;
169+
while (insertIndex < lines.length && lines[insertIndex].trim() === '') {
170+
insertIndex++;
171+
}
172+
lines.splice(insertIndex, 0, installSection);
173+
content = lines.join('\n');
174+
}
175+
176+
// Update import examples
177+
content = content.replace(/from '\.\/index'/g, `from '${packageName}'`);
178+
content = content.replace(/from 'pgsql-deparser-v\d+'/g, `from '${packageName}'`);
179+
180+
fs.writeFileSync(readmePath, content);
181+
console.log(` ✓ Updated README.md with package name`);
182+
}
183+
184+
// Run the script
185+
generateVersionPackages();

packages/transform/scripts/organize-transformers-by-version.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ function organizeByVersion(): void {
235235
} catch (error) {
236236
console.error('Failed to generate deparser files:', error);
237237
}
238+
239+
// Generate package.json files
240+
console.log('\nGenerating package.json files...');
241+
try {
242+
execSync('npx ts-node scripts/generate-version-packages.ts', { stdio: 'inherit' });
243+
} catch (error) {
244+
console.error('Failed to generate package files:', error);
245+
}
238246
}
239247

240248
// Run the script

0 commit comments

Comments
 (0)