@nx/react-native
Version:
55 lines (54 loc) • 2.76 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.reactNativeComponentGenerator = reactNativeComponentGenerator;
const devkit_1 = require("@nx/devkit");
const normalize_options_1 = require("./lib/normalize-options");
const add_import_1 = require("./lib/add-import");
const ensure_typescript_1 = require("@nx/js/src/utils/typescript/ensure-typescript");
const path_1 = require("path");
const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
async function reactNativeComponentGenerator(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}`));
}
}
let tsModule;
function addExportsToBarrel(host, options) {
if (!tsModule) {
tsModule = (0, ensure_typescript_1.ensureTypescript)();
}
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 = tsModule.createSourceFile(indexFilePath, indexSource, tsModule.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 = reactNativeComponentGenerator;
;