Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expo): use createNodesV2 #28005

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/detox/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
createNodes,
createDependencies,
createNodesV2,
DetoxPluginOptions,
} from './src/plugins/plugin';
112 changes: 78 additions & 34 deletions packages/detox/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
CreateDependencies,
CreateNodes,
CreateNodesContext,
createNodesFromFiles,
CreateNodesResult,
CreateNodesV2,
detectPackageManager,
NxJsonConfiguration,
readJsonFile,
Expand All @@ -11,66 +13,108 @@ import {
import { dirname, join } from 'path';
import { getLockFileName } from '@nx/js';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync } from 'fs';
import { existsSync } from 'fs';
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { hashObject } from 'nx/src/devkit-internals';

export interface DetoxPluginOptions {
buildTargetName?: string;
startTargetName?: string;
testTargetName?: string;
}

const cachePath = join(workspaceDataDirectory, 'detox.hash');
const targetsCache = readTargetsCache();

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

function writeTargetsToCache() {
writeJsonFile(cachePath, targetsCache);
function writeTargetsToCache(
cachePath: string,
targetsCache: Record<
string,
Record<string, TargetConfiguration<DetoxPluginOptions>>
>
) {
const oldCache = readTargetsCache(cachePath);
writeJsonFile(cachePath, {
...oldCache,
targetsCache,
});
}

export const createDependencies: CreateDependencies = () => {
writeTargetsToCache();
return [];
};
export const createNodesV2: CreateNodesV2<DetoxPluginOptions> = [
'**/{detox.config,.detoxrc}.{json,js}',
async (configFiles, options, context) => {
const optionsHash = hashObject(options);
const cachePath = join(workspaceDataDirectory, `expo-${optionsHash}.hash`);
const targetsCache = readTargetsCache(cachePath);

try {
return await createNodesFromFiles(
(configFile, options, context) =>
createNodesInternal(configFile, options, context, targetsCache),
configFiles,
options,
context
);
} finally {
writeTargetsToCache(cachePath, targetsCache);
}
},
];

export const createNodes: CreateNodes<DetoxPluginOptions> = [
'**/{detox.config,.detoxrc}.{json,js}',
async (configFilePath, options, context) => {
options = normalizeOptions(options);
const projectRoot = dirname(configFilePath);
const optionsHash = hashObject(options);
const cachePath = join(workspaceDataDirectory, `detox-${optionsHash}.hash`);

// Do not create a project if project.json isn't there.
const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot));
if (!siblingFiles.includes('project.json')) {
return {};
}

const hash = await calculateHashForCreateNodes(
projectRoot,
const targetsCache = readTargetsCache(cachePath);
const result = await createNodesInternal(
configFilePath,
options,
context,
[getLockFileName(detectPackageManager(context.workspaceRoot))]
targetsCache
);

targetsCache[hash] ??= buildDetoxTargets(projectRoot, options, context);
writeTargetsToCache(cachePath, targetsCache);

return {
projects: {
[projectRoot]: {
targets: targetsCache[hash],
},
},
};
return result;
},
];

async function createNodesInternal(
configFile: string,
options: DetoxPluginOptions,
context: CreateNodesContext,
targetsCache: Record<
string,
Record<string, TargetConfiguration<DetoxPluginOptions>>
>
): Promise<CreateNodesResult> {
options = normalizeOptions(options);
const projectRoot = dirname(configFile);

const hash = await calculateHashForCreateNodes(
projectRoot,
options,
context,
[getLockFileName(detectPackageManager(context.workspaceRoot))]
);

targetsCache[hash] ??= buildDetoxTargets(projectRoot, options, context);

return {
projects: {
[projectRoot]: {
targets: targetsCache[hash],
},
},
};
}

function buildDetoxTargets(
projectRoot: string,
options: DetoxPluginOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/expo/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
createNodes,
createDependencies,
createNodesV2,
ExpoPluginOptions,
} from './plugins/plugin';
140 changes: 95 additions & 45 deletions packages/expo/plugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
CreateDependencies,
CreateNodes,
CreateNodesContext,
createNodesFromFiles,
CreateNodesResult,
CreateNodesV2,
detectPackageManager,
logger,
NxJsonConfiguration,
readJsonFile,
TargetConfiguration,
workspaceRoot,
writeJsonFile,
} from '@nx/devkit';
import { dirname, join } from 'path';
Expand All @@ -15,6 +17,8 @@ import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync } from 'fs';
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { combineGlobPatterns } from 'nx/src/utils/globs';
import { hashObject } from 'nx/src/devkit-internals';
import { loadConfigFile } from '@nx/devkit/src/utils/config-utils';

