UNPKG

@nx/docker

Version:

The Nx Plugin for Docker to aid in containerizing projects.

103 lines (102 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createNodesV2 = void 0; const devkit_1 = require("@nx/devkit"); const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes"); const file_hasher_1 = require("nx/src/hasher/file-hasher"); const cache_directory_1 = require("nx/src/utils/cache-directory"); const fs_1 = require("fs"); const path_1 = require("path"); const get_named_inputs_1 = require("@nx/devkit/src/utils/get-named-inputs"); function readTargetsCache(cachePath) { return (0, fs_1.existsSync)(cachePath) ? (0, devkit_1.readJsonFile)(cachePath) : {}; } function writeTargetsCache(cachePath, results) { (0, devkit_1.writeJsonFile)(cachePath, results ?? {}); } const dockerfileGlob = '**/Dockerfile'; exports.createNodesV2 = [ dockerfileGlob, async (configFilePaths, options, context) => { const optionsHash = (0, file_hasher_1.hashObject)(options); const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, `docker-${optionsHash}.hash`); const targetsCache = readTargetsCache(cachePath); try { return await (0, devkit_1.createNodesFromFiles)((configFile, options, context) => createNodesInternal(configFile, options, context, targetsCache), configFilePaths, options, context); } finally { writeTargetsCache(cachePath, targetsCache); } }, ]; async function createNodesInternal(configFilePath, options, context, targetsCache) { const projectRoot = (0, path_1.dirname)(configFilePath); const normalizedOptions = normalizePluginOptions(options); const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, normalizedOptions, context); targetsCache[hash] ??= await createDockerTargets(projectRoot, normalizedOptions, context); const { targets, metadata } = targetsCache[hash]; return { projects: { [projectRoot]: { root: projectRoot, targets, metadata, }, }, }; } async function createDockerTargets(projectRoot, options, context) { const imageRef = projectRoot.replace(/^[\\/]/, '').replace(/[\\/\s]+/g, '-'); const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context); const targets = {}; targets[options.buildTarget] = { command: `docker build .`, options: { cwd: projectRoot, args: [`--tag ${imageRef}`], }, inputs: [ ...('production' in namedInputs ? ['production', '^production'] : ['default', '^default']), ], metadata: { technologies: ['docker'], description: `Run Docker build`, help: { command: `docker build --help`, example: {}, }, }, }; targets[options.runTarget] = { dependsOn: [options.buildTarget], command: `docker run {args} ${imageRef}`, options: { cwd: projectRoot, }, inputs: [ ...('production' in namedInputs ? ['production', '^production'] : ['default', '^default']), ], metadata: { technologies: ['docker'], description: `Run Docker run`, help: { command: `docker run --help`, example: {}, }, }, }; targets['nx-release-publish'] = { executor: '@nx/docker:release-publish', }; return { targets, metadata: {} }; } function normalizePluginOptions(options) { return { buildTarget: options.buildTarget ?? 'docker:build', runTarget: options.runTarget ?? 'docker:run', }; }