@tiangong-lca/cli 0.0.14 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -14,7 +14,6 @@ export function runDatasetImportLcaConvert(options) {
|
|
|
14
14
|
const processBundlesDir = options.processBundlesDir
|
|
15
15
|
? path.resolve(options.processBundlesDir)
|
|
16
16
|
: path.join(outputDir, 'process-bundles');
|
|
17
|
-
const processBundles = !options.detectOnly && options.processBundles !== false;
|
|
18
17
|
if (options.processBundles === false && options.processBundlesDir?.trim()) {
|
|
19
18
|
throw new CliError('--process-bundles-dir cannot be used with --no-process-bundles.', {
|
|
20
19
|
code: 'DATASET_IMPORT_LCA_PROCESS_BUNDLES_INVALID',
|
|
@@ -46,8 +45,11 @@ export function runDatasetImportLcaConvert(options) {
|
|
|
46
45
|
if (options.failOnWarning) {
|
|
47
46
|
commandArgs.push('--fail-on-warning');
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
// tidas-tools >= 0.0.28 enables process bundles by default and only accepts
|
|
49
|
+
// --no-process-bundles / --process-bundles-dir; a bare --process-bundles flag
|
|
50
|
+
// gets prefix-matched by argparse to --process-bundles-dir and aborts.
|
|
51
|
+
if (!options.detectOnly && options.processBundles === false) {
|
|
52
|
+
commandArgs.push('--no-process-bundles');
|
|
51
53
|
}
|
|
52
54
|
if (!options.detectOnly && options.processBundlesDir?.trim()) {
|
|
53
55
|
commandArgs.push('--process-bundles-dir', processBundlesDir);
|
|
@@ -64,6 +66,13 @@ export function runDatasetImportLcaConvert(options) {
|
|
|
64
66
|
encoding: 'utf8',
|
|
65
67
|
});
|
|
66
68
|
const conversionReport = readOptionalJson(reportPath);
|
|
69
|
+
// Mapping CSV is opt-in in tidas-tools >= 0.0.28 (mapping.csv.gz) and bundle
|
|
70
|
+
// output depends on converter defaults, so report what is actually on disk.
|
|
71
|
+
const mappingCsv = firstExistingPath([
|
|
72
|
+
path.join(outputDir, 'mapping.csv.gz'),
|
|
73
|
+
path.join(outputDir, 'mapping.csv'),
|
|
74
|
+
]);
|
|
75
|
+
const processBundlesIndexPath = path.join(processBundlesDir, 'index.json');
|
|
67
76
|
const files = {
|
|
68
77
|
report: path.join(outputDir, 'outputs', 'import-lca-report.json'),
|
|
69
78
|
conversion_report: reportPath,
|
|
@@ -71,9 +80,9 @@ export function runDatasetImportLcaConvert(options) {
|
|
|
71
80
|
ilcd_dir: !options.detectOnly && (target === 'ilcd' || target === 'both')
|
|
72
81
|
? path.join(outputDir, 'ilcd')
|
|
73
82
|
: null,
|
|
74
|
-
mapping_csv:
|
|
75
|
-
process_bundles_dir:
|
|
76
|
-
process_bundles_index:
|
|
83
|
+
mapping_csv: mappingCsv,
|
|
84
|
+
process_bundles_dir: existsSync(processBundlesDir) ? processBundlesDir : null,
|
|
85
|
+
process_bundles_index: existsSync(processBundlesIndexPath) ? processBundlesIndexPath : null,
|
|
77
86
|
};
|
|
78
87
|
const report = {
|
|
79
88
|
schema_version: 1,
|
|
@@ -175,6 +184,9 @@ function resolveCliRepoRoot(candidatesOverride) {
|
|
|
175
184
|
function buildPythonPath(tidasToolsRoot, env) {
|
|
176
185
|
return [path.join(tidasToolsRoot, 'src'), env.PYTHONPATH].filter(Boolean).join(path.delimiter);
|
|
177
186
|
}
|
|
187
|
+
function firstExistingPath(candidates) {
|
|
188
|
+
return candidates.find((candidate) => existsSync(candidate)) ?? null;
|
|
189
|
+
}
|
|
178
190
|
function readOptionalJson(filePath) {
|
|
179
191
|
if (!existsSync(filePath)) {
|
|
180
192
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataset-import-lca.js","sourceRoot":"","sources":["../../../src/lib/dataset-import-lca.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAA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AAI,CAAC,SAAS,CAAC,CAAC;AACjG,CAAC;AAED,SAAS,gBAAgB,CAAC,QAAgB;IACxC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AACpD,CAAC;AAED,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,eAAe;IACf,kBAAkB;IAClB,qBAAqB;CACtB,CAAC","sourcesContent":["import { existsSync, readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { spawnSync, type SpawnSyncReturns } from 'node:child_process';\nimport { writeJsonArtifact } from './artifacts.js';\nimport { CliError } from './errors.js';\n\nexport type DatasetImportLcaTarget = 'tidas' | 'ilcd' | 'both';\n\nexport type DatasetImportLcaReport = {\n schema_version: 1;\n status: 'completed' | 'blocked';\n generated_at_utc: string;\n input_path: string;\n output_dir: string;\n from_format: string;\n target: DatasetImportLcaTarget;\n detect_only: boolean;\n command: {\n executable: string;\n args: string[];\n cwd: string;\n exit_code: number | null;\n stdout: string;\n stderr: string;\n };\n conversion_report: unknown | null;\n files: {\n report: string;\n conversion_report: string;\n tidas_dir: string | null;\n ilcd_dir: string | null;\n mapping_csv: string | null;\n process_bundles_dir: string | null;\n process_bundles_index: string | null;\n };\n};\n\nexport type RunDatasetImportLcaConvertOptions = {\n inputPath: string;\n outputDir: string;\n fromFormat?: string | undefined;\n target?: string | undefined;\n language?: string | undefined;\n reportPath?: string | undefined;\n mappingDir?: string | undefined;\n failOnWarning?: boolean | undefined;\n validationJobs?: number | undefined;\n processBundles?: boolean | undefined;\n processBundlesDir?: string | undefined;\n detectOnly?: boolean | undefined;\n pythonBin?: string | undefined;\n tidasToolsDir?: string | undefined;\n env?: NodeJS.ProcessEnv | undefined;\n now?: Date | undefined;\n spawnImpl?: typeof spawnSync | undefined;\n};\n\nexport function runDatasetImportLcaConvert(\n options: RunDatasetImportLcaConvertOptions,\n): DatasetImportLcaReport {\n const inputPath = requireInputPath(options.inputPath);\n const outputDir = requireOutputDir(options.outputDir);\n const target = normalizeTarget(options.target);\n const fromFormat = options.fromFormat?.trim() || 'auto';\n const pythonBin = options.pythonBin?.trim() || 'python3';\n const tidasToolsRoot = resolveTidasToolsRoot(options.tidasToolsDir, options.env ?? process.env);\n const processBundlesDir = options.processBundlesDir\n ? path.resolve(options.processBundlesDir)\n : path.join(outputDir, 'process-bundles');\n const processBundles = !options.detectOnly && options.processBundles !== false;\n if (options.processBundles === false && options.processBundlesDir?.trim()) {\n throw new CliError('--process-bundles-dir cannot be used with --no-process-bundles.', {\n code: 'DATASET_IMPORT_LCA_PROCESS_BUNDLES_INVALID',\n exitCode: 2,\n });\n }\n const reportPath = path.resolve(\n options.reportPath ?? path.join(outputDir, 'conversion-report.json'),\n );\n const commandArgs = [\n '-m',\n 'tidas_tools.import_lca.cli',\n '--input',\n inputPath,\n '--output-dir',\n outputDir,\n '--from-format',\n fromFormat,\n '--target',\n target,\n '--report',\n reportPath,\n '--language',\n options.language?.trim() || 'en',\n '--validation-jobs',\n String(options.validationJobs ?? 1),\n ];\n if (options.mappingDir) {\n commandArgs.push('--mapping-dir', path.resolve(options.mappingDir));\n }\n if (options.failOnWarning) {\n commandArgs.push('--fail-on-warning');\n }\n if (!options.detectOnly && (processBundles || options.processBundlesDir?.trim())) {\n commandArgs.push('--process-bundles');\n }\n if (!options.detectOnly && options.processBundlesDir?.trim()) {\n commandArgs.push('--process-bundles-dir', processBundlesDir);\n }\n if (options.detectOnly) {\n commandArgs.push('--detect-only');\n }\n\n const run = (options.spawnImpl ?? spawnSync)(pythonBin, commandArgs, {\n cwd: tidasToolsRoot,\n env: {\n ...(options.env ?? process.env),\n PYTHONPATH: buildPythonPath(tidasToolsRoot, options.env ?? process.env),\n },\n encoding: 'utf8',\n }) as SpawnSyncReturns<string>;\n const conversionReport = readOptionalJson(reportPath);\n const files = {\n report: path.join(outputDir, 'outputs', 'import-lca-report.json'),\n conversion_report: reportPath,\n tidas_dir: options.detectOnly ? null : path.join(outputDir, 'tidas'),\n ilcd_dir:\n !options.detectOnly && (target === 'ilcd' || target === 'both')\n ? path.join(outputDir, 'ilcd')\n : null,\n mapping_csv: options.detectOnly ? null : path.join(outputDir, 'mapping.csv'),\n process_bundles_dir: processBundles ? processBundlesDir : null,\n process_bundles_index: processBundles ? path.join(processBundlesDir, 'index.json') : null,\n };\n const report: DatasetImportLcaReport = {\n schema_version: 1,\n status: run.status === 0 ? 'completed' : 'blocked',\n generated_at_utc: (options.now ?? new Date()).toISOString(),\n input_path: inputPath,\n output_dir: outputDir,\n from_format: fromFormat,\n target,\n detect_only: Boolean(options.detectOnly),\n command: {\n executable: pythonBin,\n args: commandArgs,\n cwd: tidasToolsRoot,\n exit_code: run.status,\n stdout: run.stdout ?? '',\n stderr: run.stderr ?? '',\n },\n conversion_report: conversionReport,\n files,\n };\n\n writeJsonArtifact(files.report, report);\n return report;\n}\n\nfunction requireInputPath(value: string): string {\n if (!value?.trim()) {\n throw new CliError('Missing required --input value.', {\n code: 'DATASET_IMPORT_LCA_INPUT_REQUIRED',\n exitCode: 2,\n });\n }\n const resolved = path.resolve(value);\n if (!existsSync(resolved)) {\n throw new CliError(`Input path not found: ${resolved}`, {\n code: 'DATASET_IMPORT_LCA_INPUT_NOT_FOUND',\n exitCode: 2,\n });\n }\n return resolved;\n}\n\nfunction requireOutputDir(value: string): string {\n if (!value?.trim()) {\n throw new CliError('Missing required --output-dir value.', {\n code: 'DATASET_IMPORT_LCA_OUTPUT_DIR_REQUIRED',\n exitCode: 2,\n });\n }\n return path.resolve(value);\n}\n\nfunction normalizeTarget(value: string | undefined): DatasetImportLcaTarget {\n const target = value?.trim() || 'tidas';\n if (target === 'tidas' || target === 'ilcd' || target === 'both') {\n return target;\n }\n throw new CliError(\"--target must be 'tidas', 'ilcd', or 'both'.\", {\n code: 'DATASET_IMPORT_LCA_TARGET_INVALID',\n exitCode: 2,\n });\n}\n\nfunction resolveTidasToolsRoot(\n explicitValue: string | undefined,\n env: NodeJS.ProcessEnv,\n defaultCandidate?: string,\n): string {\n const cliRepoRoot = resolveCliRepoRoot();\n if (explicitValue?.trim()) {\n const resolved = path.resolve(explicitValue);\n if (existsSync(path.join(resolved, 'src/tidas_tools/import_lca/cli.py'))) {\n return resolved;\n }\n throw new CliError('Could not resolve a tidas-tools checkout for dataset import.', {\n code: 'DATASET_IMPORT_LCA_TIDAS_TOOLS_NOT_FOUND',\n exitCode: 2,\n details: { candidates: [explicitValue] },\n });\n }\n\n const candidates = [\n env.TIDAS_TOOLS_DIR,\n env.TIDAS_TOOLS_PATH,\n defaultCandidate ?? path.resolve(cliRepoRoot, '../tidas-tools'),\n ].filter((candidate): candidate is string => Boolean(candidate?.trim()));\n\n for (const candidate of candidates) {\n const resolved = path.resolve(candidate);\n if (existsSync(path.join(resolved, 'src/tidas_tools/import_lca/cli.py'))) {\n return resolved;\n }\n }\n\n throw new CliError('Could not resolve a tidas-tools checkout for dataset import.', {\n code: 'DATASET_IMPORT_LCA_TIDAS_TOOLS_NOT_FOUND',\n exitCode: 2,\n details: { candidates },\n });\n}\n\nfunction resolveCliRepoRoot(candidatesOverride?: string[]): string {\n const moduleDir = path.dirname(fileURLToPath(import.meta.url));\n const candidates = candidatesOverride ?? [\n path.resolve(moduleDir, '../..'),\n path.resolve(moduleDir, '../../..'),\n ];\n return (\n candidates.find(\n (candidate) =>\n existsSync(path.join(candidate, 'package.json')) &&\n existsSync(path.join(candidate, 'src/cli.ts')),\n ) ?? candidates[0]\n );\n}\n\nfunction buildPythonPath(tidasToolsRoot: string, env: NodeJS.ProcessEnv): string {\n return [path.join(tidasToolsRoot, 'src'), env.PYTHONPATH].filter(Boolean).join(path.delimiter);\n}\n\nfunction readOptionalJson(filePath: string): unknown | null {\n if (!existsSync(filePath)) {\n return null;\n }\n return JSON.parse(readFileSync(filePath, 'utf8'));\n}\n\nexport const __testInternals = {\n normalizeTarget,\n resolveCliRepoRoot,\n resolveTidasToolsRoot,\n};\n"]}
|
|
1
|
+
{"version":3,"file":"dataset-import-lca.js","sourceRoot":"","sources":["../../../src/lib/dataset-import-lca.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,SAAS,EAAyB,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAqDvC,MAAM,UAAU,0BAA0B,CACxC,OAA0C;IAE1C,MAAM,SAAS,GAAG,gBAAgB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACtD,MAAM,SAAS,GAAG,gBAAgB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,MAAM,CAAC;IACxD,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,SAAS,CAAC;IACzD,MAAM,cAAc,GAAG,qBAAqB,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC;IAChG,MAAM,iBAAiB,GAAG,OAAO,CAAC,iBAAiB;QACjD,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC;QACzC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC;IAC5C,IAAI,OAAO,CAAC,cAAc,KAAK,KAAK,IAAI,O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AC,SAAS,EAAE,EAAE,CACZ,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;QAChD,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CACjD,IAAI,UAAU,CAAC,CAAC,CAAC,CACnB,CAAC;AACJ,CAAC;AAED,SAAS,eAAe,CAAC,cAAsB,EAAE,GAAsB;IACrE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,KAAK,CAAC,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AACjG,CAAC;AAED,SAAS,iBAAiB,CAAC,UAAoB;IAC7C,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,IAAI,IAAI,CAAC;AACvE,CAAC;AAED,SAAS,gBAAgB,CAAC,QAAgB;IACxC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AACpD,CAAC;AAED,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,eAAe;IACf,kBAAkB;IAClB,qBAAqB;CACtB,CAAC","sourcesContent":["import { existsSync, readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { spawnSync, type SpawnSyncReturns } from 'node:child_process';\nimport { writeJsonArtifact } from './artifacts.js';\nimport { CliError } from './errors.js';\n\nexport type DatasetImportLcaTarget = 'tidas' | 'ilcd' | 'both';\n\nexport type DatasetImportLcaReport = {\n schema_version: 1;\n status: 'completed' | 'blocked';\n generated_at_utc: string;\n input_path: string;\n output_dir: string;\n from_format: string;\n target: DatasetImportLcaTarget;\n detect_only: boolean;\n command: {\n executable: string;\n args: string[];\n cwd: string;\n exit_code: number | null;\n stdout: string;\n stderr: string;\n };\n conversion_report: unknown | null;\n files: {\n report: string;\n conversion_report: string;\n tidas_dir: string | null;\n ilcd_dir: string | null;\n mapping_csv: string | null;\n process_bundles_dir: string | null;\n process_bundles_index: string | null;\n };\n};\n\nexport type RunDatasetImportLcaConvertOptions = {\n inputPath: string;\n outputDir: string;\n fromFormat?: string | undefined;\n target?: string | undefined;\n language?: string | undefined;\n reportPath?: string | undefined;\n mappingDir?: string | undefined;\n failOnWarning?: boolean | undefined;\n validationJobs?: number | undefined;\n processBundles?: boolean | undefined;\n processBundlesDir?: string | undefined;\n detectOnly?: boolean | undefined;\n pythonBin?: string | undefined;\n tidasToolsDir?: string | undefined;\n env?: NodeJS.ProcessEnv | undefined;\n now?: Date | undefined;\n spawnImpl?: typeof spawnSync | undefined;\n};\n\nexport function runDatasetImportLcaConvert(\n options: RunDatasetImportLcaConvertOptions,\n): DatasetImportLcaReport {\n const inputPath = requireInputPath(options.inputPath);\n const outputDir = requireOutputDir(options.outputDir);\n const target = normalizeTarget(options.target);\n const fromFormat = options.fromFormat?.trim() || 'auto';\n const pythonBin = options.pythonBin?.trim() || 'python3';\n const tidasToolsRoot = resolveTidasToolsRoot(options.tidasToolsDir, options.env ?? process.env);\n const processBundlesDir = options.processBundlesDir\n ? path.resolve(options.processBundlesDir)\n : path.join(outputDir, 'process-bundles');\n if (options.processBundles === false && options.processBundlesDir?.trim()) {\n throw new CliError('--process-bundles-dir cannot be used with --no-process-bundles.', {\n code: 'DATASET_IMPORT_LCA_PROCESS_BUNDLES_INVALID',\n exitCode: 2,\n });\n }\n const reportPath = path.resolve(\n options.reportPath ?? path.join(outputDir, 'conversion-report.json'),\n );\n const commandArgs = [\n '-m',\n 'tidas_tools.import_lca.cli',\n '--input',\n inputPath,\n '--output-dir',\n outputDir,\n '--from-format',\n fromFormat,\n '--target',\n target,\n '--report',\n reportPath,\n '--language',\n options.language?.trim() || 'en',\n '--validation-jobs',\n String(options.validationJobs ?? 1),\n ];\n if (options.mappingDir) {\n commandArgs.push('--mapping-dir', path.resolve(options.mappingDir));\n }\n if (options.failOnWarning) {\n commandArgs.push('--fail-on-warning');\n }\n // tidas-tools >= 0.0.28 enables process bundles by default and only accepts\n // --no-process-bundles / --process-bundles-dir; a bare --process-bundles flag\n // gets prefix-matched by argparse to --process-bundles-dir and aborts.\n if (!options.detectOnly && options.processBundles === false) {\n commandArgs.push('--no-process-bundles');\n }\n if (!options.detectOnly && options.processBundlesDir?.trim()) {\n commandArgs.push('--process-bundles-dir', processBundlesDir);\n }\n if (options.detectOnly) {\n commandArgs.push('--detect-only');\n }\n\n const run = (options.spawnImpl ?? spawnSync)(pythonBin, commandArgs, {\n cwd: tidasToolsRoot,\n env: {\n ...(options.env ?? process.env),\n PYTHONPATH: buildPythonPath(tidasToolsRoot, options.env ?? process.env),\n },\n encoding: 'utf8',\n }) as SpawnSyncReturns<string>;\n const conversionReport = readOptionalJson(reportPath);\n // Mapping CSV is opt-in in tidas-tools >= 0.0.28 (mapping.csv.gz) and bundle\n // output depends on converter defaults, so report what is actually on disk.\n const mappingCsv = firstExistingPath([\n path.join(outputDir, 'mapping.csv.gz'),\n path.join(outputDir, 'mapping.csv'),\n ]);\n const processBundlesIndexPath = path.join(processBundlesDir, 'index.json');\n const files = {\n report: path.join(outputDir, 'outputs', 'import-lca-report.json'),\n conversion_report: reportPath,\n tidas_dir: options.detectOnly ? null : path.join(outputDir, 'tidas'),\n ilcd_dir:\n !options.detectOnly && (target === 'ilcd' || target === 'both')\n ? path.join(outputDir, 'ilcd')\n : null,\n mapping_csv: mappingCsv,\n process_bundles_dir: existsSync(processBundlesDir) ? processBundlesDir : null,\n process_bundles_index: existsSync(processBundlesIndexPath) ? processBundlesIndexPath : null,\n };\n const report: DatasetImportLcaReport = {\n schema_version: 1,\n status: run.status === 0 ? 'completed' : 'blocked',\n generated_at_utc: (options.now ?? new Date()).toISOString(),\n input_path: inputPath,\n output_dir: outputDir,\n from_format: fromFormat,\n target,\n detect_only: Boolean(options.detectOnly),\n command: {\n executable: pythonBin,\n args: commandArgs,\n cwd: tidasToolsRoot,\n exit_code: run.status,\n stdout: run.stdout ?? '',\n stderr: run.stderr ?? '',\n },\n conversion_report: conversionReport,\n files,\n };\n\n writeJsonArtifact(files.report, report);\n return report;\n}\n\nfunction requireInputPath(value: string): string {\n if (!value?.trim()) {\n throw new CliError('Missing required --input value.', {\n code: 'DATASET_IMPORT_LCA_INPUT_REQUIRED',\n exitCode: 2,\n });\n }\n const resolved = path.resolve(value);\n if (!existsSync(resolved)) {\n throw new CliError(`Input path not found: ${resolved}`, {\n code: 'DATASET_IMPORT_LCA_INPUT_NOT_FOUND',\n exitCode: 2,\n });\n }\n return resolved;\n}\n\nfunction requireOutputDir(value: string): string {\n if (!value?.trim()) {\n throw new CliError('Missing required --output-dir value.', {\n code: 'DATASET_IMPORT_LCA_OUTPUT_DIR_REQUIRED',\n exitCode: 2,\n });\n }\n return path.resolve(value);\n}\n\nfunction normalizeTarget(value: string | undefined): DatasetImportLcaTarget {\n const target = value?.trim() || 'tidas';\n if (target === 'tidas' || target === 'ilcd' || target === 'both') {\n return target;\n }\n throw new CliError(\"--target must be 'tidas', 'ilcd', or 'both'.\", {\n code: 'DATASET_IMPORT_LCA_TARGET_INVALID',\n exitCode: 2,\n });\n}\n\nfunction resolveTidasToolsRoot(\n explicitValue: string | undefined,\n env: NodeJS.ProcessEnv,\n defaultCandidate?: string,\n): string {\n const cliRepoRoot = resolveCliRepoRoot();\n if (explicitValue?.trim()) {\n const resolved = path.resolve(explicitValue);\n if (existsSync(path.join(resolved, 'src/tidas_tools/import_lca/cli.py'))) {\n return resolved;\n }\n throw new CliError('Could not resolve a tidas-tools checkout for dataset import.', {\n code: 'DATASET_IMPORT_LCA_TIDAS_TOOLS_NOT_FOUND',\n exitCode: 2,\n details: { candidates: [explicitValue] },\n });\n }\n\n const candidates = [\n env.TIDAS_TOOLS_DIR,\n env.TIDAS_TOOLS_PATH,\n defaultCandidate ?? path.resolve(cliRepoRoot, '../tidas-tools'),\n ].filter((candidate): candidate is string => Boolean(candidate?.trim()));\n\n for (const candidate of candidates) {\n const resolved = path.resolve(candidate);\n if (existsSync(path.join(resolved, 'src/tidas_tools/import_lca/cli.py'))) {\n return resolved;\n }\n }\n\n throw new CliError('Could not resolve a tidas-tools checkout for dataset import.', {\n code: 'DATASET_IMPORT_LCA_TIDAS_TOOLS_NOT_FOUND',\n exitCode: 2,\n details: { candidates },\n });\n}\n\nfunction resolveCliRepoRoot(candidatesOverride?: string[]): string {\n const moduleDir = path.dirname(fileURLToPath(import.meta.url));\n const candidates = candidatesOverride ?? [\n path.resolve(moduleDir, '../..'),\n path.resolve(moduleDir, '../../..'),\n ];\n return (\n candidates.find(\n (candidate) =>\n existsSync(path.join(candidate, 'package.json')) &&\n existsSync(path.join(candidate, 'src/cli.ts')),\n ) ?? candidates[0]\n );\n}\n\nfunction buildPythonPath(tidasToolsRoot: string, env: NodeJS.ProcessEnv): string {\n return [path.join(tidasToolsRoot, 'src'), env.PYTHONPATH].filter(Boolean).join(path.delimiter);\n}\n\nfunction firstExistingPath(candidates: string[]): string | null {\n return candidates.find((candidate) => existsSync(candidate)) ?? null;\n}\n\nfunction readOptionalJson(filePath: string): unknown | null {\n if (!existsSync(filePath)) {\n return null;\n }\n return JSON.parse(readFileSync(filePath, 'utf8'));\n}\n\nexport const __testInternals = {\n normalizeTarget,\n resolveCliRepoRoot,\n resolveTidasToolsRoot,\n};\n"]}
|