-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use deno.json and move it inside build (#658)
- Loading branch information
Showing
6 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Brisa build utils | ||
|
||
This folder is made to move all the `utils` we currently have that are related to the building of the brisa here so that it is easier to maintain and understand. | ||
|
||
We are going to do this migration gradually, so as not to break anything, if you want to contribute to this task, it will be very well received. |
103 changes: 103 additions & 0 deletions
103
packages/brisa/src/build-utils/create-deno-json/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { createDenoJSON } from '.'; | ||
|
||
describe('createDenoJSON', () => { | ||
beforeEach(() => { | ||
globalThis.mockConstants = { | ||
BUILD_DIR: import.meta.dirname, | ||
ROOT_DIR: import.meta.dirname, | ||
SRC_DIR: import.meta.dirname, | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
delete globalThis.mockConstants; | ||
fs.rmSync(path.join(import.meta.dirname, 'deno.json')); | ||
}); | ||
|
||
it('should create the default deno.json file when no existing one', () => { | ||
createDenoJSON(); | ||
expect( | ||
JSON.parse( | ||
fs.readFileSync(path.join(import.meta.dirname, 'deno.json'), 'utf-8'), | ||
), | ||
).toEqual({ | ||
imports: { | ||
'fs/promises': 'node:fs/promises', | ||
path: 'node:path', | ||
}, | ||
permissions: { | ||
read: true, | ||
write: true, | ||
run: 'inherit', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should merge with the existing deno.json file in the root directory', () => { | ||
// Ensure is not taking src dir | ||
globalThis.mockConstants!.SRC_DIR = path.join(import.meta.dirname, 'src'); | ||
fs.writeFileSync( | ||
path.join(import.meta.dirname, 'deno.json'), | ||
JSON.stringify({ | ||
permissions: { | ||
read: false, | ||
}, | ||
}), | ||
); | ||
createDenoJSON(); | ||
expect( | ||
JSON.parse( | ||
fs.readFileSync(path.join(import.meta.dirname, 'deno.json'), 'utf-8'), | ||
), | ||
).toEqual({ | ||
imports: { | ||
'fs/promises': 'node:fs/promises', | ||
path: 'node:path', | ||
}, | ||
permissions: { | ||
read: false, | ||
write: true, | ||
run: 'inherit', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should merge with the existing deno.json file in the src directory', () => { | ||
// Ensure is taking src dir | ||
globalThis.mockConstants!.ROOT_DIR = path.join( | ||
import.meta.dirname, | ||
'not-exist', | ||
); | ||
|
||
fs.writeFileSync( | ||
path.join(import.meta.dirname, 'deno.json'), | ||
JSON.stringify({ | ||
imports: { | ||
cluster: 'node:cluster', | ||
}, | ||
foo: 'bar', | ||
}), | ||
); | ||
createDenoJSON(); | ||
expect( | ||
JSON.parse( | ||
fs.readFileSync(path.join(import.meta.dirname, 'deno.json'), 'utf-8'), | ||
), | ||
).toEqual({ | ||
foo: 'bar', | ||
imports: { | ||
'fs/promises': 'node:fs/promises', | ||
path: 'node:path', | ||
cluster: 'node:cluster', | ||
}, | ||
permissions: { | ||
read: true, | ||
write: true, | ||
run: 'inherit', | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { getConstants } from '@/constants'; | ||
|
||
export function createDenoJSON() { | ||
const { BUILD_DIR } = getConstants(); | ||
const denoJSON = getDenoJSON(); | ||
fs.writeFileSync( | ||
path.join(BUILD_DIR, 'deno.json'), | ||
JSON.stringify(denoJSON, null, 2), | ||
); | ||
} | ||
|
||
function getDenoJSON() { | ||
const { ROOT_DIR, SRC_DIR } = getConstants(); | ||
const defaultDenoJSON = { | ||
imports: { | ||
'fs/promises': 'node:fs/promises', | ||
path: 'node:path', | ||
}, | ||
permissions: { | ||
read: true, | ||
write: true, | ||
run: 'inherit', | ||
}, | ||
}; | ||
|
||
if (fs.existsSync(path.join(ROOT_DIR, 'deno.json'))) { | ||
const denoJSON = JSON.parse( | ||
fs.readFileSync(path.join(ROOT_DIR, 'deno.json'), 'utf-8'), | ||
); | ||
return { | ||
...denoJSON, | ||
imports: { | ||
...defaultDenoJSON.imports, | ||
...denoJSON.imports, | ||
}, | ||
permissions: { | ||
...defaultDenoJSON.permissions, | ||
...denoJSON.permissions, | ||
}, | ||
}; | ||
} | ||
|
||
if (fs.existsSync(path.join(SRC_DIR, 'deno.json'))) { | ||
const denoJSON = JSON.parse( | ||
fs.readFileSync(path.join(SRC_DIR, 'deno.json'), 'utf-8'), | ||
); | ||
return { | ||
...denoJSON, | ||
imports: { | ||
...defaultDenoJSON.imports, | ||
...denoJSON.imports, | ||
}, | ||
permissions: { | ||
...defaultDenoJSON.permissions, | ||
...denoJSON.permissions, | ||
}, | ||
}; | ||
} | ||
|
||
return defaultDenoJSON; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters