@o3r/core
Version:
Core of the Otter Framework
61 lines • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = require("node:fs");
const path = require("node:path");
const architect_1 = require("@angular-devkit/architect");
const globby_1 = require("globby");
const ts = require("typescript");
const utils_1 = require("../utils");
/** List of option dedicated to this build which should not be propagated to target build */
const libBuildOptions = ['target', 'skipJasmineFixtureWorkaround'];
exports.default = (0, architect_1.createBuilder)((0, utils_1.createBuilderWithMetricsIfInstalled)(async (options, context) => {
const specifiedRoot = context.target?.project && (await context.getProjectMetadata(context.target.project)).root;
const ROOT_PATH = specifiedRoot ? path.resolve(context.workspaceRoot, specifiedRoot) : context.currentDirectory;
const SRC_PATH = path.resolve(ROOT_PATH, 'src');
const SRC_PACKAGE_JSON = path.resolve(SRC_PATH, 'package.json');
const packageJsonFile = path.resolve(ROOT_PATH, 'package.json');
await node_fs_1.promises.copyFile(packageJsonFile, SRC_PACKAGE_JSON);
// Run ngPackagr build
const [project, target, configuration] = options.target.split(':');
const nextBuildTarget = { project, target, configuration };
const opts = Object.fromEntries(Object.entries(options)
.filter(([key]) => !libBuildOptions.includes(key))
.map(([key, value]) => [key, value]));
const build = await context.scheduleTarget(nextBuildTarget, opts);
const buildResult = await build.result;
/**
* Workaround to remove additional Jest references bring by ngPackagr into jest fixtures
*/
const patchJasmineFixtures = async () => {
const tsConfig = (await context.getTargetOptions(nextBuildTarget)).tsConfig || undefined;
const tsconfigPath = tsConfig && path.resolve(context.workspaceRoot, tsConfig);
const configFile = tsconfigPath && ts.readConfigFile(tsconfigPath, (p) => ts.sys.readFile(p));
const compilerOptions = configFile ? ts.parseJsonConfigFileContent(configFile.config, ts.sys, context.currentDirectory) : undefined;
const outDir = compilerOptions?.options?.outDir;
if (!outDir) {
context.logger.warn(`Tsconfig file (${tsconfigPath || 'unknown'}) not read, the jasmine fixtures will not be patched`);
return;
}
return Promise.all((0, globby_1.sync)(path.posix.join(outDir, '**', '*.jasmine.d.ts'), { cwd: context.currentDirectory })
.map((file) => path.resolve(context.currentDirectory, file))
.map(async (file) => {
context.logger.debug(`Removing Jest reference from ${file}`);
const content = (await node_fs_1.promises.readFile(file, { encoding: 'utf8' })).replace(/^\/{3} <reference types="jest" \/>$/m, '');
return node_fs_1.promises.writeFile(file, content);
}));
};
/** call in the end of the build process */
const tearDown = async () => {
await node_fs_1.promises.unlink(SRC_PACKAGE_JSON);
if (!options.skipJasmineFixtureWorkaround) {
await patchJasmineFixtures();
}
};
return options.watch
? new Promise((resolve) => process.once('SIGINT', async () => {
await tearDown();
resolve(buildResult);
}))
: tearDown().then(() => buildResult);
}));
//# sourceMappingURL=index.js.map