Skip to content

Commit 5e1df6d

Browse files
authored
feat: workspaces (#89)
* wip * wip * fix * fix lint * change @dcl dependencies version * update kernel version * update min cli version and last kernel PR version * add timeout to export * fix lint * endpoint if scene.json fails * update kernel version, add TODO comment and change .filter to a single reduce call * fix typo * ordering preview wearables * fix typo * update kernel * remove cli version requirement * add cli version requirement 3.7.0
1 parent 23214a5 commit 5e1df6d

File tree

6 files changed

+73
-66
lines changed

6 files changed

+73
-66
lines changed

packages/decentraland-ecs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"dependencies": {
3030
"@dcl/amd": "file:../@dcl/amd",
3131
"@dcl/build-ecs": "file:../@dcl/build-ecs",
32-
"@dcl/kernel": "1.0.0-1692945184.commit-4d9fcfd",
32+
"@dcl/kernel": "1.0.0-1772786810.commit-feaf312",
3333
"@dcl/posix": "^1.0.4",
3434
"@dcl/schemas": "3.5.0",
3535
"@dcl/unity-renderer": "^1.0.26165-20220128194341.commit-8eaafdc",

packages/decentraland-ecs/src/cli/mock-catalyst/index.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import { getAllPreviewWearables } from '../wearables'
66

77
export const mockCatalyst = (
88
app: express.Application,
9-
baseFolders: string[],
10-
rootFolder: string
9+
baseFolders: string[]
1110
) => {
12-
serveFolders(app, baseFolders, rootFolder)
11+
serveFolders(app, baseFolders)
1312
app.get('/lambdas/explore/realms', (req, res) => {
1413
res.json([
1514
{
@@ -37,7 +36,6 @@ export const mockCatalyst = (
3736
try {
3837
const previewWearables = await getAllPreviewWearables({
3938
baseFolders,
40-
catalystRootFolder: rootFolder,
4139
baseUrl: ''
4240
}).map((wearable) => wearable.id)
4341

@@ -95,11 +93,7 @@ export const mockCatalyst = (
9593
)
9694
}
9795

98-
const serveFolders = (
99-
app: express.Application,
100-
baseFolders: string[],
101-
catalystRootFolder: string
102-
) => {
96+
const serveFolders = (app: express.Application, baseFolders: string[]) => {
10397
app.get('/content/contents/:hash', (req, res, next) => {
10498
if (req.params.hash && req.params.hash.startsWith('b64-')) {
10599
const fullPath = path.resolve(
@@ -110,7 +104,7 @@ const serveFolders = (
110104

111105
// only return files IF the file is within a baseFolder
112106
if (!baseFolders.find((folder: string) => fullPath.startsWith(folder))) {
113-
res.end(404)
107+
next()
114108
return
115109
}
116110

@@ -149,8 +143,7 @@ const serveFolders = (
149143

150144
const resultEntities = getSceneJson({
151145
baseFolders,
152-
pointers: Array.from(requestedPointers),
153-
catalystRootFolder
146+
pointers: Array.from(requestedPointers)
154147
})
155148
res.json(resultEntities).end()
156149
})

packages/decentraland-ecs/src/cli/setupUtils.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ export const getFilesFromFolder = ({
3030
folder,
3131
addOriginalPath,
3232
ignorePattern,
33-
customHashMaker,
34-
rootFolder
33+
customHashMaker
3534
}: {
3635
folder: string
3736
addOriginalPath?: boolean
3837
ignorePattern?: string
3938
customHashMaker?: (str: string) => string
40-
rootFolder?: string
4139
}) => {
4240
const hashMaker = customHashMaker ? customHashMaker : defaultHashMaker
4341

@@ -73,20 +71,16 @@ export const getFilesFromFolder = ({
7371
return
7472
}
7573

76-
const absoluteFolder = rootFolder
77-
? rootFolder.replace(/\\/gi, '/')
78-
: folder.replace(/\\/gi, '/')
79-
const relativeFilePathToRootFolder = absolutePath
80-
.replace(absoluteFolder, '')
81-
.replace(/^\/+/, '')
74+
const absoluteFolder = folder.replace(/\\/gi, '/')
75+
8276
const relativeFilePathToFolder = file
8377
.replace(absoluteFolder, '')
8478
.replace(/^\/+/, '')
8579

8680
return {
8781
file: relativeFilePathToFolder.toLowerCase(),
8882
original_path: addOriginalPath ? absolutePath : undefined,
89-
hash: hashMaker(relativeFilePathToRootFolder)
83+
hash: hashMaker(absolutePath)
9084
}
9185
})
9286
.filter(($) => !!$)
@@ -96,14 +90,12 @@ export function entityV3FromFolder({
9690
folder,
9791
addOriginalPath,
9892
ignorePattern,
99-
customHashMaker,
100-
catalystRootFolder
93+
customHashMaker
10194
}: {
10295
folder: string
10396
addOriginalPath?: boolean
10497
ignorePattern?: string
10598
customHashMaker?: (str: string) => string
106-
catalystRootFolder: string
10799
}) {
108100
const sceneJsonPath = path.resolve(folder, './scene.json')
109101
let isParcelScene = true
@@ -128,7 +120,6 @@ export function entityV3FromFolder({
128120

129121
if (fs.existsSync(sceneJsonPath) && isParcelScene) {
130122
const sceneJson = JSON.parse(fs.readFileSync(sceneJsonPath).toString())
131-
132123
const { base, parcels }: { base: string; parcels: string[] } =
133124
sceneJson.scene
134125
const pointers = new Set<string>()
@@ -139,8 +130,7 @@ export function entityV3FromFolder({
139130
folder,
140131
addOriginalPath,
141132
ignorePattern,
142-
customHashMaker,
143-
rootFolder: catalystRootFolder
133+
customHashMaker
144134
})
145135
return {
146136
version: 'v3',
@@ -159,13 +149,11 @@ export function entityV3FromFolder({
159149
export function getSceneJson({
160150
baseFolders,
161151
pointers,
162-
customHashMaker,
163-
catalystRootFolder
152+
customHashMaker
164153
}: {
165154
baseFolders: string[]
166155
pointers: string[]
167156
customHashMaker?: (str: string) => string
168-
catalystRootFolder: string
169157
}) {
170158
const requestedPointers = new Set<string>(pointers)
171159
const resultEntities = []
@@ -183,8 +171,7 @@ export function getSceneJson({
183171
folder,
184172
addOriginalPath: false,
185173
ignorePattern: ignoreFileContent,
186-
customHashMaker,
187-
catalystRootFolder
174+
customHashMaker
188175
})
189176
})
190177

@@ -224,7 +211,11 @@ export async function ensureCopyFile(fromFilePath: string, filePath: any) {
224211
await fs.promises.copyFile(fromFilePath, filePath)
225212
}
226213

227-
export const downloadFile = async (url: string, path: string) => {
214+
export const downloadFile = async (
215+
url: string,
216+
path: string,
217+
timeout_seg: number = 15
218+
) => {
228219
return new Promise((resolve, reject) => {
229220
const file = fs.createWriteStream(path)
230221

@@ -233,18 +224,28 @@ export const downloadFile = async (url: string, path: string) => {
233224
schema = https as any
234225
}
235226

236-
schema
227+
let finished = false
228+
const request = schema
237229
.get(url, function (response) {
238230
response.pipe(file)
239231
file.on('finish', function () {
240232
file.close()
233+
finished = true
241234
resolve(true)
242235
})
243236
})
244237
.on('error', function (err) {
245238
fs.unlinkSync(path)
239+
finished = true
246240
reject(err)
247241
})
242+
243+
setTimeout(() => {
244+
if (!finished) {
245+
request.destroy()
246+
reject(new Error(`Timeout ${url}`))
247+
}
248+
}, timeout_seg * 1000)
248249
})
249250
}
250251

packages/decentraland-ecs/src/cli/wearables.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import { sdk } from '@dcl/schemas'
77

88
const serveWearable = ({
99
assetJsonPath,
10-
baseUrl,
11-
catalystRootFolder
10+
baseUrl
1211
}: {
1312
assetJsonPath: string
1413
baseUrl: string
15-
catalystRootFolder: string
1614
}) => {
1715
const wearableDir = path.dirname(assetJsonPath)
1816
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -42,8 +40,7 @@ const serveWearable = ({
4240
const hashedFiles = getFilesFromFolder({
4341
folder: wearableDir,
4442
addOriginalPath: false,
45-
ignorePattern: ignoreFileContent,
46-
rootFolder: catalystRootFolder
43+
ignorePattern: ignoreFileContent
4744
})
4845

4946
const thumbnailFiltered = hashedFiles.filter(
@@ -96,11 +93,9 @@ const serveWearable = ({
9693

9794
export const getAllPreviewWearables = ({
9895
baseFolders,
99-
catalystRootFolder,
10096
baseUrl
10197
}: {
10298
baseFolders: string[]
103-
catalystRootFolder: string
10499
baseUrl: string
105100
}) => {
106101
const assetPathArray: string[] = []
@@ -114,7 +109,7 @@ export const getAllPreviewWearables = ({
114109
const ret = []
115110
for (const assetJsonPath of assetPathArray) {
116111
try {
117-
ret.push(serveWearable({ assetJsonPath, baseUrl, catalystRootFolder }))
112+
ret.push(serveWearable({ assetJsonPath, baseUrl }))
118113
} catch (err) {
119114
console.error(
120115
`Couldn't mock the asset ${assetJsonPath}. Please verify the correct format and scheme.`,
@@ -127,29 +122,26 @@ export const getAllPreviewWearables = ({
127122

128123
export const mockPreviewWearables = (
129124
app: express.Application,
130-
baseFolders: string[],
131-
catalystRootFolder: string
125+
baseFolders: string[]
132126
) => {
133-
app.use('/preview-wearables', async (req, res) => {
134-
const baseUrl = `http://${req.get('host')}/content/contents`
135-
return res.json({
136-
ok: true,
137-
data: getAllPreviewWearables({ baseUrl, baseFolders, catalystRootFolder })
138-
})
139-
})
140-
141127
app.use('/preview-wearables/:id', async (req, res) => {
142-
const baseUrl = `http://${req.get('host')}/content/contents`
128+
const baseUrl = `${req.protocol}://${req.get('host')}/content/contents`
143129
const wearables = getAllPreviewWearables({
144130
baseUrl,
145-
baseFolders,
146-
catalystRootFolder
131+
baseFolders
147132
})
148133
const wearableId = req.params.id
149-
150134
return res.json({
151135
ok: true,
152136
data: wearables.filter((w) => w?.id === wearableId)
153137
})
154138
})
139+
140+
app.use('/preview-wearables', async (req, res) => {
141+
const baseUrl = `${req.protocol}://${req.get('host')}/content/contents`
142+
return res.json({
143+
ok: true,
144+
data: getAllPreviewWearables({ baseUrl, baseFolders })
145+
})
146+
})
155147
}

packages/decentraland-ecs/src/setupExport.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ const setupExport = async ({
106106
getSceneJson({
107107
baseFolders: [workDir],
108108
pointers: sceneJson?.scene?.parcels || ['0,0'],
109-
customHashMaker: shaHashMaker,
110-
catalystRootFolder: workDir
109+
customHashMaker: shaHashMaker
111110
})
112111
)
113112
)
@@ -128,8 +127,7 @@ const setupExport = async ({
128127
folder: workDir,
129128
addOriginalPath: true,
130129
ignorePattern: ignoreFileContent,
131-
customHashMaker: shaHashMaker,
132-
catalystRootFolder: workDir
130+
customHashMaker: shaHashMaker
133131
})
134132
if (contentStatic?.content) {
135133
for (const $ of contentStatic?.content) {

packages/decentraland-ecs/src/setupProxy.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as express from 'express'
44
import { createStaticRoutes } from './cli/setupUtils'
55
import { mockCatalyst } from './cli/mock-catalyst'
66
import { mockPreviewWearables } from './cli/wearables'
7+
import { sdk } from '@dcl/schemas'
78

89
const setupProxy = (dcl: any, app: express.Application) => {
910
// first resolve all dependencies in the local current working directory
@@ -44,16 +45,38 @@ const setupProxy = (dcl: any, app: express.Application) => {
4445
})
4546
)
4647

47-
const baseSceneFolders: string[] = [dcl.getWorkingDir()]
48+
let baseSceneFolders: string[] = [dcl.getWorkingDir()]
49+
let baseWearableFolders: string[] = [dcl.getWorkingDir()]
50+
51+
// TODO: merge types from github.com/decentraland/cli
52+
if (dcl.workspace) {
53+
const projects = dcl.workspace.getAllProjects()
54+
if (!!projects?.length) {
55+
const { wearables, scenes } = projects.reduce(
56+
(acc: { wearables: string[]; scenes: string[] }, project: any) => {
57+
const projectType = project.getInfo().sceneType
58+
const projectDir = project.getProjectWorkingDir()
59+
if (projectType === sdk.ProjectType.SCENE) acc.scenes.push(projectDir)
60+
if (projectType === sdk.ProjectType.PORTABLE_EXPERIENCE)
61+
acc.wearables.push(projectDir)
62+
return acc
63+
},
64+
{ wearables: [], scenes: [] }
65+
)
66+
67+
baseSceneFolders = scenes
68+
baseWearableFolders = wearables
69+
}
70+
}
71+
4872
try {
49-
mockCatalyst(app, baseSceneFolders, dcl.getWorkingDir())
73+
mockCatalyst(app, [...baseSceneFolders, ...baseWearableFolders])
5074
} catch (err) {
5175
console.error(`Fatal error, couldn't mock the catalyst`, err)
5276
}
5377

54-
const baseWearableFolders: string[] = [dcl.getWorkingDir()]
5578
try {
56-
mockPreviewWearables(app, baseWearableFolders, dcl.getWorkingDir())
79+
mockPreviewWearables(app, baseWearableFolders)
5780
} catch (err) {
5881
console.error(`Fatal error, couldn't mock the wearables`, err)
5982
}

0 commit comments

Comments
 (0)