Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a .gitattributes file into nitrogen/generated #473

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/docs/configuration-nitro-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"androidCxxLibName": "$$androidCxxLibName$$"
},
"autolinking": {},
"ignorePaths": ["**/node_modules"]
"ignorePaths": ["**/node_modules"],
"createGitAttributes": true
}
```

Expand Down Expand Up @@ -176,3 +177,7 @@ Here, the Hybrid Object "`Math`" is autolinked to create an instance of `HybridM
Configures the TypeScript parser to ignore specific given paths when looking for `*.nitro.ts` specs.

By default, this is empty (`[]`), but it can be set to ignore paths like `["node_modules", "lib"]`.

## `createGitAttributes`

Configures whether a `.gitattributes` file will be generated in the `nitrogen/generated/` directory to mark files as [`linguist-generated`](https://docs.github.com/en/repositories/working-with-files/managing-files/customizing-how-changed-files-appear-on-github) for GitHub.
5 changes: 5 additions & 0 deletions docs/static/nitro.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
"type": "string"
},
"description": "A list of paths relative to the project directory that should be ignored by nitrogen. Nitrogen will not look for `.nitro.ts` files in these directories."
},
"createGitAttributes": {
"type": "boolean",
"default": true,
"description": "Configures whether a `.gitattributes` file will be generated in the `nitrogen/generated/` directory to mark files as `linguist-generated` for GitHub."
}
}
}
16 changes: 10 additions & 6 deletions packages/nitrogen/src/config/NitroConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ export const NitroConfig = {
return [...ANDROID_BASE_NAMESPACE, ...userPackage, ...subPackage]
},

/**
* Get the autolinking configuration of all HybridObjects.
* Those will be generated and default-constructed.
*/
getAutolinkedHybridObjects(): NitroUserConfig['autolinking'] {
return getUserConfig().autolinking
},

/**
* Get the paths that will be ignored when loading the TypeScript project.
* In most cases, this just contains `node_modules/`.
Expand All @@ -110,11 +118,7 @@ export const NitroConfig = {
return getUserConfig().ignorePaths ?? []
},

/**
* Get the autolinking configuration of all HybridObjects.
* Those will be generated and default-constructed.
*/
getAutolinkedHybridObjects(): NitroUserConfig['autolinking'] {
return getUserConfig().autolinking
getCreateGitAttributes(): boolean {
return getUserConfig().createGitAttributes ?? false
},
}
27 changes: 5 additions & 22 deletions packages/nitrogen/src/config/NitroUserConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { z } from 'zod'
import { capitalizeName } from '../utils.js'
import path from 'path'
import { promises as fs } from 'fs'

// Namespaces and package names in C++/Java will be matched with a regex.
const safeNamePattern = /^[a-zA-Z_][a-zA-Z0-9_]*$/
Expand Down Expand Up @@ -93,28 +90,14 @@ export const NitroUserConfigSchema = z.object({
* Nitrogen will not look for `.nitro.ts` files in these directories.
*/
ignorePaths: z.array(z.string()).optional(),
/**
* Configures whether a `.gitattributes` file will be generated in
* the `nitrogen/generated/` directory to mark files as linguist-generated for GitHub.
*/
createGitAttributes: z.boolean().optional().default(true),
})

/**
* Represents the structure of a `nitro.json` config file.
*/
export type NitroUserConfig = z.infer<typeof NitroUserConfigSchema>

export function writeUserConfigFile(
moduleName: string,
directory: string
): Promise<void> {
const config: NitroUserConfig = {
android: {
androidCxxLibName: `Nitro${capitalizeName(moduleName)}`,
androidNamespace: [moduleName.toLowerCase()],
},
cxxNamespace: [moduleName.toLowerCase()],
ios: {
iosModuleName: `Nitro${capitalizeName(moduleName)}`,
},
autolinking: {},
}
const dir = path.join(directory, 'nitro.json')
return fs.writeFile(dir, JSON.stringify(config, null, 2))
}
8 changes: 8 additions & 0 deletions packages/nitrogen/src/createGitAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from 'fs/promises'
import path from 'path'

export async function createGitAttributes(folder: string): Promise<void> {
const file = path.join(folder, '.gitattributes')
// Marks all files in this current folder as "generated"
await fs.writeFile(file, `* linguist-generated`)
}
14 changes: 12 additions & 2 deletions packages/nitrogen/src/nitrogen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { NitroConfig } from './config/NitroConfig.js'
import { createIOSAutolinking } from './autolinking/createIOSAutolinking.js'
import { createAndroidAutolinking } from './autolinking/createAndroidAutolinking.js'
import type { Autolinking } from './autolinking/Autolinking.js'
import { createGitAttributes } from './createGitAttributes.js'

interface NitrogenOptions {
baseDirectory: string
Expand Down Expand Up @@ -159,7 +160,7 @@ export async function runNitrogen({
generatedSpecs++
} catch (error) {
const message = indent(errorToString(error), ' ')
console.error(
Logger.error(
chalk.redBright(
` ❌ Failed to generate spec for ${typeName}! ${message}`
)
Expand All @@ -169,7 +170,7 @@ export async function runNitrogen({
}

if (generatedSpecs === startedWithSpecs) {
console.error(
Logger.error(
chalk.redBright(
` ❌ No specs found in ${sourceFile.getBaseName()}!`
)
Expand Down Expand Up @@ -203,6 +204,15 @@ export async function runNitrogen({
}
}

try {
if (NitroConfig.getCreateGitAttributes()) {
// write a .gitattributes file
await createGitAttributes(outputDirectory)
}
} catch {
Logger.error(`❌ Failed to write ${chalk.dim(`.gitattributes`)}!`)
}

return {
generatedFiles: filesAfter,
targetSpecsCount: targetSpecs,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native-nitro-image/nitro.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
},
"ignorePaths": [
"**/node_modules"
]
],
"createGitAttributes": false
}
Loading