Skip to content

Commit

Permalink
Merge pull request #120 from sveltinio/kit-advanced-layout
Browse files Browse the repository at this point in the history
kit advanced layout
  • Loading branch information
indaco authored Sep 16, 2022
2 parents 53555be + 6799ae0 commit f514b24
Show file tree
Hide file tree
Showing 101 changed files with 794 additions and 569 deletions.
23 changes: 9 additions & 14 deletions cmd/addContent.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/sveltinio/sveltin/internal/composer"
sveltinerr "github.com/sveltinio/sveltin/internal/errors"
"github.com/sveltinio/sveltin/internal/markup"
"github.com/sveltinio/sveltin/internal/tpltypes"
"github.com/sveltinio/sveltin/resources"
"github.com/sveltinio/sveltin/utils"
)
Expand Down Expand Up @@ -111,7 +112,7 @@ func init() {

//=============================================================================

func promptContentName(fs afero.Fs, inputs []string, isSample bool, c *config.SveltinConfig) (*config.TemplateData, error) {
func promptContentName(fs afero.Fs, inputs []string, isSample bool, c *config.SveltinConfig) (*tpltypes.ContentData, error) {
contentType := Blank
if isSample {
contentType = Sample
Expand All @@ -133,7 +134,7 @@ func promptContentName(fs afero.Fs, inputs []string, isSample bool, c *config.Sv
return nil, err
}

return &config.TemplateData{
return &tpltypes.ContentData{
Name: utils.ToSlug(contentName),
Type: contentType,
Resource: contentResource,
Expand All @@ -148,7 +149,7 @@ func promptContentName(fs afero.Fs, inputs []string, isSample bool, c *config.Sv
return nil, err
}

return &config.TemplateData{
return &tpltypes.ContentData{
Name: utils.ToSlug(contentName),
Type: contentType,
Resource: contentResource,
Expand Down Expand Up @@ -176,7 +177,7 @@ func promptResourceList(fs afero.Fs, c *config.SveltinConfig) (string, error) {

//=============================================================================

func makeContentFolderStructure(folderName string, contentData *config.TemplateData) (*composer.Folder, error) {
func makeContentFolderStructure(folderName string, contentData *tpltypes.ContentData) (*composer.Folder, error) {
switch folderName {
case ContentFolder:
return createContentLocalFolder(contentData), nil
Expand All @@ -191,28 +192,22 @@ func makeContentFolderStructure(folderName string, contentData *config.TemplateD

//=============================================================================

func createContentLocalFolder(contentData *config.TemplateData) *composer.Folder {
func createContentLocalFolder(contentData *tpltypes.ContentData) *composer.Folder {
// GET FOLDER: content
contentFolder := cfg.fsManager.GetFolder(ContentFolder)

// NEW FOLDER content/<resource_name>/<content_name>
resourceContentFolder := cfg.fsManager.NewResourceContentFolder(contentData.Name, contentData.Resource)
resourceContentFolder := cfg.fsManager.NewResourceContentFolder(contentData)

// NEW FILE: content/<resource_name>/<content_name>/index.svx
contentFile := &composer.File{
Name: cfg.pathMaker.GetResourceContentFilename(),
TemplateID: contentData.Type,
TemplateData: &config.TemplateData{
Name: contentData.Name,
},
}
contentFile := cfg.fsManager.NewResourceContentFile(contentData)

resourceContentFolder.Add(contentFile)
contentFolder.Add(resourceContentFolder)
return contentFolder
}

func createStaticFolderStructure(contentData *config.TemplateData) *composer.Folder {
func createStaticFolderStructure(contentData *tpltypes.ContentData) *composer.Folder {
// GET FOLDER: static
staticFolder := cfg.fsManager.GetFolder(StaticFolder)
// NEW FOLDER static/resources
Expand Down
96 changes: 57 additions & 39 deletions cmd/addMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/sveltinio/sveltin/internal/composer"
sveltinerr "github.com/sveltinio/sveltin/internal/errors"
"github.com/sveltinio/sveltin/internal/markup"
"github.com/sveltinio/sveltin/internal/tpltypes"
"github.com/sveltinio/sveltin/resources"
"github.com/sveltinio/sveltin/utils"
)
Expand Down Expand Up @@ -71,11 +72,10 @@ func RunAddMetadataCmd(cmd *cobra.Command, args []string) {
mdType, err := promptMetadataType(metadataType)
utils.ExitIfError(err)

metadataTemplateData := &config.TemplateData{
metadataTemplateData := &tpltypes.MetadataData{
Name: mdName,
Resource: mdResource,
Type: mdType,
Config: cfg.sveltin,
}

headingText := fmt.Sprintf("Creating '%s' as metadata for the '%s' resource", metadataTemplateData.Name, metadataTemplateData.Resource)
Expand Down Expand Up @@ -207,16 +207,16 @@ func promptMetadataType(mdTypeFlag string) (string, error) {

//=============================================================================

func makeOrAddContentForMetadataToProjectStructure(folderName string, metadataTemaplateData *config.TemplateData) (*composer.Folder, error) {
func makeOrAddContentForMetadataToProjectStructure(folderName string, metadataData *tpltypes.MetadataData) (*composer.Folder, error) {
switch folderName {
case LibFolder:
return createOrAddContentForMetadataToLibLocalFolder(metadataTemaplateData), nil
return createOrAddContentForMetadataToLibLocalFolder(metadataData), nil
case ParamsFolder:
return createOrAddContentForMetadataToParamsLocalFolder(metadataTemaplateData), nil
return createOrAddContentForMetadataToParamsLocalFolder(metadataData), nil
case RoutesFolder:
return createOrAddContentForMetadataToRoutesLocalFolder(metadataTemaplateData), nil
return createOrAddContentForMetadataToRoutesLocalFolder(metadataData), nil
case ApiFolder:
return createOrAddContentForMetadataToApiLocalFolder(metadataTemaplateData), nil
return createOrAddContentForMetadataToApiLocalFolder(metadataData), nil
default:
err := errors.New("something went wrong: folder not found as mapped resource for sveltin projects")
return nil, sveltinerr.NewDefaultError(err)
Expand All @@ -225,16 +225,19 @@ func makeOrAddContentForMetadataToProjectStructure(folderName string, metadataTe

//=============================================================================

func createOrAddContentForMetadataToLibLocalFolder(metadataTemplateData *config.TemplateData) *composer.Folder {
func createOrAddContentForMetadataToLibLocalFolder(metadataData *tpltypes.MetadataData) *composer.Folder {
// NEW FILE: api<metadata_name>.ts file into src/lib/<resource_name> folder
cfg.log.Info("Lib files")
libFile := &composer.File{
Name: cfg.pathMaker.GetResourceLibFilename(metadataTemplateData.Name),
TemplateID: LibFolder,
TemplateData: metadataTemplateData,
Name: cfg.pathMaker.GetResourceLibFilename(metadataData.Name),
TemplateID: LibFolder,
TemplateData: &config.TemplateData{
Config: cfg.sveltin,
Metadata: metadataData,
},
}
// NEW FOLDER: src/lib/<resource_name>
resourceLibFolder := composer.NewFolder(metadataTemplateData.Resource)
resourceLibFolder := composer.NewFolder(metadataData.Resource)
resourceLibFolder.Add(libFile)

// GET FOLDER: src/lib folder
Expand All @@ -244,34 +247,40 @@ func createOrAddContentForMetadataToLibLocalFolder(metadataTemplateData *config.
return libFolder
}

func createOrAddContentForMetadataToParamsLocalFolder(metadataTemplateData *config.TemplateData) *composer.Folder {
func createOrAddContentForMetadataToParamsLocalFolder(metadataData *tpltypes.MetadataData) *composer.Folder {
cfg.log.Info("Parameters matchers")
// GET FOLDER: src/params folder
paramsFolder := cfg.fsManager.GetFolder(ParamsFolder)

// NEW FILE: src/params/<metadata_name>.js
metadataMatcherFile := &composer.File{
Name: fmt.Sprintf("%s%s", utils.ToSnakeCase(metadataTemplateData.Name), ".js"),
TemplateID: GenericMatcher,
TemplateData: metadataTemplateData,
Name: fmt.Sprintf("%s%s", utils.ToSnakeCase(metadataData.Name), ".js"),
TemplateID: GenericMatcher,
TemplateData: &config.TemplateData{
Config: cfg.sveltin,
Metadata: metadataData,
},
}
// Add file to folder
paramsFolder.Add(metadataMatcherFile)

return paramsFolder
}

func createOrAddContentForMetadataToRoutesLocalFolder(metadataTemaplateData *config.TemplateData) *composer.Folder {
func createOrAddContentForMetadataToRoutesLocalFolder(metadataData *tpltypes.MetadataData) *composer.Folder {
cfg.log.Info("Routes")
// NEW FOLDER: <metadata_name>
resourceMedatadaRoutesFolder := composer.NewFolder(metadataTemaplateData.Name)
resourceMedatadaRoutesFolder := composer.NewFolder(metadataData.Name)

// NEW FILE: src/routes/<resource_name>/<metadata_name>/{+page.svelte, +page.server.ts}
for _, item := range []string{IndexFile, IndexEndpointFile} {
f := &composer.File{
Name: helpers.GetResourceRouteFilename(item, cfg.sveltin),
TemplateID: item,
TemplateData: metadataTemaplateData,
Name: helpers.GetResourceRouteFilename(item, cfg.sveltin),
TemplateID: item,
TemplateData: &config.TemplateData{
Config: cfg.sveltin,
Metadata: metadataData,
},
}
resourceMedatadaRoutesFolder.Add(f)
}
Expand All @@ -281,16 +290,19 @@ func createOrAddContentForMetadataToRoutesLocalFolder(metadataTemaplateData *con
// NEW FILE: src/routes/<resource_name>/[slug]{+page.svelte, +page.ts}
for _, item := range []string{SlugFile, SlugEndpointFile} {
f := &composer.File{
Name: helpers.GetResourceRouteFilename(item, cfg.sveltin),
TemplateID: item,
TemplateData: metadataTemaplateData,
Name: helpers.GetResourceRouteFilename(item, cfg.sveltin),
TemplateID: item,
TemplateData: &config.TemplateData{
Config: cfg.sveltin,
Metadata: metadataData,
},
}
slugFolder.Add(f)
}
resourceMedatadaRoutesFolder.Add(slugFolder)

// NEW FOLDER: src/routes/<resource_name>/<metadata_name>
resourceRoutesFolder := composer.NewFolder(metadataTemaplateData.Resource)
resourceRoutesFolder := composer.NewFolder(metadataData.Resource)
resourceRoutesFolder.Add(resourceMedatadaRoutesFolder)

// GET FOLDER: src/routes folder
Expand All @@ -300,34 +312,40 @@ func createOrAddContentForMetadataToRoutesLocalFolder(metadataTemaplateData *con
return routesFolder
}

func createOrAddContentForMetadataToApiLocalFolder(metadataTemplateData *config.TemplateData) *composer.Folder {
func createOrAddContentForMetadataToApiLocalFolder(metadataData *tpltypes.MetadataData) *composer.Folder {
cfg.log.Info("REST endpoint")
// GET FOLDER: src/routes/api/<api_version> folder
apiFolder := cfg.fsManager.GetFolder(ApiFolder)

// NEW FOLDER: src/routes/api/<api_version>/<resource_name>
resourceAPIFolder := composer.NewFolder(metadataTemplateData.Resource)
resourceAPIFolder := composer.NewFolder(metadataData.Resource)

// NEW FOLDER: src/routes/api/<version>/<resource_name>/[<resource_name> = <metadata_name>]
resourceAPIMetadataMatcherFolder := composer.NewFolder(fmt.Sprintf("%s%s%s%s%s", "[", utils.ToSnakeCase(metadataTemplateData.Resource), "=", utils.ToSnakeCase(metadataTemplateData.Name), "]"))
// NEW FOLDER: src/routes/api/<version>/<resource_name>/<metadata_name>
resourceAPIMetadataMatcherFolder := composer.NewFolder(utils.ToSnakeCase(metadataData.Name))

// NEW FILE: src/routes/api/<version>/<resource_name>/[<resource_name> = <metadata_name>]/index.ts
// NEW FILE: src/routes/api/<version>/<resource_name>/[<resource_name> = <metadata_name>]/+server.ts
resourceMetadataIndexAPIFile := &composer.File{
Name: cfg.sveltin.GetAPIFilename(),
TemplateID: ApiMetadataIndex,
TemplateData: metadataTemplateData,
Name: cfg.sveltin.GetAPIFilename(),
TemplateID: ApiMetadataIndex,
TemplateData: &config.TemplateData{
Config: cfg.sveltin,
Metadata: metadataData,
},
}
resourceAPIMetadataMatcherFolder.Add(resourceMetadataIndexAPIFile)
resourceAPIFolder.Add(resourceAPIMetadataMatcherFolder)

// NEW FOLDER: src/routes/api/<version>/<resource_name>/[<resource_name> = <metadata_name>]/[<metadata_name> = string]
resourceAPIMetadataNameMatcherFolder := composer.NewFolder(fmt.Sprintf("%s%s%s%s%s", "[", utils.ToSnakeCase(metadataTemplateData.Name), "=", "string", "]"))
// NEW FOLDER: src/routes/api/<version>/<resource_name>/<metadata_name>/[slug=string]
resourceAPIMetadataNameMatcherFolder := composer.NewFolder("[slug=string]")

// NEW FILE: src/routes/api/<version>/<resource_name>/[<resource_name> = <metadata_name>]/[<metadata_name> = string]/index.ts
// NEW FILE: src/routes/api/<version>/<resource_name>/<metadata_name>/[slug=string]/+server.ts
resourceMetadataNameIndexAPIFile := &composer.File{
Name: cfg.sveltin.GetAPIFilename(),
TemplateID: ApiFolder,
TemplateData: metadataTemplateData,
Name: cfg.sveltin.GetAPIFilename(),
TemplateID: ApiFolder,
TemplateData: &config.TemplateData{
Config: cfg.sveltin,
Metadata: metadataData,
},
}
resourceAPIMetadataNameMatcherFolder.Add(resourceMetadataNameIndexAPIFile)
resourceAPIMetadataMatcherFolder.Add(resourceAPIMetadataNameMatcherFolder)
Expand Down
29 changes: 6 additions & 23 deletions cmd/generateMenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ package cmd

import (
"github.com/spf13/cobra"
"github.com/sveltinio/sveltin/common"
"github.com/sveltinio/sveltin/config"
"github.com/sveltinio/sveltin/helpers"
"github.com/sveltinio/sveltin/helpers/factory"
"github.com/sveltinio/sveltin/internal/composer"
"github.com/sveltinio/sveltin/internal/markup"
"github.com/sveltinio/sveltin/resources"
"github.com/sveltinio/sveltin/utils"
Expand Down Expand Up @@ -46,37 +43,23 @@ func RunGenerateMenuCmd(cmd *cobra.Command, args []string) {

cfg.log.Plain(markup.H1("Generating the menu structure file"))

projectFolder := cfg.fsManager.GetFolder(RootFolder)
cfg.log.Info("Getting list of all resources contents")
existingResources := helpers.GetAllResources(cfg.fs, cfg.sveltin.GetContentPath())
contents := helpers.GetResourceContentMap(cfg.fs, existingResources, cfg.sveltin.GetContentPath())

cfg.log.Info("Getting list of existing public pages")
cfg.log.Info("Getting list of all routes")
allRoutes := helpers.GetAllRoutes(cfg.fs, cfg.pathMaker.GetPathToRoutes())
allResources := helpers.GetAllResources(cfg.fs, cfg.pathMaker.GetPathToExistingResources())
allRoutesExceptsResource := common.Difference(allRoutes, allResources)
// exclude api folder from the list
publicPages := common.Difference(allRoutesExceptsResource, []string{ApiFolder})

cfg.log.Info("Getting list of existing resources")
availableResources := helpers.GetAllResourcesWithContentName(cfg.fs, cfg.pathMaker.GetPathToExistingResources(), withContentFlag)

// GET FOLDER: config
configFolder := cfg.fsManager.GetFolder(ConfigFolder)

// ADD FILE: config/menu.js
cfg.log.Info("Saving the menu.js.ts file")
menuFile := &composer.File{
Name: "menu.js.ts",
TemplateID: "menu",
TemplateData: &config.TemplateData{
Menu: &config.MenuConfig{
Resources: availableResources,
Pages: publicPages,
WithContent: withContentFlag,
},
},
}
menuFile := cfg.fsManager.NewMenuFile("menu", &cfg.project, allRoutes, contents, withContentFlag)
configFolder.Add(menuFile)

// SET FOLDER STRUCTURE
projectFolder := cfg.fsManager.GetFolder(RootFolder)
projectFolder.Add(configFolder)

// GENERATE THE FOLDER TREE
Expand Down
13 changes: 3 additions & 10 deletions cmd/generateRss.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package cmd

import (
"github.com/spf13/cobra"
"github.com/sveltinio/sveltin/common"
"github.com/sveltinio/sveltin/helpers"
"github.com/sveltinio/sveltin/helpers/factory"
"github.com/sveltinio/sveltin/internal/markup"
Expand Down Expand Up @@ -38,25 +37,19 @@ func RunGenerateRSSCmd(cmd *cobra.Command, args []string) {

cfg.log.Plain(markup.H1("Generating the RSS feed file"))

cfg.log.Info("Getting all existing resources")
cfg.log.Info("Getting list of all resources contents")
existingResources := helpers.GetAllResources(cfg.fs, cfg.pathMaker.GetPathToExistingResources())

cfg.log.Info("Getting all resources contents")
contents := helpers.GetResourceContentMap(cfg.fs, existingResources, cfg.sveltin.GetContentPath())

cfg.log.Info("Getting all existing public pages")
cfg.log.Info("Getting list of all routes")
allRoutes := helpers.GetAllRoutes(cfg.fs, cfg.pathMaker.GetPathToRoutes())
allRoutesExceptsResource := common.Difference(allRoutes, existingResources)
// exclude api folder from the list
pages := common.Difference(allRoutesExceptsResource, []string{ApiFolder})
//pages := helpers.GetAllPublicPages(cfg.fs, cfg.pathMaker.GetPathToPublicPages())

// GET FOLDER: static
staticFolder := cfg.fsManager.GetFolder(StaticFolder)

// NEW FILE: static/rss.xml
cfg.log.Info("Saving the file to the static folder")
rssFile := cfg.fsManager.NewNoPage("rss", &cfg.project, existingResources, contents, nil, pages)
rssFile := cfg.fsManager.NewNoPageFile("rss", &cfg.project, allRoutes, contents)
staticFolder.Add(rssFile)

// SET FOLDER STRUCTURE
Expand Down
Loading

0 comments on commit f514b24

Please sign in to comment.