From 10542684c00f569c03453fa63bf08c007934320b Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Fri, 21 Jun 2024 22:24:43 +0200 Subject: [PATCH] feat(translations): Updated plugin to `CreateNodesV2` --- packages/translations/plugin.ts | 2 +- packages/translations/src/plugins/plugin.ts | 107 ++++++------------ .../translations/src/utils/config-file.ts | 2 +- 3 files changed, 39 insertions(+), 72 deletions(-) diff --git a/packages/translations/plugin.ts b/packages/translations/plugin.ts index 4c5de8ee..67dde227 100644 --- a/packages/translations/plugin.ts +++ b/packages/translations/plugin.ts @@ -1 +1 @@ -export { createNodes, createDependencies, TranslationPluginOptions } from './src/plugins/plugin' +export * from './src/plugins/plugin' diff --git a/packages/translations/src/plugins/plugin.ts b/packages/translations/src/plugins/plugin.ts index c8da0fc2..9f0ceef2 100644 --- a/packages/translations/src/plugins/plugin.ts +++ b/packages/translations/src/plugins/plugin.ts @@ -1,19 +1,11 @@ -import { - CreateDependencies, - CreateNodes, - CreateNodesContext, - detectPackageManager, - readJsonFile, - TargetConfiguration, - writeJsonFile -} from '@nx/devkit' -import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes' +import { createNodesFromFiles } from '@nx/devkit' import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs' -import { existsSync, readdirSync } from 'fs' -import { getLockFileName } from 'nx/src/plugins/js/lock-file/lock-file' -import { workspaceDataDirectory } from 'nx/src/utils/cache-directory' +import { readdirSync } from 'fs' import { dirname, join } from 'path' +import type { CreateNodesResultV2, CreateNodesV2, TargetConfiguration } from '@nx/devkit' +import type { CreateNodesContext, CreateNodesResult } from '@nx/devkit' + import { BaseConfigFile, getConfigFileInRoot } from '../utils/config-file' export interface TranslationPluginOptions { @@ -23,71 +15,46 @@ export interface TranslationPluginOptions { translateTargetName?: string } -const cachePath = join(workspaceDataDirectory, 'nx-extend.translations.hash') -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {} - -const calculatedTargets: Record< - string, - Record -> = {} - -function readTargetsCache(): Record< - string, - Record -> { - return readJsonFile(cachePath) -} - -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets) -} - -export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets) - - return [] -} - -export const createNodes: CreateNodes = [ +export const createNodesV2: CreateNodesV2 = [ '**/.translationsrc.json', - async (configFilePath, options, context) => { - const projectRoot = dirname(configFilePath) - const fullyQualifiedProjectRoot = join(context.workspaceRoot, projectRoot) - - // Do not create a project if package.json and project.json isn't there - const siblingFiles = readdirSync(fullyQualifiedProjectRoot) - if (!siblingFiles.includes('project.json') || siblingFiles.includes('nx.json')) { - return {} - } - - const config = getConfigFileInRoot(projectRoot) - options = normalizeOptions(options) + async (configFiles, options: TranslationPluginOptions, context): Promise => { + console.log('context.workspaceRoot',context.workspaceRoot) + return createNodesFromFiles( + createTargets, + configFiles, + options, + context + ) + } +] - const hash = await calculateHashForCreateNodes(projectRoot, options, context, [ - getLockFileName(detectPackageManager(context.workspaceRoot)) - ]) +function createTargets(projectConfigurationFile: string, options: TranslationPluginOptions, context: CreateNodesContext): CreateNodesResult { + const projectRoot = dirname(projectConfigurationFile) + const fullyQualifiedProjectRoot = join(context.workspaceRoot, projectRoot) - const targets = targetsCache[hash] ?? buildTranslationTargets( - projectRoot, - context, - config, - options - ) + // Do not create a project if package.json and project.json isn't there + const siblingFiles = readdirSync(fullyQualifiedProjectRoot) + if (!siblingFiles.includes('project.json') || siblingFiles.includes('nx.json')) { + return {} + } - calculatedTargets[hash] = targets + const config = getConfigFileInRoot(projectRoot) + options = normalizeOptions(options) - return { - projects: { - [projectRoot]: { - root: projectRoot, - targets: targets - } + return { + projects: { + [projectRoot]: { + root: projectRoot, + targets: buildTranslationTargets( + projectRoot, + context, + config, + options + ) } } } -] +} function buildTranslationTargets( projectRoot: string, diff --git a/packages/translations/src/utils/config-file.ts b/packages/translations/src/utils/config-file.ts index 3c9d234f..5af5a73c 100644 --- a/packages/translations/src/utils/config-file.ts +++ b/packages/translations/src/utils/config-file.ts @@ -88,7 +88,7 @@ export const getConfigFileInRoot = ( return configFile as Config } -export const updateConfigFile = (context: ExecutorContext, update): void => { +export const updateConfigFile = (context: ExecutorContext, update: object): void => { const fileLocation = resolve(getProjectRoot(context), '.translationsrc.json') writeJsonFile(fileLocation, Object.assign(readJsonFile(fileLocation), update))