Skip to content

Commit 16b5cdc

Browse files
committed
Add support for URLs to copy. #114
1 parent 3a8806b commit 16b5cdc

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

packages/project/src/android/project.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'path';
2+
import fetch from 'cross-fetch';
23
import {
34
pathExists,
45
move,
@@ -259,12 +260,18 @@ export class AndroidProject extends PlatformProject {
259260
return writeFile(join(dir, file), contents);
260261
}
261262

262-
copyFile(src: string, dest: string): Promise<void> {
263+
async copyFile(src: string, dest: string): Promise<void> {
263264
if (!this.project?.config?.android?.path) {
264265
return Promise.reject();
265266
}
266-
const srcPath = join(this.project.config.android.path, src);
267267
const destPath = join(this.project.config.android.path, dest);
268+
269+
if (/^(https?:\/\/)/.test(src)) {
270+
const res = await fetch(src);
271+
return writeFile(destPath, Buffer.from(await res.arrayBuffer()));
272+
}
273+
274+
const srcPath = join(this.project.config.android.path, src);
268275
return copy(srcPath, destPath);
269276
}
270277

packages/project/test/project.android.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,11 @@ try {
319319
const destContents = await readFile(dest);
320320
expect(srcContents).toEqual(destContents);
321321
});
322+
323+
it('should copy URL', async () => {
324+
await project.android?.copyFile('https://via.placeholder.com/150C', 'placeholder.png');
325+
const dest = join(dir, 'android', 'placeholder.png');
326+
const destContents = await readFile(dest);
327+
expect(destContents.length).toBeGreaterThan(0);
328+
});
322329
});

packages/website/docs/configuration-tool.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ platforms:
300300
}
301301
```
302302

303+
Files can also be copied by using the `source` option instead of `text`:
304+
305+
```yaml
306+
platforms:
307+
android:
308+
res:
309+
- path: drawable
310+
file: icon.png
311+
source: ../common/test/fixtures/icon.png
312+
```
313+
314+
`source` supports a URL instead of a local path for remote resource loading.
315+
303316
### `json`
304317

305318
Modifies JSON files relative to the root of the Android project. Use `set` to override the element (and clobber any children), or `merge` to merge the values:
@@ -346,7 +359,7 @@ platforms:
346359

347360
### `copy`
348361

349-
Copies files and directories relative to the root of the android project (`./android` by default).
362+
Copies files, directories, or URLs relative to the root of the android project (`./android` by default).
350363

351364
```yaml
352365
platforms:
@@ -356,6 +369,8 @@ platforms:
356369
dest: app/google-services.json
357370
- src: old/path/of/directory
358371
dest: new/path/of/directory
372+
- src: https://example.com/file.png
373+
dest: new/path/of/file.png
359374
```
360375

361376
## iOS
@@ -569,7 +584,7 @@ platforms:
569584

570585
### `copy`
571586

572-
Copies files and directories relative to the root of the iOS project (`./ios/App` by default).
587+
Copies files, directories, or URLs relative to the root of the iOS project (`./ios/App` by default).
573588

574589
```yaml
575590
platforms:
@@ -581,4 +596,6 @@ platforms:
581596
dest: App/GoogleService-Info.plist
582597
- src: old/path/of/directory
583598
dest: new/path/of/directory
599+
- src: https://example.com/file.png
600+
dest: new/path/of/file.png
584601
```

packages/website/docs/project-api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ await project.android?.copyToResources('drawable', 'icon.png', source);
273273
const data = await project.android?.getResource('raw', 'test.json');
274274
```
275275

276+
`copyToResources` supports a URL as the last `source` option which will fetch the remote file instead.
277+
276278
#### Gradle
277279

278280
Gradle modifications are the most complicated and powerful of the capabilities in this library. Remember, `JAVA_HOME` must be set before using these methods.

0 commit comments

Comments
 (0)