Skip to content

Commit

Permalink
feat(translations): Updated plugin to CreateNodesV2
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed Jun 21, 2024
1 parent e5ffbf5 commit 1054268
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 72 deletions.
2 changes: 1 addition & 1 deletion packages/translations/plugin.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { createNodes, createDependencies, TranslationPluginOptions } from './src/plugins/plugin'
export * from './src/plugins/plugin'
107 changes: 37 additions & 70 deletions packages/translations/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<string, TargetConfiguration>
> = {}

function readTargetsCache(): Record<
string,
Record<string, TargetConfiguration>
> {
return readJsonFile(cachePath)
}

function writeTargetsToCache(
targets: Record<string, Record<string, TargetConfiguration>>
) {
writeJsonFile(cachePath, targets)
}

export const createDependencies: CreateDependencies = () => {
writeTargetsToCache(calculatedTargets)

return []
}

export const createNodes: CreateNodes<TranslationPluginOptions> = [
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<CreateNodesResultV2> => {
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,
Expand Down
2 changes: 1 addition & 1 deletion packages/translations/src/utils/config-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const getConfigFileInRoot = <Config extends BaseConfigFile>(
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))
Expand Down

0 comments on commit 1054268

Please sign in to comment.