-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add smart werable pack and fix preview of SW on sdk7 (#682)
* add smart werable pack and fix preview of SW on sdk7 * add sw ENABLE_WEB3 * update wearable id so the popup is shown on every restart * fix build for smart wearables * fix typo
- Loading branch information
Showing
11 changed files
with
876 additions
and
16 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
127 changes: 127 additions & 0 deletions
127
packages/@dcl/sdk-commands/src/commands/pack-smart-wearable/index.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,127 @@ | ||
import path from 'path' | ||
import archiver from 'archiver' | ||
|
||
import { CliComponents } from '../../components' | ||
import { declareArgs } from '../../logic/args' | ||
import { installDependencies, needsDependencies, WearableProject } from '../../logic/project-validations' | ||
import { b64HashingFunction, getProjectPublishableFilesWithHashes } from '../../logic/project-files' | ||
import { printCurrentProjectStarting } from '../../logic/beautiful-logs' | ||
import { getValidWorkspace } from '../../logic/workspace-validations' | ||
import { Result } from 'arg' | ||
import { buildScene } from '../build' | ||
|
||
interface Options { | ||
args: Result<typeof args> | ||
components: Pick<CliComponents, 'fs' | 'logger' | 'analytics' | 'spawner'> | ||
} | ||
|
||
export const args = declareArgs({ | ||
'--skip-build': Boolean, | ||
'--skip-install': Boolean, | ||
'--dir': String | ||
}) | ||
|
||
export function help(options: Options) { | ||
options.components.logger.log(` | ||
Usage: 'sdk-commands pack-smart-wearable [options]' | ||
Options:' | ||
-h, --help Displays complete help | ||
--skip-build Skip build and use the file defined in scene.json | ||
--skip-install Skip installing dependencies | ||
--dir Path to directory to build | ||
Example: | ||
- Pack your smart-wearable scene: | ||
'$ sdk-commands pack-smart-wearable' | ||
`) | ||
} | ||
|
||
export async function main(options: Options) { | ||
const workingDirectory = path.resolve(process.cwd(), options.args['--dir'] || '.') | ||
|
||
const workspace = await getValidWorkspace(options.components, workingDirectory) | ||
|
||
for (const project of workspace.projects) { | ||
printCurrentProjectStarting(options.components.logger, project, workspace) | ||
if (project.kind === 'wearable') { | ||
await packSmartWearable(options, project) | ||
} | ||
} | ||
} | ||
|
||
export async function packSmartWearable(options: Options, project: WearableProject) { | ||
const shouldInstallDeps = | ||
!options.args['--skip-install'] && (await needsDependencies(options.components, project.workingDirectory)) | ||
const shouldBuild = !options.args['--skip-build'] | ||
|
||
if (shouldInstallDeps && !options.args['--skip-install']) { | ||
await installDependencies(options.components, project.workingDirectory) | ||
} | ||
|
||
if (shouldBuild) { | ||
await buildScene({ ...options, args: { '--dir': project.workingDirectory, _: [], '--production': true } }, project) | ||
} | ||
|
||
const files = await getProjectPublishableFilesWithHashes(options.components, project.workingDirectory, async ($) => $) | ||
let totalSize = 0 | ||
for (const filePath of files) { | ||
const stat = await options.components.fs.stat(filePath.absolutePath) | ||
if (stat.isFile()) { | ||
totalSize += stat.size | ||
} | ||
} | ||
const MAX_WEARABLE_SIZE = 2097152 | ||
if (totalSize > MAX_WEARABLE_SIZE) { | ||
options.components.logger.info(`Smart Wearable max size (${MAX_WEARABLE_SIZE} bytes) reached: ${totalSize} bytes. | ||
Please try to remove unneccessary files and/or reduce the files size, you can ignore file adding in .dclignore.`) | ||
} | ||
const ZIP_FILE_NAME = 'smart-wearable.zip' | ||
const packDir = path.resolve(project.workingDirectory, ZIP_FILE_NAME) | ||
if (await options.components.fs.fileExists(packDir)) { | ||
await options.components.fs.rm(packDir) | ||
} | ||
options.components.logger.info(packDir) | ||
|
||
try { | ||
await zipProject( | ||
options.components.fs, | ||
files.map(($) => $.absolutePath.replace(project.workingDirectory + '/', '')), | ||
packDir | ||
) | ||
} catch (e) { | ||
options.components.logger.error('Error creating zip file', (e as any).message) | ||
} | ||
|
||
options.components.analytics.track('Pack smart wearable', { | ||
projectHash: await b64HashingFunction(project.workingDirectory) | ||
}) | ||
options.components.logger.log('Smart wearable packed successfully.') | ||
} | ||
|
||
function zipProject(fs: CliComponents['fs'], files: string[], target: string) { | ||
const output = fs.createWriteStream(target) | ||
const archive = archiver('zip') | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
output.on('close', () => { | ||
resolve() | ||
}) | ||
|
||
archive.on('warning', (err) => { | ||
reject(err) | ||
}) | ||
|
||
archive.on('error', (err) => { | ||
reject(err) | ||
}) | ||
|
||
archive.pipe(output) | ||
|
||
for (const file of files) { | ||
if (file === '') continue | ||
archive.file(file, { name: file }) | ||
} | ||
|
||
return archive.finalize() | ||
}) | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
packages/@dcl/sdk-commands/src/logic/portable-experience-sw-validations.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,50 @@ | ||
import { resolve } from 'path' | ||
import { Scene } from '@dcl/schemas' | ||
|
||
import { CliError } from './error' | ||
import { CliComponents } from '../components' | ||
export interface IFile { | ||
path: string | ||
content: Buffer | ||
size: number | ||
} | ||
|
||
export const SMART_WEARABLE_FILE = 'wearable.json' | ||
|
||
/** | ||
* Composes the path to the `scene.json` file based on the provided path. | ||
* @param projectRoot The path to the directory containing the scene file. | ||
*/ | ||
export function getSmartWearableFile(projectRoot: string): string { | ||
return resolve(projectRoot, SMART_WEARABLE_FILE) | ||
} | ||
|
||
export function assertValidSmartWearable(scene: Scene) { | ||
if (!Scene.validate(scene)) { | ||
const errors: string[] = [] | ||
if (Scene.validate.errors) { | ||
for (const error of Scene.validate.errors) { | ||
errors.push(`Error validating scene.json: ${error.message}`) | ||
} | ||
} | ||
throw new CliError('Invalid scene.json file:\n' + errors.join('\n')) | ||
} | ||
// TODO | ||
return true | ||
} | ||
|
||
/** | ||
* Get valid Scene JSON | ||
*/ | ||
export async function getValidWearableJson( | ||
components: Pick<CliComponents, 'fs' | 'logger'>, | ||
projectRoot: string | ||
): Promise<Scene> { | ||
try { | ||
const wearableJsonRaw = await components.fs.readFile(getSmartWearableFile(projectRoot), 'utf8') | ||
const wearableJson = JSON.parse(wearableJsonRaw) as Scene | ||
return wearableJson | ||
} catch (err: any) { | ||
throw new CliError(`Error reading the wearable.json file: ${err.message}`) | ||
} | ||
} |
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