UNPKG

@o3r/core

Version:
61 lines 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs = require("node:fs"); const path = require("node:path"); const main_1 = require("@angular/compiler-cli/src/main"); const file_system_1 = require("@angular/compiler-cli/src/ngtsc/file_system"); const architect_1 = require("@angular-devkit/architect"); const chokidar = require("chokidar"); const ts = require("typescript"); const utils_1 = require("../utils"); tslib_1.__exportStar(require("./schema"), exports); /** Maximum number of steps */ const STEP_NUMBER = 2; exports.default = (0, architect_1.createBuilder)((0, utils_1.createBuilderWithMetricsIfInstalled)(async (options, context) => { context.reportRunning(); context.reportProgress(0, STEP_NUMBER, 'Run ngc.'); const args = ['--project', options.tsConfig]; if (options.watch) { args.push('--watch'); } (0, file_system_1.setFileSystem)(new file_system_1.NodeJSFileSystem()); const buildResultCode = (0, main_1.main)(args); context.reportProgress(1, STEP_NUMBER, 'Handle package.json.'); const handlePackageJson = (distPath) => { const packageJsonFile = path.resolve(context.currentDirectory, 'package.json'); const packageJsonString = fs.readFileSync(packageJsonFile, { encoding: 'utf8' }); const packageJson = JSON.parse(packageJsonString); if (packageJson.otterBuilder && packageJson.otterBuilder.entryPoint && (typeof packageJson.otterBuilder.entryPoint === 'string')) { const barrelName = path.parse(packageJson.otterBuilder.entryPoint).name; packageJson.typings = barrelName + '.d.ts'; packageJson.main = barrelName + '.js'; packageJson.module = barrelName + '.js'; fs.writeFileSync(path.resolve(distPath, 'package.json'), JSON.stringify(packageJson, null, 2)); } else { context.logger.error(`unable to find otterBuilder.entryPoint in ${packageJsonFile}.`); } }; const tsConfigFile = path.resolve(context.currentDirectory, options.tsConfig); try { const tsConfig = ts.readConfigFile(tsConfigFile, (pathFile) => fs.readFileSync(pathFile, { encoding: 'utf8' })).config; const dist = tsConfig.compilerOptions.outDir; handlePackageJson(dist); if (options.watch) { chokidar.watch(path.resolve(context.currentDirectory, 'package.json')).on('change', () => handlePackageJson(dist)); } if (buildResultCode > 0) { context.logger.error(`ngc build failed with code ${buildResultCode}.`); return options.watch ? new Promise((resolve) => process.once('SIGINT', () => resolve({ success: false }))) : { success: false }; } return options.watch ? new Promise((resolve) => process.once('SIGINT', () => resolve({ success: true }))) : { success: true }; } catch (e) { context.logger.error(e); return { success: false }; } })); //# sourceMappingURL=index.js.map