Browse Source

one more step to pro-router init

master
Marek Piasecki 2 years ago
parent
commit
6402b76ea8
  1. 36
      bin/pro-router.imba
  2. 5
      src/index.js
  3. 2
      src/main.imba

36
bin/pro-router.imba

@ -3,6 +3,7 @@ import nfs from 'fs'
C = console.log C = console.log
def file(path) do np.join (__dirname, path) def file(path) do np.join (__dirname, path)
def read(file) do try nfs.readFileSync(file, 'utf-8')
def run(steps) def run(steps)
for step in steps for step in steps
@ -12,43 +13,62 @@ def run(steps)
def addProRouterViewsPlugin() def addProRouterViewsPlugin()
const configFile = file "vite.config.js" const configFile = file "vite.config.js"
const configContent = try nfs.readFileSync(configFile, 'utf-8') const configContent = read configFile
return { return {
desc: "Adding proRouterViews() plugin to vite.config.js", desc: "Adding proRouterViews() plugin to vite.config.js",
run: do run: do
if configContent if configContent
if /proRouterViews\(\)/.test(configContent) if /proRouterViews\(\)/.test(configContent)
return status: true, message: "Plugin already there" return status: true, message: "plugin already there"
else else
# import pro-router-imba2/views-plugin # import pro-router-imba2/views-plugin
nfs.writeFileSync(configFile, configContent.replace("imba()","proRouterViews(), imba()")) nfs.writeFileSync(configFile, configContent.replace("imba()","proRouterViews(), imba()"))
return status: true, message: "Config updated" return status: true, message: "config updated"
else else
return status: false, message: "vite.config.js not found in the project" return status: false, message: "vite.config.js not found in the project"
} }
def generateRouterFile() def generateRouterFile()
const destinationFile = file 'src/router.imba' const destinationFile = file 'src/router.imba'
const destinationContent = try nfs.readFileSync(destinationFile, 'utf-8') const destinationContent = read destinationFile
return { return {
desc: "Generating src/router.imba file" desc: "Generating src/router.imba file"
run: do run: do
if destinationContent if destinationContent
if /R\.init/.test(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 else
return status: false, message: "file already exist" return status: false, message: "file already exist"
else else
try 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.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" return status: true, message: "generated successfuly"
catch catch
return status: false, message: 'unable to copy router.imba template' 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" if process.argv.slice(2) == "init"
run([addProRouterViewsPlugin(), generateRouterFile()]) run([addProRouterViewsPlugin(), generateRouterFile(), importRouter()])
else else
C("For initial configuration of pro router use command: pro-router init") C("For initial configuration of pro router use command: pro-router init")

5
src/index.js

@ -1 +1,4 @@
import "./main.imba" import './router.imba'
if(import.meta.env.MODE == 'development'){
import("./main.imba")
}

2
src/main.imba

@ -1,5 +1,3 @@
import './router.imba'
tag App tag App
<self[d:bbox]> <self[d:bbox]>
<pro-ref view=v> v for v in R.views <pro-ref view=v> v for v in R.views

Loading…
Cancel
Save