From 03e0cbf7f5fc19822c21a496466964cd02ced7c9 Mon Sep 17 00:00:00 2001 From: Christian Dietrich Date: Wed, 27 Sep 2023 15:30:02 +0200 Subject: [PATCH] Fix generator-langium cli for esm (#1201) Signed-off-by: Christian Dietrich --- packages/generator-langium/templates/cli/bin/cli.js | 3 ++- .../generator-langium/templates/cli/src/cli/main.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/generator-langium/templates/cli/bin/cli.js b/packages/generator-langium/templates/cli/bin/cli.js index 338ac4b3e..e166dc613 100644 --- a/packages/generator-langium/templates/cli/bin/cli.js +++ b/packages/generator-langium/templates/cli/bin/cli.js @@ -1,3 +1,4 @@ #!/usr/bin/env node -import '../out/cli/main'; +import main from '../out/cli/main.js'; +main(); diff --git a/packages/generator-langium/templates/cli/src/cli/main.ts b/packages/generator-langium/templates/cli/src/cli/main.ts index 158ae38a2..996c9b481 100644 --- a/packages/generator-langium/templates/cli/src/cli/main.ts +++ b/packages/generator-langium/templates/cli/src/cli/main.ts @@ -6,6 +6,13 @@ import { create<%= LanguageName %>Services } from '../language/<%= language-id % import { extractAstNode } from './cli-util.js'; import { generateJavaScript } from './generator.js'; import { NodeFileSystem } from 'langium/node'; +import * as url from 'node:url'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); + +const packagePath = path.resolve(__dirname, '..', '..', 'package.json'); +const packageContent = await fs.readFile(packagePath, 'utf-8'); export const generateAction = async (fileName: string, opts: GenerateOptions): Promise => { const services = create<%= LanguageName %>Services(NodeFileSystem).<%= LanguageName %>; @@ -21,9 +28,7 @@ export type GenerateOptions = { export default function(): void { const program = new Command(); - program - // eslint-disable-next-line @typescript-eslint/no-var-requires - .version(require('../../package.json').version); + program.version(JSON.parse(packageContent).version); const fileExtensions = <%= LanguageName %>LanguageMetaData.fileExtensions.join(', '); program