-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merges the 1.2.0 branch, which includes changes and features for VLE, into main. --------- Co-authored-by: Alexis Sanehisa <[email protected]> Co-authored-by: Jacob <[email protected]> Co-authored-by: briantstephan <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Brian Baugher <[email protected]> Co-authored-by: Ben Life <[email protected]>
- Loading branch information
1 parent
07b9600
commit 34ed444
Showing
32 changed files
with
1,903 additions
and
139 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
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
32 changes: 32 additions & 0 deletions
32
packages/pages/src/common/src/parsers/puckConfigParser.test.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,32 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import fs from "node:fs"; | ||
import { addDataToPuckConfig } from "./puckConfigParser.js"; | ||
|
||
describe("addDataToPuckConfig", () => { | ||
it("should throw an error if the filepath is invalid", () => { | ||
expect(() => addDataToPuckConfig("fileName", "invalid/filepath")).toThrow( | ||
'Filepath "invalid/filepath" is invalid.' | ||
); | ||
}); | ||
|
||
it("correctly adds new config to the puck config file", () => { | ||
try { | ||
fs.writeFileSync( | ||
"test.tsx", | ||
`export const componentRegistry = new Map<string, Config<any>>([ | ||
["location", locationConfig], | ||
]);` | ||
); | ||
addDataToPuckConfig("foo", "test.tsx"); | ||
const modifiedContent = fs.readFileSync("test.tsx", "utf-8"); | ||
expect(modifiedContent).toContain('["foo", fooConfig]'); | ||
expect(modifiedContent).toContain( | ||
`export const fooConfig: Config<FooProps>` | ||
); | ||
} finally { | ||
if (fs.existsSync("test.tsx")) { | ||
fs.unlinkSync("test.tsx"); | ||
} | ||
} | ||
}); | ||
}); |
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,44 @@ | ||
import fs from "node:fs"; | ||
import SourceFileParser, { createTsMorphProject } from "./sourceFileParser.js"; | ||
import { newConfig } from "../../../scaffold/template/sampleTemplates.js"; | ||
import { SyntaxKind } from "ts-morph"; | ||
|
||
/** | ||
* Adds variables to the puck config file and adds the new config to | ||
* the exported map. | ||
* @param fileName template name with invalid chars and spaces removed | ||
* @param filepath /src/ve.config.tsx | ||
*/ | ||
export function addDataToPuckConfig(fileName: string, filepath: string) { | ||
if (!fs.existsSync(filepath)) { | ||
throw new Error(`Filepath "${filepath}" is invalid.`); | ||
} | ||
const parser = new SourceFileParser(filepath, createTsMorphProject()); | ||
|
||
const puckConfigsStatement = parser.getVariableStatement("componentRegistry"); | ||
|
||
const formattedTemplateName = | ||
fileName.charAt(0).toUpperCase() + fileName.slice(1); | ||
|
||
const puckConfigsStartLocation = puckConfigsStatement.getStart(); | ||
parser.insertStatement( | ||
newConfig(formattedTemplateName, fileName), | ||
puckConfigsStartLocation | ||
); | ||
|
||
const puckConfigsDeclaration = | ||
parser.getVariableDeclaration("componentRegistry"); | ||
const puckConfigsInitializer = puckConfigsDeclaration.getInitializer(); | ||
if ( | ||
puckConfigsInitializer && | ||
puckConfigsInitializer.getKind() === SyntaxKind.NewExpression | ||
) { | ||
const newExpression = puckConfigsInitializer; | ||
const puckConfigsArray = newExpression.getFirstChildByKindOrThrow( | ||
SyntaxKind.ArrayLiteralExpression | ||
); | ||
puckConfigsArray.addElement(`["${fileName}", ${fileName}Config]`); | ||
} | ||
parser.format(); | ||
parser.save(); | ||
} |
Oops, something went wrong.