11import fs from "fs" ;
2- import path from "path" ;
3- import https from "https" ;
42import { fileURLToPath } from "url" ;
3+ import { dirname , join } from "path" ;
54
6- const __filename = fileURLToPath ( import . meta. url ) ;
7- const __dirname = path . dirname ( __filename ) ;
8-
9- const DATAPACKAGE_PATH = path . join ( __dirname , "data" , "datapackage.json" ) ;
10- const LICENSES_API_URL =
11- "https://licenses.opendefinition.org/licenses/groups/all.json" ;
5+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
6+ const DATAPACKAGE_PATH = join ( __dirname , "data" , "datapackage.json" ) ;
7+ const LICENSES_API_URL = "https://licenses.opendefinition.org/licenses/groups/all.json" ;
128
139/**
1410 * Fetch license data from the Open Definition API
1511 */
16- function fetchLicenseData ( ) {
17- return new Promise ( ( resolve , reject ) => {
18- https
19- . get ( LICENSES_API_URL , ( res ) => {
20- let data = "" ;
12+ async function fetchLicenseData ( ) {
13+ const response = await fetch ( LICENSES_API_URL ) ;
14+ if ( ! response . ok ) {
15+ throw new Error ( `Failed to fetch license data: ${ response . statusText } ` ) ;
16+ }
17+ return response . json ( ) ;
18+ }
19+
20+ /**
21+ * Read and parse the datapackage.json file
22+ */
23+ function readDatapackage ( ) {
24+ const fileContent = fs . readFileSync ( DATAPACKAGE_PATH , "utf8" ) ;
25+ return JSON . parse ( fileContent ) ;
26+ }
27+
28+ /**
29+ * Write the updated datapackage back to file
30+ */
31+ function writeDatapackage ( datapackage ) {
32+ const jsonString = JSON . stringify ( datapackage , null , 2 ) ;
33+ fs . writeFileSync ( DATAPACKAGE_PATH , jsonString , "utf8" ) ;
34+ }
2135
22- res . on ( "data" , ( chunk ) => {
23- data += chunk ;
24- } ) ;
36+ /**
37+ * Find and update the license_id field with enum values
38+ */
39+ function updateLicenseIdField ( datapackage , licenseIds ) {
40+ const portalsResource = datapackage . resources . find ( r => r . name === "portals" ) ;
41+ const licenseIdField = portalsResource . schema . fields . find ( f => f . name === "license_id" ) ;
42+
43+ if ( ! licenseIdField . constraints ) {
44+ licenseIdField . constraints = { } ;
45+ }
46+ licenseIdField . constraints . enum = licenseIds ;
47+ }
2548
26- res . on ( "end" , ( ) => {
27- try {
28- const jsonData = JSON . parse ( data ) ;
29- resolve ( jsonData ) ;
30- } catch ( error ) {
31- reject ( new Error ( `Failed to parse JSON: ${ error . message } ` ) ) ;
32- }
33- } ) ;
34- } )
35- . on ( "error" , ( error ) => {
36- reject ( new Error ( `Failed to fetch license data: ${ error . message } ` ) ) ;
37- } ) ;
38- } ) ;
49+ /**
50+ * Display summary of the update
51+ */
52+ function showUpdateSummary ( licenseIds ) {
53+ console . log ( "✅ Successfully updated datapackage.json with license enum values" ) ;
54+ console . log ( ` Added ${ licenseIds . length } license IDs to the enum constraint` ) ;
55+
56+ const firstFive = licenseIds . slice ( 0 , 5 ) ;
57+ const lastFive = licenseIds . slice ( - 5 ) ;
58+ const examples = [ ...firstFive , "..." , ...lastFive ] ;
59+ const exampleString = examples . join ( ", " ) ;
60+ console . log ( ` Examples: ${ exampleString } ` ) ;
3961}
4062
4163/**
@@ -45,55 +67,18 @@ async function updateDatapackage() {
4567 try {
4668 console . log ( "Fetching license data from Open Definition API..." ) ;
4769 const licenseData = await fetchLicenseData ( ) ;
48-
49- // Extract license IDs (keys from the JSON object)
5070 const licenseIds = Object . keys ( licenseData ) . sort ( ) ;
5171 console . log ( `Found ${ licenseIds . length } license IDs` ) ;
5272
53- // Read current datapackage.json
5473 console . log ( "Reading current datapackage.json..." ) ;
55- const datapackageContent = fs . readFileSync ( DATAPACKAGE_PATH , "utf8" ) ;
56- const datapackage = JSON . parse ( datapackageContent ) ;
57-
58- // Find the license_id field in the schema
59- const portalsResource = datapackage . resources . find (
60- ( r ) => r . name === "portals"
61- ) ;
62- if ( ! portalsResource ) {
63- throw new Error ( 'Could not find "portals" resource in datapackage.json' ) ;
64- }
65-
66- const licenseIdField = portalsResource . schema . fields . find (
67- ( f ) => f . name === "license_id"
68- ) ;
69- if ( ! licenseIdField ) {
70- throw new Error ( 'Could not find "license_id" field in schema' ) ;
71- }
74+ const datapackage = readDatapackage ( ) ;
7275
73- // Update the constraints to include enum values
74- if ( ! licenseIdField . constraints ) {
75- licenseIdField . constraints = { } ;
76- }
76+ updateLicenseIdField ( datapackage , licenseIds ) ;
7777
78- licenseIdField . constraints . enum = licenseIds ;
79-
80- // Write updated datapackage.json back to file
8178 console . log ( "Writing updated datapackage.json..." ) ;
82- const updatedContent = JSON . stringify ( datapackage , null , 2 ) ;
83- fs . writeFileSync ( DATAPACKAGE_PATH , updatedContent , "utf8" ) ;
84-
85- console . log (
86- "✅ Successfully updated datapackage.json with license enum values"
87- ) ;
88- console . log (
89- ` Added ${ licenseIds . length } license IDs to the enum constraint`
90- ) ;
79+ writeDatapackage ( datapackage ) ;
9180
92- // Display first few and last few license IDs as examples
93- const examples = licenseIds
94- . slice ( 0 , 5 )
95- . concat ( [ "..." ] , licenseIds . slice ( - 5 ) ) ;
96- console . log ( ` Examples: ${ examples . join ( ", " ) } ` ) ;
81+ showUpdateSummary ( licenseIds ) ;
9782 } catch ( error ) {
9883 console . error ( "❌ Error updating datapackage.json:" , error . message ) ;
9984 process . exit ( 1 ) ;
0 commit comments