|
|
|
import np from 'path'
|
|
|
|
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
|
|
|
|
C step.desc
|
|
|
|
let result = step.run!
|
|
|
|
C "[ {result.status ? "OK" : "FAILED"} ] {result.message}"
|
|
|
|
|
|
|
|
def addProRouterViewsPlugin()
|
|
|
|
const configFile = file "vite.config.js"
|
|
|
|
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"
|
|
|
|
else
|
|
|
|
# import pro-router-imba2/views-plugin
|
|
|
|
nfs.writeFileSync(configFile, configContent.replace("imba()","proRouterViews(), imba()"))
|
|
|
|
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 = 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"
|
|
|
|
else
|
|
|
|
return status: false, message: "file already exist"
|
|
|
|
else
|
|
|
|
try
|
|
|
|
const routerContent = read file('node_modules/pro-router-imba2/src/router.imba')
|
|
|
|
nfs.writeFileSync(destinationFile, routerContent.replace("./", 'pro-router-imba2'))
|
|
|
|
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(), importRouter()])
|
|
|
|
else
|
|
|
|
C("For initial configuration of pro router use command: pro-router init")
|