From 6402b76ea84312cb8c32623ceb9aff4203631cff Mon Sep 17 00:00:00 2001 From: Marek Piasecki Date: Sun, 15 Jan 2023 18:23:22 +0100 Subject: [PATCH] one more step to pro-router init --- bin/pro-router.imba | 36 ++++++++++++++++++++++++++++-------- src/index.js | 5 ++++- src/main.imba | 2 -- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/bin/pro-router.imba b/bin/pro-router.imba index c45cfb8..a8ab791 100644 --- a/bin/pro-router.imba +++ b/bin/pro-router.imba @@ -3,6 +3,7 @@ import nfs from 'fs' C = console.log def file(path) do np.join (__dirname, path) +def read(file) do try nfs.readFileSync(file, 'utf-8') def run(steps) for step in steps @@ -12,43 +13,62 @@ def run(steps) def addProRouterViewsPlugin() const configFile = file "vite.config.js" - const configContent = try nfs.readFileSync(configFile, 'utf-8') + const configContent = read configFile return { desc: "Adding proRouterViews() plugin to vite.config.js", run: do if configContent if /proRouterViews\(\)/.test(configContent) - return status: true, message: "Plugin already there" + return status: true, message: "plugin already there" else # import pro-router-imba2/views-plugin nfs.writeFileSync(configFile, configContent.replace("imba()","proRouterViews(), imba()")) - return status: true, message: "Config updated" + return status: true, message: "config updated" else return status: false, message: "vite.config.js not found in the project" } def generateRouterFile() const destinationFile = file 'src/router.imba' - const destinationContent = try nfs.readFileSync(destinationFile, 'utf-8') + const destinationContent = read destinationFile return { desc: "Generating src/router.imba file" run: do if destinationContent if /R\.init/.test(destinationContent) - return status: true, message: "router seems to be ALREADY on place" + return status: true, message: "router SEEMS to be already on place" else return status: false, message: "file already exist" else try - const routerContent = nfs.readFileSync(file('node_modules/pro-router-imba2/src/router.imba'), 'utf-8') + const routerContent = read file('node_modules/pro-router-imba2/src/router.imba') nfs.writeFileSync(destinationFile, routerContent.replace("./", 'pro-router-imba2')) - nfs.cp source: 'node_modules/pro-router-imba2/src/router.imba', destination: './src/' return status: true, message: "generated successfuly" catch return status: false, message: 'unable to copy router.imba template' } +def importRouter + const indexFile = file 'src/index.js' + const indexContent = read indexFile + + return { + desc: "Adding import definition to src/index.js" + run: do + if indexContent + if /import '\.\/router\.imba'/.test(indexContent) + return status: true, message: "import definition already there" + else + try + nfs.writeFileSync indexFile, "import './router.imba'\n" + indexContent + return status: true, message: "definition added successfuly" + catch + return status: false, message: "failed during writing to file" + else + return status: false, message: 'no such file or directory' + } + if process.argv.slice(2) == "init" - run([addProRouterViewsPlugin(), generateRouterFile()]) + run([addProRouterViewsPlugin(), generateRouterFile(), importRouter()]) else C("For initial configuration of pro router use command: pro-router init") \ No newline at end of file diff --git a/src/index.js b/src/index.js index e6824ab..4273cd6 100644 --- a/src/index.js +++ b/src/index.js @@ -1 +1,4 @@ -import "./main.imba" \ No newline at end of file +import './router.imba' +if(import.meta.env.MODE == 'development'){ + import("./main.imba") +} \ No newline at end of file diff --git a/src/main.imba b/src/main.imba index ae791f7..b73d4f0 100644 --- a/src/main.imba +++ b/src/main.imba @@ -1,5 +1,3 @@ -import './router.imba' - tag App v for v in R.views