Skip to content

Commit

Permalink
Add AML lib in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
loicknuchel committed Dec 1, 2024
1 parent 273eeb9 commit f7dddde
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dry-publish": "pnpm run build && pnpm test && pnpm pack"
},
"dependencies": {
"@azimutt/aml": "workspace:^",
"@azimutt/gateway": "workspace:^",
"@azimutt/models": "workspace:^",
"@azimutt/utils": "workspace:^",
Expand Down
25 changes: 22 additions & 3 deletions cli/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ParserResult,
TokenEditor
} from "@azimutt/models";
// import {generateAml, parseAml} from "@azimutt/aml";
import {generateAml, parseAml} from "@azimutt/aml";
import {track} from "@azimutt/gateway";
import {fileRead, fileWrite} from "./utils/file.js";
import {logger} from "./utils/logger.js";
Expand Down Expand Up @@ -41,14 +41,33 @@ export async function convertFile(path: string, opts: Opts): Promise<void> {
logger.log(`Done 👍️\n`)
}

// try it with: `npm run exec -- convert test.aml --from aml --to json --out test.aml.json`
function parseDialect(dialect: string, content: string): ParserResult<Database> {
if (dialect === 'aml') return ParserResult.failure([{message: 'AML parser not available', kind: 'NotImplemented', level: 'error', offset: {start: 0, end: 0}, position: {start: {line: 0, column: 0}, end: {line: 0, column: 0}}}]) // parseAml(content)
/* FIXME: @azimutt/aml is using ESM to be bundled with Rollup, but this produce and error here:
node:internal/modules/esm/resolve:257
throw new ERR_MODULE_NOT_FOUND(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/node_modules/.pnpm/@azimutt+aml@0.1.10/node_modules/@azimutt/aml/out/amlAst' imported from /node_modules/.pnpm/@azimutt+aml@0.1.10/node_modules/@azimutt/aml/out/index.js
at finalizeResolution (node:internal/modules/esm/resolve:257:11)
at moduleResolve (node:internal/modules/esm/resolve:913:10)
at defaultResolve (node:internal/modules/esm/resolve:1037:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:650:12)
at #cachedDefaultResolve (node:internal/modules/esm/loader:599:25)
at ModuleLoader.resolve (node:internal/modules/esm/loader:582:38)
at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:241:38)
at ModuleJob._link (node:internal/modules/esm/module_job:132:49) {
code: 'ERR_MODULE_NOT_FOUND',
url: 'file:///node_modules/.pnpm/@azimutt+aml@0.1.10/node_modules/@azimutt/aml/out/amlAst'
}
*/
if (dialect === 'aml') return ParserResult.failure([{message: 'AML parser not available', kind: 'NotImplemented', level: 'error', offset: {start: 0, end: 0}, position: {start: {line: 0, column: 0}, end: {line: 0, column: 0}}}]) // FIXME: return parseAml(content)
if (dialect === 'json') return parseJsonDatabase(content)
return ParserResult.failure([parserError(`Can't parse ${dialect} dialect`, 'BadArgument')])
}

function generateDialect(dialect: string, db: Database): Result<string, string> {
if (dialect === 'aml') return Result.failure('AML generator not available') // Result.success(generateAml(db))
if (dialect === 'aml') return Result.failure('AML generator not available') // FIXME: return Result.success(generateAml(db))
if (dialect === 'json') return Result.success(generateJsonDatabase(db))
return Result.failure(`Can't generate ${dialect} dialect`)
}
Expand Down
1 change: 1 addition & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ program.command('convert')
.requiredOption('-f, --from <from>', 'The dialect of the file to convert from')
.requiredOption('-t, --to <to>', 'The dialect to convert to')
.option('-o, --out <out>', 'The file to write')
.option('--debug', 'Add debug logs and show the full stacktrace instead of a shorter error')
.action((path, args) => exec(convertFile(path, args), args))

program.command('diff')
Expand Down
1 change: 0 additions & 1 deletion cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"target": "ES2021",
"rootDir": "./src",
"outDir": "./out",
"types": ["node"],
"esModuleInterop": true,
"resolveJsonModule": true,
"sourceMap": true,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f7dddde

Please sign in to comment.