nitro-codegen
Version:
The code-generator for react-native-nitro-modules.
54 lines (53 loc) • 1.8 kB
JavaScript
import path from 'path';
import { getTypeAs } from './types/getTypeAs.js';
import { OptionalType } from './types/OptionalType.js';
export function createFileMetadataString(filename, comment = '///') {
const now = new Date();
return `
${comment}
${comment} ${filename}
${comment} This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
${comment} https://github.com/mrousavy/nitro
${comment} Copyright © ${now.getFullYear()} Marc Rousavy @ Margelo
${comment}
`.trim();
}
export function isFunction(type) {
switch (type.kind) {
case 'function':
return true;
case 'optional': {
const optional = getTypeAs(type, OptionalType);
return isFunction(optional.wrappingType);
}
default:
return false;
}
}
export function toReferenceType(type) {
return `const ${type}&`;
}
export function escapeCppName(string) {
// Replace non-alphanumeric characters with underscores
let escapedStr = string.replace(/[^a-zA-Z0-9_]/g, '_');
// Ensure the first character is a letter or underscore
if (!/^[a-zA-Z_]/.test(escapedStr)) {
escapedStr = '_' + escapedStr;
}
return escapedStr;
}
export function isBooleanPropertyPrefix(name) {
return name.startsWith('is') || name.startsWith('has');
}
export function isNotDuplicate(item, index, array) {
return array.indexOf(item) === index;
}
export function isCppFile(file) {
return file.name.endsWith('cpp') || file.name.endsWith('c');
}
export function getRelativeDirectory(file) {
return path.join('..', 'nitrogen', 'generated', file.platform, file.language, ...file.subdirectory, file.name);
}
export function getRelativeDirectoryGenerated(...subpath) {
return path.join('..', 'nitrogen', 'generated', ...subpath);
}