import { Path, resolve } from '@angular-devkit/core'; import { Tree } from '@angular-devkit/schematics'; import { isNil } from '@ibm-wch-sdk/utils'; import { readFileSync } from 'fs'; import { join } from 'path'; const PACKAGE_JSON = '/package.json' as Path; const FALLBACK = '/data' as Path; const OPTIONS = '.wchtoolsoptions.json' as Path; const SDK_IMPORT = '@ibm-wch-sdk/ng'; const CLI_IMPORT = '@ibm-wch-sdk/cli'; function _findBuildVersion(): string { // read the file const pkg = JSON.parse( readFileSync(join(__dirname, '..', '..', 'package.json'), { encoding: 'utf-8' }) ); return pkg.version; } export function findSdkVersion(host: Tree): string { // try to locate the package json const buf = host.read(PACKAGE_JSON); if (isNil(buf)) { return _findBuildVersion(); } const pkg = JSON.parse(buf.toString()); // check if we have imports const deps = pkg.dependencies || {}; const devDeps = pkg.devDependencies || {}; return deps[SDK_IMPORT] || devDeps[CLI_IMPORT] || _findBuildVersion(); } export function findDataDir(host: Tree): Path { const buf = host.read(PACKAGE_JSON); if (isNil(buf)) { return FALLBACK; } const pkg = JSON.parse(buf.toString()); const cfg = pkg.config || {}; const data = cfg.data || FALLBACK; return resolve('/' as Path, data); } export function findWchToolsOptions(host: Tree): Path { return resolve(findDataDir(host), OPTIONS); }