-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from autonomys/chore/remove-fs
Separate into node and browser outputs
- Loading branch information
Showing
13 changed files
with
1,504 additions
and
211 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,40 @@ | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import json from '@rollup/plugin-json' | ||
import resolve from '@rollup/plugin-node-resolve' | ||
import typescript from '@rollup/plugin-typescript' | ||
import { terser } from 'rollup-plugin-terser' | ||
|
||
export default [ | ||
{ | ||
input: 'src/node.ts', | ||
output: { | ||
file: 'dist/node.js', | ||
format: 'esm', | ||
sourcemap: true, | ||
}, | ||
plugins: [ | ||
resolve({ | ||
preferBuiltins: false, | ||
}), | ||
commonjs(), | ||
typescript({ tsconfig: './tsconfig.json' }), | ||
json(), | ||
], | ||
}, | ||
{ | ||
input: 'src/browser.ts', | ||
output: { | ||
file: 'dist/browser.js', | ||
format: 'esm', | ||
sourcemap: true, | ||
}, | ||
plugins: [ | ||
resolve({ | ||
preferBuiltins: false, | ||
}), | ||
commonjs(), | ||
typescript({ tsconfig: './tsconfig.json' }), | ||
json(), | ||
], | ||
}, | ||
] |
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
File renamed without changes.
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,2 @@ | ||
export * from './utils.js' | ||
export * from './wrappers.js' |
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,79 @@ | ||
import { WriteStream, createReadStream } from 'fs' | ||
import fs from 'fs/promises' | ||
import JSZip from 'jszip' | ||
import path from 'path' | ||
import { FolderTree, FolderTreeFolder } from '../api/models/folderTree.js' | ||
|
||
export const getFiles = async (folderPath: string): Promise<string[]> => { | ||
const stat = await fs.stat(folderPath) | ||
|
||
if (stat.isDirectory()) { | ||
const files = await fs.readdir(folderPath) | ||
const promises = files.map((file) => getFiles(path.join(folderPath, file))) | ||
const allFiles = await Promise.all(promises) | ||
return allFiles.flat() | ||
} else { | ||
return [folderPath] | ||
} | ||
} | ||
|
||
export const createWriteStreamAdapter = ( | ||
nodeWriteStream: WriteStream, | ||
): WritableStream<Uint8Array> => { | ||
return new WritableStream({ | ||
write(chunk) { | ||
return new Promise((resolve, reject) => { | ||
nodeWriteStream.write(chunk, (err) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
}) | ||
}, | ||
close() { | ||
nodeWriteStream.end() | ||
}, | ||
abort(err) { | ||
nodeWriteStream.destroy(err) | ||
}, | ||
}) | ||
} | ||
|
||
const addFilesFromFilepathsToZip = ( | ||
folder: JSZip, | ||
folderNode: FolderTreeFolder, | ||
files: Record<string, string>, | ||
) => { | ||
folderNode.children.forEach((child) => { | ||
if (child.type === 'file') { | ||
const file = files[child.id] | ||
if (typeof file === 'string') { | ||
folder.file(child.name, createReadStream(file)) | ||
} else { | ||
folder.file(child.name, file) | ||
} | ||
} else if (child.type === 'folder') { | ||
const subFolder = folder.folder(child.name) | ||
if (!subFolder) { | ||
throw new Error('Failed to create folder in zip') | ||
} | ||
addFilesFromFilepathsToZip(subFolder, child as FolderTreeFolder, files) | ||
} | ||
}) | ||
} | ||
|
||
export const constructZipFromTreeAndFileSystemPaths = async ( | ||
tree: FolderTree, | ||
files: Record<string, string>, | ||
) => { | ||
if (tree.type === 'file') { | ||
throw new Error('Cannot construct zip from file') | ||
} | ||
|
||
const zip = new JSZip() | ||
addFilesFromFilepathsToZip(zip, tree as FolderTreeFolder, files) | ||
|
||
return zip.generateAsync({ type: 'blob' }) | ||
} |
Oops, something went wrong.