-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add smart werable pack and fix preview of SW on sdk7 #682
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c7bba18
add smart werable pack and fix preview of SW on sdk7
gonpombo8 af02289
add sw ENABLE_WEB3
gonpombo8 18172d9
update wearable id so the popup is shown on every restart
gonpombo8 ad34eeb
fix build for smart wearables
gonpombo8 2e1656e
fix typo
gonpombo8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add description for
--skip-install