diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-09-09 23:31:43 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-09-13 21:55:29 +0300 |
commit | 85ba9bfffba9bfd0b095cb766f72700d4c211e31 (patch) | |
tree | 43b66efaafe4cb804234ca7273873ab949305799 /commands | |
parent | 9df60b62f9c4e36a269f0c6e9a69bee9dc691031 (diff) |
Add "hugo mod npm pack"
This commit also introduces a convention where these common JS config files, including `package.hugo.json`, gets mounted into:
```
assets/_jsconfig
´``
These files mapped to their real filename will be added to the environment when running PostCSS, Babel etc., so you can do `process.env.HUGO_FILE_TAILWIND_CONFIG_JS` to resolve the real filename.
But do note that `assets` is a composite/union filesystem, so if your config file is not meant to be overridden, name them something specific.
This commit also adds adds `workDir/node_modules` to `NODE_PATH` and `HUGO_WORKDIR` to the env when running the JS tools above.
Fixes #7644
Fixes #7656
Fixes #7675
Diffstat (limited to 'commands')
-rw-r--r-- | commands/mod.go | 15 | ||||
-rw-r--r-- | commands/mod_npm.go | 58 |
2 files changed, 72 insertions, 1 deletions
diff --git a/commands/mod.go b/commands/mod.go index 81f660f43..b390d1e75 100644 --- a/commands/mod.go +++ b/commands/mod.go @@ -1,4 +1,4 @@ -// Copyright 2019 The Hugo Authors. All rights reserved. +// Copyright 2020 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,6 +20,8 @@ import ( "path/filepath" "regexp" + "github.com/gohugoio/hugo/hugolib" + "github.com/gohugoio/hugo/modules" "github.com/spf13/cobra" ) @@ -114,6 +116,8 @@ This is not needed if you only operate on modules inside /themes or if you have RunE: nil, } + cmd.AddCommand(newModNPMCmd(c)) + cmd.AddCommand( &cobra.Command{ Use: "get", @@ -272,6 +276,15 @@ func (c *modCmd) withModsClient(failOnMissingConfig bool, f func(*modules.Client return f(com.hugo().ModulesClient) } +func (c *modCmd) withHugo(f func(*hugolib.HugoSites) error) error { + com, err := c.initConfig(true) + if err != nil { + return err + } + + return f(com.hugo()) +} + func (c *modCmd) initConfig(failOnNoConfig bool) (*commandeer, error) { com, err := initializeConfig(failOnNoConfig, false, &c.hugoBuilderCommon, c, nil) if err != nil { diff --git a/commands/mod_npm.go b/commands/mod_npm.go new file mode 100644 index 000000000..15c875d8e --- /dev/null +++ b/commands/mod_npm.go @@ -0,0 +1,58 @@ +// Copyright 2020 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package commands + +import ( + "github.com/gohugoio/hugo/hugolib" + "github.com/gohugoio/hugo/modules/npm" + "github.com/spf13/cobra" +) + +func newModNPMCmd(c *modCmd) *cobra.Command { + + cmd := &cobra.Command{ + Use: "npm", + Short: "Various npm helpers.", + Long: `Various npm (Node package manager) helpers.`, + RunE: func(cmd *cobra.Command, args []string) error { + return c.withHugo(func(h *hugolib.HugoSites) error { + return nil + }) + }, + } + + cmd.AddCommand(&cobra.Command{ + Use: "pack", + Short: "Experimental: Prepares and writes a composite package.json file for your project.", + Long: `Prepares and writes a composite package.json file for your project. + +On first run it creates a "package.hugo.json" in the project root if not alread there. This file will be used as a template file +with the base dependency set. + +This set will be merged with all "package.hugo.json" files found in the dependency tree, picking the version closest to the project. + +This command is marked as 'Experimental'. We think it's a great idea, so it's not likely to be +removed from Hugo, but we need to test this out in "real life" to get a feel of it, +so this may/will change in future versions of Hugo. +`, + RunE: func(cmd *cobra.Command, args []string) error { + + return c.withHugo(func(h *hugolib.HugoSites) error { + return npm.Pack(h.BaseFs.SourceFs, h.BaseFs.Assets.Dirs) + }) + }, + }) + + return cmd +} |