Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
ErwinOtten committed Oct 25, 2024
1 parent b344042 commit e4bf422
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# next-assetlinks

Easily generate an `assetlinks.json` for Android app association in your Next.js
project. This tool creates `.well-known/assetlinks.json`, allowing Android apps
to handle URLs.

## Installation

Install via npm:

```bash
npm i next-assetlinks
# or
yarn add next-assetlinks
```

## Setup

1. **Create a Config File**
Add `next-assetlinks.config.js` to your project root:

```javascript
// next-assetlinks.config.js
module.exports = {
packageName: "com.example.app",
sha256Fingerprint: ["FINGERPRINT_1", "FINGERPRINT_2"],
};
```

- **`packageName`**: Your Android app's package name.
- **`sha256Fingerprint`**: Array of SHA-256 fingerprints for your app's
signing certificates.

2. **Add Script to `package.json`**
Update package.json to add the generate-assetlinks script:

```json
"scripts": {
"generate-assetlinks": "node ./node_modules/next-assetlinks/cli.js"
}
```

## Usage

Run the command to generate `assetlinks.json`:

```bash
npm run generate-assetlinks
# or
yarn generate-assetlinks
```

This will create or update `public/.well-known/assetlinks.json` with the
configured package name and fingerprints.

## Contributing

Contributions are welcome!

## Credits

Package by [Reach Digital](https://www.reachdigital.nl/)

## License

Licensed under MIT. See [LICENSE](./LICENSE) for details.
5 changes: 5 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

const generateAssetLinks = require('./index.js')

generateAssetLinks()
45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const fs = require('fs')
const path = require('path')

function generateAssetLinks() {
const configPath = path.join(process.cwd(), 'next-assetlinks.config.js')

if (!fs.existsSync(configPath)) {
console.warn("'next-assetlinks.config.js' not found. Skipping assetlinks.json generation.")
return // Stop the function gracefully without crashing
}

const { packageName, sha256Fingerprint } = require(configPath)

if (!packageName || !Array.isArray(sha256Fingerprint) || sha256Fingerprint.length === 0) {
throw new Error(
"Error: 'packageName' or 'sha256Fingerprint' array is missing or empty in 'next-assetlinks.config.js'. Please check your configuration.",
)
}

const directoryPath = path.join(process.cwd(), 'public/.well-known')

if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true })
}

const assetLinksContent = [
{
relation: ['delegate_permission/common.handle_all_urls'],
target: {
namespace: 'android_app',
package_name: packageName,
sha256_cert_fingerprints: sha256Fingerprint,
},
},
]

fs.writeFileSync(
path.join(directoryPath, 'assetlinks.json'),
JSON.stringify(assetLinksContent, null, 2),
)

console.log('assetlinks.json file successfully created.')
}

module.exports = generateAssetLinks
4 changes: 4 additions & 0 deletions next-assetlinks.config.js.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
packageName: 'com.example.app',
sha256Fingerprint: ['FINGERPRINT_1', 'FINGERPRINT_2'],
}
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "next-assetlinks",
"homepage": "https://www.reachdigital.nl/",
"repository": "github:ho-nl/next-assetlinks",
"version": "1.0.0",
"description": "Generate an assetlinks.json file for Next.js projects",
"main": "index.js",
"bin": {
"generate-assetlinks": "./cli.js"
},
"dependencies": {},
"author": "Reach Digital",
"license": "MIT"
}

0 comments on commit e4bf422

Please sign in to comment.