@nx/cypress
Version:
90 lines (89 loc) • 4.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addBaseCypressSetup = addBaseCypressSetup;
const devkit_1 = require("@nx/devkit");
const js_1 = require("@nx/js");
const internal_1 = require("@nx/js/internal");
const path_1 = require("path");
function addBaseCypressSetup(tree, options) {
const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, options.project);
if (tree.exists((0, devkit_1.joinPathFragments)(projectConfig.root, 'cypress.config.ts')) ||
tree.exists((0, devkit_1.joinPathFragments)(projectConfig.root, 'cypress.config.js'))) {
return;
}
const opts = normalizeOptions(tree, projectConfig, options);
const isUsingTsSolutionConfig = (0, internal_1.isUsingTsSolutionSetup)(tree);
const templateVars = {
...opts,
jsx: !!opts.jsx,
offsetFromRoot: (0, devkit_1.offsetFromRoot)(projectConfig.root),
offsetFromProjectRoot: opts.hasTsConfig ? opts.offsetFromProjectRoot : '',
tsConfigPath:
// TS solution setup should always extend from tsconfig.base.json to use shared compilerOptions, the project's tsconfig.json will not have compilerOptions.
isUsingTsSolutionConfig
? (0, js_1.getRelativePathToRootTsConfig)(tree, opts.hasTsConfig
? (0, devkit_1.joinPathFragments)(projectConfig.root, options.directory)
: // If an existing tsconfig.json file does not exist, then cypress tsconfig will be moved to the project root.
projectConfig.root)
: opts.hasTsConfig
? `${opts.offsetFromProjectRoot}tsconfig.json`
: (0, js_1.getRelativePathToRootTsConfig)(tree, projectConfig.root),
linter: isEslintInstalled(tree) ? 'eslint' : 'none',
ext: '',
moduleResolution: (0, internal_1.getTsConfigModuleResolution)(tree),
// The config-*-* templates use `import.meta.url` (ESM) / `__filename`
// (CJS) - the shape base-setup already selects below - so the e2e preset is
// rendered correctly for the module system without any AST parsing.
e2ePreset: !!options.e2ePreset,
presetOptions: options.e2ePreset?.presetOptions ?? '',
baseUrl: options.e2ePreset?.baseUrl ?? '',
};
(0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, 'files/common'), projectConfig.root, templateVars);
(0, devkit_1.generateFiles)(tree, isUsingTsSolutionConfig
? (0, path_1.join)(__dirname, 'files/tsconfig/ts-solution')
: (0, path_1.join)(__dirname, 'files/tsconfig/non-ts-solution'), projectConfig.root, templateVars);
const isEsm = (0, internal_1.isEsmProject)(tree, projectConfig.root);
if (options.js) {
(0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, isEsm ? 'files/config-js-esm' : 'files/config-js-cjs'), projectConfig.root, templateVars);
}
else {
(0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, isEsm ? 'files/config-ts-esm' : 'files/config-ts-cjs'), projectConfig.root, templateVars);
}
if (opts.hasTsConfig) {
(0, devkit_1.updateJson)(tree, (0, devkit_1.joinPathFragments)(projectConfig.root, 'tsconfig.json'), (json) => {
// tsconfig doesn't have references so it shouldn't add them
// like in the case of nextjs apps.
if (!json.references) {
return json;
}
const tsconfigPath = `./${options.directory}/tsconfig.json`;
if (!json.references.some((ref) => ref.path === tsconfigPath)) {
json.references.push({
path: tsconfigPath,
});
}
return json;
});
}
else {
tree.rename((0, devkit_1.joinPathFragments)(projectConfig.root, options.directory, 'tsconfig.json'), (0, devkit_1.joinPathFragments)(projectConfig.root, 'tsconfig.json'));
}
}
function normalizeOptions(tree, projectConfig, options) {
options.directory ??= 'cypress';
const offsetFromProjectRoot = options.directory
.split('/')
.map((_) => '..')
.join('/');
const hasTsConfig = tree.exists((0, devkit_1.joinPathFragments)(projectConfig.root, 'tsconfig.json'));
return {
...options,
projectConfig,
offsetFromProjectRoot: `${offsetFromProjectRoot}/`,
hasTsConfig,
};
}
function isEslintInstalled(tree) {
const { dependencies, devDependencies } = (0, devkit_1.readJson)(tree, 'package.json');
return !!(dependencies?.eslint || devDependencies?.eslint);
}