UNPKG

@nx/expo

Version:

The Expo Plugin for Nx contains executors and generators for managing and developing an expo application within your workspace. For example, you can directly build for different target platforms as well as generate projects and publish your code.

51 lines (50 loc) 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.expoComponentGenerator = expoComponentGenerator; const ts = require("typescript"); const devkit_1 = require("@nx/devkit"); const normalize_options_1 = require("./lib/normalize-options"); const add_import_1 = require("./lib/add-import"); const path_1 = require("path"); const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup"); async function expoComponentGenerator(host, schema) { const options = await (0, normalize_options_1.normalizeOptions)(host, schema); createComponentFiles(host, options); addExportsToBarrel(host, options); if (options.skipFormat) { await (0, devkit_1.formatFiles)(host); } } function createComponentFiles(host, options) { (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, './files', options.fileExtensionType), options.directory, { ...options, ext: options.fileExtension, }); if (options.skipTests) { host.delete((0, devkit_1.joinPathFragments)(options.directory, `${options.fileName}.spec.${options.fileExtension}`)); } } function addExportsToBarrel(host, options) { const workspace = (0, devkit_1.getProjects)(host); const proj = workspace.get(options.projectName); const isApp = (0, ts_solution_setup_1.getProjectType)(host, proj.root, proj.projectType) === 'application'; if (options.export && !isApp) { const indexFilePath = (0, devkit_1.joinPathFragments)(...(options.projectSourceRoot ? [options.projectSourceRoot] : [options.projectRoot, 'src']), options.fileExtensionType === 'js' ? 'index.js' : 'index.ts'); if (!host.exists(indexFilePath)) { return; } const indexSource = host.read(indexFilePath, 'utf-8'); const indexSourceFile = ts.createSourceFile(indexFilePath, indexSource, ts.ScriptTarget.Latest, true); const relativePathFromIndex = getRelativeImportToFile(indexFilePath, options.filePath); const changes = (0, devkit_1.applyChangesToString)(indexSource, (0, add_import_1.addImport)(indexSourceFile, `export * from '${relativePathFromIndex}';`)); host.write(indexFilePath, changes); } } function getRelativeImportToFile(indexPath, filePath) { const { name, dir } = (0, path_1.parse)(filePath); const relativeDirToTarget = (0, path_1.relative)((0, path_1.dirname)(indexPath), dir); return `./${(0, devkit_1.joinPathFragments)(relativeDirToTarget, name)}`; } exports.default = expoComponentGenerator;