Skip to content

Commit

Permalink
Improvements (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs authored Jul 4, 2024
2 parents 417d79b + dc79b44 commit f1d3ed5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 74 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'
105 changes: 36 additions & 69 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 {}
}
async (configFiles, options: TranslationPluginOptions, context): Promise<CreateNodesResultV2> => {

const config = getConfigFileInRoot(projectRoot)
options = normalizeOptions(options)
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
7 changes: 4 additions & 3 deletions packages/vercel/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ export function buildExecutor(
settings: {}
})

const vercelCommand = 'npx [email protected]'
const vercelEnvironment = context.configurationName === 'production' ? 'production' : 'preview'

// Pull latest
const { success: pullSuccess } = execCommand(buildCommand([
'npx vercel pull --yes',
`${vercelCommand} pull --yes`,
`--environment=${vercelEnvironment}`,
vercelToken && `--token=${vercelToken}`,

Expand Down Expand Up @@ -110,12 +111,12 @@ export function buildExecutor(
outputDirectory: getOutputDirectory(framework, outputDirectory),
rootDirectory: null,
directoryListing: false,
nodeVersion: options.nodeVersion || '18.x'
nodeVersion: options.nodeVersion || '20.x'
}
})

const { success } = execCommand(buildCommand([
'npx vercel build',
`${vercelCommand} build`,
`--output ${outputDirectory}/.vercel/output`,
context.configurationName === 'production' && '--prod',
options.config && `--local-config=${join(workspaceRoot, options.config)}`,
Expand Down

0 comments on commit f1d3ed5

Please sign in to comment.