Skip to content
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

implement data.json serving #9

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/dummyapp/widget/hello.jsx
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
const ss = Social.get("dummydata/dummy/dummy")
Copy link
Contributor

@elliotBraem elliotBraem Aug 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change this to "dummy/dummydata/dummy/dummy" in order to get it to work, i.e was missing the "accountId"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Fixed!


console.log("ss", ss)

return <>{ss}</>

return /*__@replace:hello__*/;
28 changes: 17 additions & 11 deletions tools/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function readBosConfig(appFolder) {
const config = JSON.parse(configRaw);
if (!config.appAccount) {
console.warn(
`WARNING: appAccount not found in ${appFolder}/bos.config.json, build script may work but dev requires it`,
`WARNING: appAccount not found in ${appFolder}/bos.config.json, build script may work but dev requires it`
);
}
return config;
Expand Down Expand Up @@ -82,7 +82,7 @@ function processFile(filePath, aliases, appAccount) {
// walk through each app folder
function processDistFolder(appFolder) {
const files = glob.sync(
`./${distFolder}/${appFolder}/**/*.{js,jsx,ts,tsx,json}`,
`./${distFolder}/${appFolder}/**/*.{js,jsx,ts,tsx,json}`
);

const config = readBosConfig(appFolder);
Expand Down Expand Up @@ -146,7 +146,7 @@ function generateDataJson(appFolder) {
fileContent = processCommentCommands(
fileContent,
config.aliases,
config.appAccount,
config.appAccount
);
if (noStringifyJsonFiles(fileContent)) {
fileContent = JSON.parse(removeComments(fileContent));
Expand Down Expand Up @@ -183,14 +183,19 @@ function generateDataJson(appFolder) {

// generate the development json from the apps widgets
function generateDevJson(appFolder) {
let devJson = { components: {} };
let devJson = { components: {}, data: {} };

const appConfig = readBosConfig(appFolder);
if (!appConfig.appAccount) {
return devJson;
}
const widgetFiles = glob.sync(
`./${distFolder}/${appFolder}/src/**/*.{js,jsx,ts,tsx}`,
`./${distFolder}/${appFolder}/src/**/*.{js,jsx,ts,tsx}`
);
const dataJSON = JSON.parse(
fs.readFileSync(`./${distFolder}/${appFolder}/data.json`, "utf8")
);
devJson.data = { [appConfig.appAccount]: dataJSON };

widgetFiles.forEach((file) => {
let fileContent = fs.readFileSync(file, "utf8");
Expand Down Expand Up @@ -224,19 +229,20 @@ function serveDevJson() {
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, Content-Length, X-Requested-With",
"Content-Type, Authorization, Content-Length, X-Requested-With"
);

next();
});

app.get("/", (req, res) => {
let devJson = { components: {} };
let devJson = { components: {}, data: {} };
const appFolders = fs.readdirSync("./apps");

for (const appFolder of appFolders) {
let appDevJson = generateDevJson(appFolder);
devJson.components = { ...devJson.components, ...appDevJson.components };
devJson.data = { ...devJson.data, ...appDevJson.data };
}

res.json(devJson);
Expand All @@ -248,7 +254,7 @@ function serveDevJson() {
"Server running at " + "http://127.0.0.1:4040/" + "\n|\n|",
"To use the local widgets, go to " + "https://near.org/flags" + "\n|",
"and paste the server link above.\n|",
"--------------------------------------------\\\n",
"--------------------------------------------\\\n"
);
});
}
Expand All @@ -260,7 +266,7 @@ function deployApp(appFolder) {

if (!appAccount) {
console.error(
`App account is not defined for ${appFolder}. Skipping deployment.`,
`App account is not defined for ${appFolder}. Skipping deployment.`
);
return;
}
Expand All @@ -284,14 +290,14 @@ function uploadData(appFolder) {

if (!appAccount) {
console.error(
`App account is not defined for ${appFolder}. Skipping data upload.`,
`App account is not defined for ${appFolder}. Skipping data upload.`
);
return;
}

const dataJSON = fs.readFileSync(
path.join(distFolder, appFolder, "data.json"),
"utf8",
"utf8"
);
const args = {
data: {
Expand Down
3 changes: 3 additions & 0 deletions tools/lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,19 @@ describe("generateDevJson", () => {
// first, build the app
await generateDistFolder("testAppFolder");
await processDistFolder("testAppFolder");
await generateDataJson("testAppFolder");

const devJson = generateDevJson("testAppFolder");

// verify the structure of the devJson
expect(devJson).toHaveProperty("components");
expect(devJson).toHaveProperty("data");

// verify the content of the component file
expect(devJson.components["test/widget/Layout.Modal.index"].code).toEqual(
'return console.log("testAlias");<Widget src="nui.near/widget/index" />',
);
expect(devJson.data).toEqual({ hello: "Hello, World!", test: "{}" });
});
});

Expand Down
Loading