Skip to content

Commit d4c5f8a

Browse files
authored
Add --translate-path option to fetch-entity script (#1798)
1 parent 58536fc commit d4c5f8a

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

scripts/fetch-entity/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ yarn fetch-entity --help
1616

1717
## Examples
1818

19+
### Resolve a path to its resource type and UUID
20+
21+
```bash
22+
yarn fetch-entity --translate-path /housing-assistance/home-loans/how-to-request-coe-esp/
23+
```
24+
1925
### Fetch a single resource by UUID
2026

2127
```bash

scripts/fetch-entity/src/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
import util from 'util'
33
import { program } from 'commander'
44
import { fetchEntity } from './fetchEntity'
5+
import { translatePath } from './translatePath'
56

67
program
78
.description(
89
'Fetch a resource from the Drupal JSON:API. Useful for grabbing mock data.'
910
)
10-
.argument('<resource-type>', 'The resource type to fetch')
11+
.argument('[resource-type]', 'The resource type to fetch')
1112
.argument(
1213
'[uuid]',
13-
'The ID of the resource to fetch (optional if using --collection)'
14+
'The ID of the resource to fetch (optional if using --collection or --translate-path)'
15+
)
16+
.option(
17+
'--translate-path <path>',
18+
'Resolve a Drupal path to its resource type and UUID via translatePath'
1419
)
1520
.option(
1621
'--include <includes...>',
@@ -28,6 +33,29 @@ program
2833
'1'
2934
)
3035
.action(async (resourceType, uuid, options) => {
36+
if (options.translatePath) {
37+
const pathInfo = await translatePath(options.translatePath)
38+
39+
if (options.json) {
40+
console.log(JSON.stringify(pathInfo, null, 2))
41+
} else {
42+
console.log(
43+
util.inspect(pathInfo, {
44+
depth: null,
45+
colors: process.stdout.isTTY,
46+
})
47+
)
48+
}
49+
return
50+
}
51+
52+
if (!resourceType || !uuid) {
53+
console.error(
54+
'Error: [resource-type] and [uuid] are required unless --translate-path is provided'
55+
)
56+
process.exit(1)
57+
}
58+
3159
const data = await fetchEntity(resourceType, uuid, options)
3260
// NOTE: There may be a thing we can do to just _check_ for circular
3361
// references. console.log(util.format('%j', data)) will output just
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { getEnvFileVars } from './getEnvVars'
2+
3+
process.env = {
4+
...process.env,
5+
...getEnvFileVars(),
6+
}
7+
8+
const { drupalClient } = await import('../../../src/lib/drupal/drupalClient')
9+
10+
export async function translatePath(path: string) {
11+
try {
12+
return await drupalClient.translatePath(path)
13+
} catch (e) {
14+
console.error(`Path not found: ${path}`)
15+
process.exit(1)
16+
}
17+
}

0 commit comments

Comments
 (0)