Skip to content

Commit

Permalink
Fusion
Browse files Browse the repository at this point in the history
  • Loading branch information
breck7 committed Nov 28, 2024
1 parent 1449ba4 commit 104e025
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 78 deletions.
2 changes: 1 addition & 1 deletion builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ kitchen
particle
swim
testRacer
particleFileSystem
fusion
parsers
utils
particleComponentFramework`.split("\n")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env ts-node

const { Particle } = require("../products/Particle.js")
const { ParticleFileSystem } = require("../products/ParticleFileSystem.js")
const { Fusion } = require("../products/Fusion.js")
const { TestRacer } = require("../products/TestRacer.js")
const path = require("path")
import { particlesTypes } from "../products/particlesTypes"

const testParticles: particlesTypes.testParticles = {}

testParticles.disk = equal => {
const tfs = new ParticleFileSystem()
const tfs = new Fusion()
// Arrange/Act/Assert
equal(tfs.assembleFile(path.join(__dirname, "..", "readme.scroll")).afterImportPass.length > 0, true)
equal(tfs.fuseFile(path.join(__dirname, "..", "readme.scroll")).fused.length > 0, true)
}

const stripImported = (str: string) => {
Expand All @@ -28,21 +28,21 @@ testParticles.inMemory = equal => {
"/nested/test": "ciao",
"/nested/deep/relative": "import ../../hello\nimport ../test"
}
const tfs = new ParticleFileSystem(files)
const tfs = new Fusion(files)
equal(tfs.dirname("/"), "/")
equal(stripImported(tfs.assembleFile("/main").afterImportPass), "world\nciao")
equal(stripImported(tfs.assembleFile("/nested/deep/relative").afterImportPass), "world\nciao")
equal(tfs.assembleFile("/main").exists, true)
equal(stripImported(tfs.fuseFile("/main").fused), "world\nciao")
equal(stripImported(tfs.fuseFile("/nested/deep/relative").fused), "world\nciao")
equal(tfs.fuseFile("/main").exists, true)
}

testParticles.nonExistant = equal => {
// Arrange/Act/Assert
const files = {
"/main": "import env"
}
const tfs = new ParticleFileSystem(files)
const result = tfs.assembleFile("/main")
equal(stripImported(result.afterImportPass), "")
const tfs = new Fusion(files)
const result = tfs.fuseFile("/main")
equal(stripImported(result.fused), "")
equal(result.exists, false)
}

Expand All @@ -59,10 +59,10 @@ This is my content
"/header.scroll": "printTitle",
"/footer.scroll": "The end."
}
const tfs = new ParticleFileSystem(files)
const result = tfs.assembleFile("/hello.scroll")
equal(result.afterImportPass.includes("This is my content"), true)
equal(result.afterImportPass.includes("The end"), false)
const tfs = new Fusion(files)
const result = tfs.fuseFile("/hello.scroll")
equal(result.fused.includes("This is my content"), true)
equal(result.fused.includes("The end"), false)
equal(result.footers[0], "The end.")
}

Expand All @@ -75,11 +75,11 @@ testParticles.quickImports = equal => {
"/nested/a": "test.scroll",
"/nested/deep/relative": "../../hello.scroll\n../test.scroll"
}
const tfs = new ParticleFileSystem(files)
const tfs = new Fusion(files)
equal(tfs.dirname("/"), "/")
equal(stripImported(tfs.assembleFile("/nested/a").afterImportPass), "ciao")
equal(stripImported(tfs.assembleFile("/main").afterImportPass), "world\nciao")
equal(stripImported(tfs.assembleFile("/nested/deep/relative").afterImportPass), "world\nciao")
equal(stripImported(tfs.fuseFile("/nested/a").fused), "ciao")
equal(stripImported(tfs.fuseFile("/main").fused), "world\nciao")
equal(stripImported(tfs.fuseFile("/nested/deep/relative").fused), "world\nciao")
}

/*NODE_JS_ONLY*/ if (!module.parent) TestRacer.testSingleFile(__filename, testParticles)
Expand Down
34 changes: 17 additions & 17 deletions particleFileSystem/ParticleFileSystem.ts → fusion/Fusion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ interface OpenedFile {
stats: any // https://nodejs.org/api/fs.html#class-fsstats
}

interface AssembledFile {
afterImportPass: string // codeWithoutImportsNorParserDefinitions
interface FusedFile {
fused: string // codeWithoutImportsNorParserDefinitions
footers: string[]
importFilePaths: string[]
isImportOnly: boolean
Expand Down Expand Up @@ -133,7 +133,7 @@ class MemoryWriter implements Storage {
}
}

class ParticleFileSystem implements Storage {
class Fusion implements Storage {
constructor(inMemoryFiles: particlesTypes.diskMap) {
if (inMemoryFiles) this._storage = new MemoryWriter(inMemoryFiles)
else this._storage = new DiskWriter()
Expand Down Expand Up @@ -174,7 +174,7 @@ class ParticleFileSystem implements Storage {
private _storage: Storage
private _particleCache: { [filepath: string]: typeof Particle } = {}
private _parserCache: { [concatenatedFilepaths: string]: any } = {}
private _expandedImportCache: { [filepath: string]: AssembledFile } = {}
private _expandedImportCache: { [filepath: string]: FusedFile } = {}
private _parsersExpandersCache: { [filepath: string]: boolean } = {}

private _getFileAsParticles(absoluteFilePath: string) {
Expand All @@ -185,7 +185,7 @@ class ParticleFileSystem implements Storage {
return _particleCache[absoluteFilePath]
}

private _assembleFile(absoluteFilePath: string) {
private _fuseFile(absoluteFilePath: string) {
const { _expandedImportCache } = this
if (_expandedImportCache[absoluteFilePath]) return _expandedImportCache[absoluteFilePath]

Expand All @@ -208,8 +208,8 @@ class ParticleFileSystem implements Storage {
if (this._doesFileHaveParsersDefinitions(absoluteFilePath)) filepathsWithParserDefinitions.push(absoluteFilePath)

if (!importRegex.test(code))
return <AssembledFile>{
afterImportPass: code,
return <FusedFile>{
fused: code,
footers: [],
isImportOnly,
importFilePaths: [],
Expand All @@ -226,21 +226,21 @@ class ParticleFileSystem implements Storage {
.forEach(importParticle => {
const relativeFilePath = importParticle.getLine().replace("import ", "")
const absoluteImportFilePath = this.join(folder, relativeFilePath)
const expandedFile = this._assembleFile(absoluteImportFilePath)
const expandedFile = this._fuseFile(absoluteImportFilePath)
importFilePaths.push(absoluteImportFilePath)
importFilePaths = importFilePaths.concat(expandedFile.importFilePaths)
const exists = this.exists(absoluteImportFilePath)
importParticle.setLine("imported " + relativeFilePath)
importParticle.set("exists", `${exists}`)
footers = footers.concat(expandedFile.footers)
if (importParticle.has("footer")) footers.push(expandedFile.afterImportPass)
else importParticle.insertLinesAfter(expandedFile.afterImportPass)
if (importParticle.has("footer")) footers.push(expandedFile.fused)
else importParticle.insertLinesAfter(expandedFile.fused)
})

_expandedImportCache[absoluteFilePath] = {
importFilePaths,
isImportOnly,
afterImportPass: particle.toString(),
fused: particle.toString(),
footers,
exists: !importFilePaths.some(file => !this.exists(file)),
filepathsWithParserDefinitions: importFilePaths.filter((filename: string) => this._doesFileHaveParsersDefinitions(filename)).concat(filepathsWithParserDefinitions)
Expand Down Expand Up @@ -299,15 +299,15 @@ class ParticleFileSystem implements Storage {
return _parserCache[key]
}

assembleFile(absoluteFilePath: string, defaultParserCode?: string): AssembledFile {
const assembledFile = this._assembleFile(absoluteFilePath)
fuseFile(absoluteFilePath: string, defaultParserCode?: string): FusedFile {
const fusedFile = this._fuseFile(absoluteFilePath)

if (!defaultParserCode) return assembledFile
if (!defaultParserCode) return fusedFile

// BUILD CUSTOM COMPILER, IF THERE ARE CUSTOM PARSERS NODES DEFINED
if (assembledFile.filepathsWithParserDefinitions.length) assembledFile.parser = this.getParser(assembledFile.filepathsWithParserDefinitions, defaultParserCode).parser
return assembledFile
if (fusedFile.filepathsWithParserDefinitions.length) fusedFile.parser = this.getParser(fusedFile.filepathsWithParserDefinitions, defaultParserCode).parser
return fusedFile
}
}

export { ParticleFileSystem }
export { Fusion }
7 changes: 7 additions & 0 deletions fusion/readme.scroll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Fusion

Extends Particles with URL imports.

Fusion _fuses_ files together.


4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrollsdk",
"version": "95.0.1",
"version": "96.0.0",
"description": "This npm package includes the Particles class, the Parsers compiler-compiler, a Parsers IDE, and more, all implemented in Particles, Parsers, and TypeScript.",
"types": "./built/scrollsdk.node.d.ts",
"main": "./products/Particle.js",
Expand All @@ -25,7 +25,7 @@
"products/stump.nodejs.js",
"products/swarm.nodejs.js",
"products/TestRacer.js",
"products/ParticleFileSystem.js",
"products/Fusion.js",
"products/Particle.js",
"products/Utils.js"
],
Expand Down
2 changes: 1 addition & 1 deletion particle/Particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3094,7 +3094,7 @@ class Particle extends AbstractParticle {
return str ? indent + str.replace(/\n/g, indent) : ""
}

static getVersion = () => "95.0.1"
static getVersion = () => "96.0.0"

static fromDisk(path: string): Particle {
const format = this._getFileFormat(path)
Expand Down
3 changes: 0 additions & 3 deletions particleFileSystem/readme.scroll

This file was deleted.

10 changes: 5 additions & 5 deletions products.scroll
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ nodeProduct
insertLastLine module.exports = {Utils}
combineTypeScriptFiles utils/Utils.ts
nodeProduct
outputFileName ParticleFileSystem.js
insertLastLine module.exports = {ParticleFileSystem}
combineTypeScriptFiles particleFileSystem/ParticleFileSystem.ts
outputFileName Fusion.js
insertLastLine module.exports = {Fusion}
combineTypeScriptFiles fusion/Fusion.ts
nodeProduct
outputFileName Kitchen.node.js
combineTypeScriptFiles products/particlesTypes.ts kitchen/Kitchen.node.ts
Expand Down Expand Up @@ -61,8 +61,8 @@ browserProduct
outputFileName Utils.browser.js
combineTypeScriptFiles utils/Utils.ts
browserProduct
outputFileName ParticleFileSystem.browser.js
combineTypeScriptFiles particleFileSystem/ParticleFileSystem.ts
outputFileName Fusion.browser.js
combineTypeScriptFiles fusion/Fusion.ts
browserProduct
outputFileName Particle.browser.js
removeAll window.particlesTypes = particlesTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MemoryWriter {
return posix.join(...arguments)
}
}
class ParticleFileSystem {
class Fusion {
constructor(inMemoryFiles) {
this._particleCache = {}
this._parserCache = {}
Expand Down Expand Up @@ -114,7 +114,7 @@ class ParticleFileSystem {
}
return _particleCache[absoluteFilePath]
}
_assembleFile(absoluteFilePath) {
_fuseFile(absoluteFilePath) {
const { _expandedImportCache } = this
if (_expandedImportCache[absoluteFilePath]) return _expandedImportCache[absoluteFilePath]
let code = this.read(absoluteFilePath)
Expand All @@ -133,7 +133,7 @@ class ParticleFileSystem {
if (this._doesFileHaveParsersDefinitions(absoluteFilePath)) filepathsWithParserDefinitions.push(absoluteFilePath)
if (!importRegex.test(code))
return {
afterImportPass: code,
fused: code,
footers: [],
isImportOnly,
importFilePaths: [],
Expand All @@ -149,20 +149,20 @@ class ParticleFileSystem {
.forEach(importParticle => {
const relativeFilePath = importParticle.getLine().replace("import ", "")
const absoluteImportFilePath = this.join(folder, relativeFilePath)
const expandedFile = this._assembleFile(absoluteImportFilePath)
const expandedFile = this._fuseFile(absoluteImportFilePath)
importFilePaths.push(absoluteImportFilePath)
importFilePaths = importFilePaths.concat(expandedFile.importFilePaths)
const exists = this.exists(absoluteImportFilePath)
importParticle.setLine("imported " + relativeFilePath)
importParticle.set("exists", `${exists}`)
footers = footers.concat(expandedFile.footers)
if (importParticle.has("footer")) footers.push(expandedFile.afterImportPass)
else importParticle.insertLinesAfter(expandedFile.afterImportPass)
if (importParticle.has("footer")) footers.push(expandedFile.fused)
else importParticle.insertLinesAfter(expandedFile.fused)
})
_expandedImportCache[absoluteFilePath] = {
importFilePaths,
isImportOnly,
afterImportPass: particle.toString(),
fused: particle.toString(),
footers,
exists: !importFilePaths.some(file => !this.exists(file)),
filepathsWithParserDefinitions: importFilePaths.filter(filename => this._doesFileHaveParsersDefinitions(filename)).concat(filepathsWithParserDefinitions)
Expand Down Expand Up @@ -213,12 +213,12 @@ class ParticleFileSystem {
}
return _parserCache[key]
}
assembleFile(absoluteFilePath, defaultParserCode) {
const assembledFile = this._assembleFile(absoluteFilePath)
if (!defaultParserCode) return assembledFile
fuseFile(absoluteFilePath, defaultParserCode) {
const fusedFile = this._fuseFile(absoluteFilePath)
if (!defaultParserCode) return fusedFile
// BUILD CUSTOM COMPILER, IF THERE ARE CUSTOM PARSERS NODES DEFINED
if (assembledFile.filepathsWithParserDefinitions.length) assembledFile.parser = this.getParser(assembledFile.filepathsWithParserDefinitions, defaultParserCode).parser
return assembledFile
if (fusedFile.filepathsWithParserDefinitions.length) fusedFile.parser = this.getParser(fusedFile.filepathsWithParserDefinitions, defaultParserCode).parser
return fusedFile
}
}
window.ParticleFileSystem = ParticleFileSystem
window.Fusion = Fusion
26 changes: 13 additions & 13 deletions products/ParticleFileSystem.js → products/Fusion.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MemoryWriter {
return posix.join(...arguments)
}
}
class ParticleFileSystem {
class Fusion {
constructor(inMemoryFiles) {
this._particleCache = {}
this._parserCache = {}
Expand Down Expand Up @@ -122,7 +122,7 @@ class ParticleFileSystem {
}
return _particleCache[absoluteFilePath]
}
_assembleFile(absoluteFilePath) {
_fuseFile(absoluteFilePath) {
const { _expandedImportCache } = this
if (_expandedImportCache[absoluteFilePath]) return _expandedImportCache[absoluteFilePath]
let code = this.read(absoluteFilePath)
Expand All @@ -141,7 +141,7 @@ class ParticleFileSystem {
if (this._doesFileHaveParsersDefinitions(absoluteFilePath)) filepathsWithParserDefinitions.push(absoluteFilePath)
if (!importRegex.test(code))
return {
afterImportPass: code,
fused: code,
footers: [],
isImportOnly,
importFilePaths: [],
Expand All @@ -157,20 +157,20 @@ class ParticleFileSystem {
.forEach(importParticle => {
const relativeFilePath = importParticle.getLine().replace("import ", "")
const absoluteImportFilePath = this.join(folder, relativeFilePath)
const expandedFile = this._assembleFile(absoluteImportFilePath)
const expandedFile = this._fuseFile(absoluteImportFilePath)
importFilePaths.push(absoluteImportFilePath)
importFilePaths = importFilePaths.concat(expandedFile.importFilePaths)
const exists = this.exists(absoluteImportFilePath)
importParticle.setLine("imported " + relativeFilePath)
importParticle.set("exists", `${exists}`)
footers = footers.concat(expandedFile.footers)
if (importParticle.has("footer")) footers.push(expandedFile.afterImportPass)
else importParticle.insertLinesAfter(expandedFile.afterImportPass)
if (importParticle.has("footer")) footers.push(expandedFile.fused)
else importParticle.insertLinesAfter(expandedFile.fused)
})
_expandedImportCache[absoluteFilePath] = {
importFilePaths,
isImportOnly,
afterImportPass: particle.toString(),
fused: particle.toString(),
footers,
exists: !importFilePaths.some(file => !this.exists(file)),
filepathsWithParserDefinitions: importFilePaths.filter(filename => this._doesFileHaveParsersDefinitions(filename)).concat(filepathsWithParserDefinitions)
Expand Down Expand Up @@ -221,13 +221,13 @@ class ParticleFileSystem {
}
return _parserCache[key]
}
assembleFile(absoluteFilePath, defaultParserCode) {
const assembledFile = this._assembleFile(absoluteFilePath)
if (!defaultParserCode) return assembledFile
fuseFile(absoluteFilePath, defaultParserCode) {
const fusedFile = this._fuseFile(absoluteFilePath)
if (!defaultParserCode) return fusedFile
// BUILD CUSTOM COMPILER, IF THERE ARE CUSTOM PARSERS NODES DEFINED
if (assembledFile.filepathsWithParserDefinitions.length) assembledFile.parser = this.getParser(assembledFile.filepathsWithParserDefinitions, defaultParserCode).parser
return assembledFile
if (fusedFile.filepathsWithParserDefinitions.length) fusedFile.parser = this.getParser(fusedFile.filepathsWithParserDefinitions, defaultParserCode).parser
return fusedFile
}
}

module.exports = { ParticleFileSystem }
module.exports = { Fusion }
Loading

0 comments on commit 104e025

Please sign in to comment.