Skip to content

Commit

Permalink
cli updated
Browse files Browse the repository at this point in the history
  • Loading branch information
wpdas committed Mar 6, 2024
1 parent 04a6e6e commit e4cdbea
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 3,809 deletions.
8 changes: 6 additions & 2 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const distFolder = process.env.DIST_FOLDER || "build";
// Main function to orchestrate the build script
async function build() {
create_dist(distFolder);
process_dist(distFolder);
process_dist();
generate_data_json();
}

Expand Down Expand Up @@ -98,7 +98,11 @@ function process_dist() {
fileBundleBody = mimify(fileBundleBody);

// Note: Save unified file
fs.writeFileSync(path.join(`./build/${finalFileName}.jsx`), fileBundleBody);
// Note: must save inside a ./src folder. This is the only folder bos-clir-rs recognizes
fs.writeFileSync(
path.join(`./build/src/${finalFileName}.jsx`),
fileBundleBody,
);
}

module.exports = {
Expand Down
52 changes: 7 additions & 45 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { deploy_app, deploy_data } = require("./deploy");
const { version, name, description } = require("../package.json");
const { Command } = require("commander");
const fs = require("fs");
const readline = require("readline");

const program = new Command();

Expand Down Expand Up @@ -34,12 +32,11 @@ async function run() {
program
.command("deploy")
.description("Deploy the project")
.argument("[string]", "app name")
.action((appName) => {
deployCLI(appName);
.action(() => {
deployCLI();
});
program
.command("upload")
.command("upload-data")
.description("Upload data to SocialDB")
.argument("[string]", "app name")
.action((appName) => {
Expand All @@ -49,47 +46,12 @@ async function run() {
program.parse();
}

function deployCLI(appName) {
appSelectorCLI(deploy_app, appName);
}

function uploadDataCLI(appName) {
appSelectorCLI(deploy_data, appName);
function deployCLI() {
deploy_app();
}

function appSelectorCLI(callback, specifiedAppFolder) {
const appFolders = fs.readdirSync("./apps");

// Check if appFolder is provided as a command line argument
if (specifiedAppFolder) {
if (appFolders.includes(specifiedAppFolder)) {
callback(specifiedAppFolder);
return;
}
console.error("Invalid app name specified.");
}

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

console.log("Please select an app:");
appFolders.forEach((folder, index) => {
console.log(`${index + 1}. ${folder}`);
});

rl.question("Enter the number of the app you want to use: ", (answer) => {
const appIndex = parseInt(answer, 10) - 1;
if (appIndex >= 0 && appIndex < appFolders.length) {
const appFolder = appFolders[appIndex];
callback(appFolder);
rl.close();
} else {
console.error("Invalid selection. Exiting.");
rl.close();
}
});
function uploadDataCLI() {
deploy_data();
}

module.exports = {
Expand Down
13 changes: 10 additions & 3 deletions lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const distFolder = process.env.DIST_FOLDER || "build";
function generate_data_json() {
let fileContent = fs.readFileSync(path.join(".", "bos.config.json"), "utf8");

fileContent = removeComments(fileContent).replace(/\s/g, ""); // remove comments and spaces
fileContent = removeComments(fileContent); // remove comments and spaces
console.log("FOOO:", fileContent);
fileContent = JSON.parse(fileContent);

// prepare fields for data.json structure
Expand All @@ -25,7 +26,12 @@ function generate_data_json() {
});
}

if (key === "isIndex" || key === "account" || key === "options") {
if (
key === "isIndex" ||
key === "account" ||
key === "options" ||
key === "tags"
) {
return;
}

Expand All @@ -43,7 +49,8 @@ function generate_data_json() {
},
};

const dataPath = path.join(".", distFolder, "data.json");
// Note: must save inside a ./src folder. This is the only folder bos-clir-rs recognizes
const dataPath = path.join(".", distFolder, "src", "data.json");

if (!fs.existsSync(dataPath)) {
fs.mkdirSync(path.dirname(dataPath), { recursive: true });
Expand Down
32 changes: 14 additions & 18 deletions lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ const { read_bos_config } = require("./config");
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const { log } = require("./utils");

const distFolder = process.env.DIST_FOLDER || "build";

// TODO: need tests
function deploy_app(appFolder) {
const config = read_bos_config(appFolder);
function deploy_app() {
const config = read_bos_config();
const account = config.account;

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

Expand All @@ -33,28 +31,26 @@ function deploy_app(appFolder) {

try {
execSync(command, {
cwd: path.join(distFolder, appFolder),
cwd: path.join(distFolder),
stdio: "inherit",
}).toString();
console.log(`Deployed ${appFolder}`);
log.sucess(`DApp ${config.name} Deployed to NEAR BOS.`);
} catch (error) {
console.error(`Error deploying ${appFolder} widgets:\n${error.message}`);
log.error(`Error deploying dApp:\n${error.message}`);
}
}

function deploy_data(appFolder) {
const config = read_bos_config(appFolder);
function deploy_data() {
const config = read_bos_config();
const account = config.account;

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

const dataJSON = fs.readFileSync(
path.join(distFolder, appFolder, "data.json"),
path.join(distFolder, "src", "data.json"),
"utf8",
);
const args = {
Expand Down Expand Up @@ -89,12 +85,12 @@ function deploy_data(appFolder) {

try {
execSync(command, {
cwd: path.join(distFolder, appFolder),
cwd: path.join(distFolder, "src"),
stdio: "inherit",
}).toString();
console.log(`Uploaded data for ${appFolder}`);
console.log(`Uploaded data for ${config.name}`);
} catch (error) {
console.error(`Error uploading data for ${appFolder}:\n${error.message}`);
console.error(`Error uploading data:\n${error.message}`);
}
}

Expand Down
15 changes: 8 additions & 7 deletions lib/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ async function dev(opts) {
setTimeout(() => {
log.log("");
log.info(
[
"Watching for changes in the following folders:",
" - ./apps",
" - ./modules",
].join("\n"),
["Watching for changes in the following folders:", " - ./src"].join(
"\n",
),
);
}, 500);
}
Expand All @@ -78,7 +76,7 @@ function generateDevJson() {
});

const dataJSON = JSON.parse(
fs.readFileSync(path.join(".", distFolder, "data.json"), "utf8"),
fs.readFileSync(path.join(".", distFolder, "src", "data.json"), "utf8"),
);
devJson.data = { [appConfig.account]: dataJSON };

Expand All @@ -87,10 +85,13 @@ function generateDevJson() {
let widgetPath = file
.replace(path.join(".", distFolder), "")
.replace(path.extname(file), "");

let widgetKey = `${appConfig.account}/widget/${widgetPath
.slice(1)
.split(path.sep)
.join(".")}`;
.join(".")
.replace("src.", "")}`;

devJson.components[widgetKey] = { code: fileContent };
});

Expand Down
82 changes: 0 additions & 82 deletions lib/tools/routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,85 +95,3 @@ export const RouteLink = ({ to, children }) => {
};

export const createRoute = (path, component) => ({ path, component });

// TypeScript model
// import { createStore, useStore } from "../../";
// import { props, useEffect } from "../../bos";

// export type Route = {
// path: string;
// component: () => JSX.Element;
// };

// // built in route store
// createStore("alem:routes", { activeRoute: "", routes: [] });
// const useAlemLibRoutesStore = () => useStore("alem:routes");

// type RoutesProps = {
// routes: Route[];
// };

// const Routes = ({ routes }: RoutesProps) => {
// const { activeRoute, update } = useAlemLibRoutesStore();

// useEffect(() => {
// // BOS.props
// const bosProps = props;

// if (routes) {
// update({
// // list routes
// routes: routes.map((route) => route.path),
// // path= has priority
// ...(bosProps.path ? { activeRoute: bosProps.path } : {}),
// });
// }
// }, []);

// // Default route
// if (activeRoute === "") {
// const Component = routes[0].component;
// return <Component />;
// }

// // Route by route path
// const Component = routes.find(
// (route) => route.path === activeRoute,
// )?.component;
// if (Component) {
// return <Component />;
// }

// // Empty
// return "";
// };

// export default Routes;

// // go programatically to a new route
// // NOTE: removed for now to avoid confusion
// // export const navigate = (routePath: string) => {
// // const { routes, update } = useAlemLibRoutesStore();

// // if (routes.includes(routePath)) {
// // update({ activeRoute: routePath });
// // }
// // };

// // Link
// type RouteLinkProps = {
// to: string;
// children: JSX.Element;
// };
// export const RouteLink = ({ to, children }: RouteLinkProps) => {
// return (
// <a
// style={{ cursor: "pointer", textDecoration: "none" }}
// href={`?path=${to}`}
// >
// {children}
// </a>
// );
// };

// const createRoute = (path: string, component: () => JSX.Element) => ({path, component})
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ function for_rfile(folder, extensions, fn) {
* @param {string} distFolder
*/
function create_dist(distFolder) {
const distPath = path.join(".", distFolder);
// NOTE: Should save file to build/src. bos-cli-rs recognizes components inside ./src only
const distPath = path.join(".", distFolder, "src");
try {
if (fs.existsSync(distPath)) {
fs.rmSync(distPath, { recursive: true });
Expand Down
Loading

0 comments on commit e4cdbea

Please sign in to comment.