export interface ExpoPluginOptions {
Expand All @@ -29,68 +33,114 @@ export interface ExpoPluginOptions {
submitTargetName?: string;
}

const cachePath = join(workspaceDataDirectory, 'expo.hash');
const targetsCache = readTargetsCache();

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

function writeTargetsToCache() {
const oldCache = readTargetsCache();
function writeTargetsToCache(
cachePath: string,
targetsCache: Record<
string,
Record<string, TargetConfiguration<ExpoPluginOptions>>
>
) {
const oldCache = readTargetsCache(cachePath);
writeJsonFile(cachePath, {
...oldCache,
targetsCache,
});
}

export const createDependencies: CreateDependencies = () => {
writeTargetsToCache();
return [];
};
export const createNodesV2: CreateNodesV2<ExpoPluginOptions> = [
'**/app.{json,config.js,config.ts}',
async (configFiles, options, context) => {
const optionsHash = hashObject(options);
const cachePath = join(workspaceDataDirectory, `expo-${optionsHash}.hash`);
const targetsCache = readTargetsCache(cachePath);

try {
return await createNodesFromFiles(
(configFile, options, context) =>
createNodesInternal(configFile, options, context, targetsCache),
configFiles,
options,
context
);
} finally {
writeTargetsToCache(cachePath, targetsCache);
}
},
];

export const createNodes: CreateNodes<ExpoPluginOptions> = [
'**/app.{json,config.js,config.ts}',
async (configFilePath, options, context) => {
options = normalizeOptions(options);
const projectRoot = dirname(configFilePath);

// Do not create a project if package.json or project.json or metro.config.js isn't there.
const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot));
if (
!siblingFiles.includes('package.json') ||
!siblingFiles.includes('metro.config.js')
) {
return {};
}
const appConfig = await getAppConfig(configFilePath, context);
// if appConfig.expo is not defined
if (!appConfig.expo) {
return {};
}

const hash = await calculateHashForCreateNodes(
projectRoot,
options,
context,
[getLockFileName(detectPackageManager(context.workspaceRoot))]
logger.warn(
'`createNodes` is deprecated. Update your plugin to utilize createNodesV2 instead. In Nx 20, this will change to the createNodesV2 API.'
);

targetsCache[hash] ??= buildExpoTargets(projectRoot, options, context);
const optionsHash = hashObject(options);
const cachePath = join(workspaceDataDirectory, `expo-${optionsHash}.hash`);
const targetsCache = readTargetsCache(cachePath);

return {
projects: {
[projectRoot]: {
targets: targetsCache[hash],
},
},
};
return createNodesInternal(configFilePath, options, context, targetsCache);
},
];

async function createNodesInternal(
configFile: string,
options: ExpoPluginOptions,
context: CreateNodesContext,
targetsCache: Record<
string,
Record<string, TargetConfiguration<ExpoPluginOptions>>
>
): Promise<CreateNodesResult> {
options = normalizeOptions(options);
const projectRoot = dirname(configFile);

// Do not create a project if package.json or project.json or metro.config.js isn't there.
const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot));
if (
!siblingFiles.includes('package.json') ||
!siblingFiles.includes('metro.config.js')
) {
return {};
}

// Check if it's an Expo project
const packageJson = readJsonFile(
join(context.workspaceRoot, projectRoot, 'package.json')
);
const appConfig = await getAppConfig(configFile, context);
if (
!appConfig.expo &&
!packageJson.dependencies['expo'] &&
!packageJson.devDependencies['expo']
) {
return {};
}

const hash = await calculateHashForCreateNodes(
projectRoot,
options,
context,
[getLockFileName(detectPackageManager(context.workspaceRoot))]
);

targetsCache[hash] ??= buildExpoTargets(projectRoot, options, context);

return {
projects: {
[projectRoot]: {
targets: targetsCache[hash],
},
},
};
}

function buildExpoTargets(
projectRoot: string,
options: ExpoPluginOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
createNodes,
createDependencies,
createNodesV2,
ReactNativePluginOptions,
} from './plugins/plugin';
Loading