@routineless/nx-aws-cdk
Version:
Nx plugin for AWS CDK
149 lines • 6.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const js_1 = require("@nx/js");
const fs_extra_1 = require("fs-extra");
const path_1 = tslib_1.__importDefault(require("path"));
const dependencies_1 = require("./lib/dependencies");
const esbuild_helper_1 = require("./lib/esbuild-helper");
const defaultExternal = ['@aws-sdk/*'];
const normalizeOptions = (options, context) => {
const projectConfig = context.projectsConfigurations.projects[context.projectName];
// If we're not generating package.json file, then copy it as-is as an asset.
const assets = options.generatePackageJson
? options.assets
: [...options.assets, (0, devkit_1.joinPathFragments)(projectConfig.root, 'package.json')];
let userDefinedBuildOptions = {};
if (options.esbuildConfig) {
const userDefinedConfig = path_1.default.resolve(context.root, options.esbuildConfig);
if (options.esbuildOptions)
throw new Error(`Cannot use both esbuildOptions and esbuildConfig options.`);
if (!(0, fs_extra_1.existsSync)(userDefinedConfig))
throw new Error(`Path of esbuildConfig does not exist: ${userDefinedConfig}`);
userDefinedBuildOptions = require(userDefinedConfig);
}
else if (options.esbuildOptions) {
userDefinedBuildOptions = options.esbuildOptions;
}
const partialOptions = {
main: options.main ?? path_1.default.join(context.root, projectConfig.root, 'src', 'main.ts'),
platform: 'node',
target: 'esnext',
deleteOutputPath: !!options.deleteOutputPath,
metafile: !!options.metafile,
includeInternal: true,
thirdParty: !!options.thirdParty,
assets,
userDefinedBuildOptions,
external: options.external ? [...options.external] : [...defaultExternal],
};
if (options.additionalEntryPoints && options.additionalEntryPoints.length > 0) {
const { outputFileName, ...rest } = options;
if (outputFileName) {
throw new Error(`Cannot use outputFileName and additionalEntry points together.`);
}
return {
...rest,
...partialOptions,
singleEntry: false,
// Use the `main` file name as the output file name.
// NOTE: The .js default extension may be replaced later in getOutfile() call.
outputFileName: `${path_1.default.parse(partialOptions.main).name}.js`,
};
}
else {
return {
...options,
...partialOptions,
singleEntry: true,
outputFileName:
// NOTE: The .js default extension may be replaced later in getOutfile() call.
options.outputFileName ?? `${path_1.default.parse(partialOptions.main).name}.js`,
};
}
};
async function* awsLambdaRuntimeExecutor(_options, context) {
process.env['NODE_ENV'] ??= context.configurationName ?? 'production';
const projectGraph = context.projectGraph;
if (!projectGraph)
throw new Error('projectGraph is undefined');
const projectName = context.projectName;
if (!projectName)
throw new Error('projectName is undefined');
const projectConfig = context.projectsConfigurations?.projects?.[projectName];
if (!projectConfig)
throw new Error('can not resolve project configuration');
const projectNode = projectGraph.nodes[projectName];
if (!projectNode)
throw new Error('projectNode is undefined');
const options = normalizeOptions(_options, context);
if (options.deleteOutputPath)
(0, fs_extra_1.removeSync)(options.outputPath);
const assetsResult = await (0, js_1.copyAssets)(options, context);
if (!assetsResult.success) {
throw new Error('Failed to copy assets');
}
const { resolved, excluded } = (0, dependencies_1.resolveDependencies)(context, projectGraph, options);
options.external = [];
for (const excludedDep of excluded) {
options.external.push((0, dependencies_1.getPackageName)(excludedDep));
}
// Run type-checks first and bail if they don't pass.
if (!options.skipTypeCheck) {
const { errors } = await runTypeCheck(options, context);
if (errors && errors.length > 0) {
yield { success: false };
return;
}
}
const buildResult = await (0, esbuild_helper_1.build)(options, context, resolved);
if (options.generatePackageJson) {
await generatePackageJson(context, options, excluded);
}
yield {
success: buildResult.errors.length === 0,
};
}
exports.default = awsLambdaRuntimeExecutor;
const generatePackageJson = async (context, options, externalDependencies) => {
const cpjOptions = {
...options,
format: [options.format],
skipTypings: true,
generateLockfile: false,
outputFileExtensionForCjs: (0, esbuild_helper_1.getOutExtension)({ ...options, format: 'cjs' }),
updateBuildableProjectDepsInPackageJson: true,
};
// Any dependencies expect excluded external should already be bundeld or added to external node module
cpjOptions.overrideDependencies = externalDependencies;
const packageJsonResult = await (0, js_1.copyPackageJson)(cpjOptions, context);
const generatedPackageJson = (0, devkit_1.readJsonFile)(`${options.outputPath}/package.json`);
generatedPackageJson.dependencies = externalDependencies.reduce((acc, dep) => ({ ...acc, [(0, dependencies_1.getPackageName)(dep)]: (0, dependencies_1.getPackageVersion)(dep) }), {});
(0, devkit_1.writeJsonFile)(`${options.outputPath}/package.json`, generatedPackageJson);
if (!packageJsonResult.success) {
throw new Error('Failed to generate package.json');
}
};
const getTypeCheckOptions = (options, context) => {
const { tsConfig } = options;
const typeCheckOptions = {
// TODO(jack): Add support for d.ts declaration files -- once the `@nx/js:tsc` changes are in we can use the same logic.
mode: 'noEmit',
tsConfigPath: tsConfig,
// outDir: outputPath,
workspaceRoot: context.root,
rootDir: context.root,
};
return typeCheckOptions;
};
const runTypeCheck = async (options, context) => {
const { errors, warnings } = await (0, js_1.runTypeCheck)(getTypeCheckOptions(options, context));
const hasErrors = errors && errors.length > 0;
const hasWarnings = warnings && warnings.length > 0;
if (hasErrors || hasWarnings) {
await (0, js_1.printDiagnostics)(errors, warnings);
}
return { errors, warnings };
};
//# sourceMappingURL=executor.js.map