@nx/detox
Version:
93 lines (92 loc) • 3.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNodesV2 = exports.createNodes = void 0;
const devkit_1 = require("@nx/devkit");
const path_1 = require("path");
const js_1 = require("@nx/js");
const get_named_inputs_1 = require("@nx/devkit/src/utils/get-named-inputs");
const fs_1 = require("fs");
const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
const cache_directory_1 = require("nx/src/utils/cache-directory");
const devkit_internals_1 = require("nx/src/devkit-internals");
const util_1 = require("@nx/js/src/plugins/typescript/util");
const pmc = (0, devkit_1.getPackageManagerCommand)();
function readTargetsCache(cachePath) {
return (0, fs_1.existsSync)(cachePath) ? (0, devkit_1.readJsonFile)(cachePath) : {};
}
function writeTargetsToCache(cachePath, targetsCache) {
const oldCache = readTargetsCache(cachePath);
(0, devkit_1.writeJsonFile)(cachePath, {
...oldCache,
targetsCache,
});
}
exports.createNodes = [
'**/{detox.config,.detoxrc}.{json,js}',
async (configFiles, options, context) => {
const optionsHash = (0, devkit_internals_1.hashObject)(options);
const cachePath = (0, path_1.join)(cache_directory_1.workspaceDataDirectory, `expo-${optionsHash}.hash`);
const targetsCache = readTargetsCache(cachePath);
try {
return await (0, devkit_1.createNodesFromFiles)((configFile, options, context) => createNodesInternal(configFile, options, context, targetsCache), configFiles, options, context);
}
finally {
writeTargetsToCache(cachePath, targetsCache);
}
},
];
exports.createNodesV2 = exports.createNodes;
async function createNodesInternal(configFile, options, context, targetsCache) {
options = normalizeOptions(options);
const projectRoot = (0, path_1.dirname)(configFile);
const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, options, context, [(0, js_1.getLockFileName)((0, devkit_1.detectPackageManager)(context.workspaceRoot))]);
targetsCache[hash] ??= buildDetoxTargets(projectRoot, options, context);
return {
projects: {
[projectRoot]: {
targets: targetsCache[hash],
},
},
};
}
function buildDetoxTargets(projectRoot, options, context) {
const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context);
const targets = {
[options.buildTargetName]: {
command: `detox build`,
options: { cwd: projectRoot },
cache: true,
inputs: getInputs(namedInputs),
},
[options.startTargetName]: {
command: `detox start`,
continuous: true,
options: { cwd: projectRoot },
},
[options.testTargetName]: {
command: `detox test`,
options: { cwd: projectRoot },
cache: true,
inputs: getInputs(namedInputs),
},
};
(0, util_1.addBuildAndWatchDepsTargets)(context.workspaceRoot, projectRoot, targets, options, pmc);
return targets;
}
function getInputs(namedInputs) {
return [
...('production' in namedInputs
? ['default', '^production']
: ['default', '^default']),
{
externalDependencies: ['detox'],
},
];
}
function normalizeOptions(options) {
options ??= {};
options.buildTargetName ??= 'build';
options.startTargetName ??= 'start';
options.testTargetName ??= 'test';
return options;
}