-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve sd-playground, eject feature (#1196)
- Loading branch information
1 parent
a140902
commit 96439c7
Showing
6 changed files
with
212 additions
and
51 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
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,36 @@ | ||
import { parse } from 'acorn'; | ||
import { asyncWalk } from 'estree-walker'; | ||
|
||
import type { Node } from 'estree-walker'; | ||
|
||
export async function analyzeDependencies(code: string) { | ||
let dependencies: Array<{ | ||
source: string; | ||
specifiers: Array<{ name: string; default: boolean }>; | ||
package: string; | ||
}> = []; | ||
const ast = parse(code, { | ||
allowImportExportEverywhere: true, | ||
ecmaVersion: 'latest', | ||
}) as Node; | ||
|
||
await asyncWalk(ast, { | ||
enter: async (node) => { | ||
if (node.type === 'ImportDeclaration') { | ||
const source = `${node.source.value}`; | ||
dependencies.push({ | ||
source: source, | ||
specifiers: node.specifiers.map((spec) => ({ | ||
name: spec.local.name, | ||
default: spec.type === 'ImportDefaultSpecifier', | ||
})), | ||
package: source | ||
.split('/') | ||
.slice(0, source.startsWith('@') ? 2 : 1) | ||
.join('/'), | ||
}); | ||
} | ||
}, | ||
}); | ||
return dependencies; | ||
} |
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,23 @@ | ||
import * as zip from '@zip.js/zip.js'; | ||
|
||
export async function downloadZIP(files: Record<string, string>) { | ||
const zipWriter = new zip.ZipWriter(new zip.BlobWriter('application/zip')); | ||
|
||
await Promise.all( | ||
Object.entries(files).map(([key, value]) => zipWriter.add(key, new zip.TextReader(value))), | ||
); | ||
|
||
// Close zip and make into URL | ||
const dataURI = await zipWriter.close(); | ||
const url = URL.createObjectURL(dataURI); | ||
|
||
// Auto-download the ZIP through anchor | ||
const anchor = document.createElement('a'); | ||
anchor.href = url; | ||
const today = new Date(); | ||
anchor.download = `sd-output_${today.getFullYear()}-${today.getMonth()}-${( | ||
'0' + today.getDate() | ||
).slice(-2)}.zip`; | ||
anchor.click(); | ||
URL.revokeObjectURL(url); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.