Git Product home page Git Product logo

copy-template-dir's Introduction

copy-template-dir

NPM version build status Test coverage Downloads js-standard-style

High throughput template dir writes. Supports variable injection using the mustache {{ }} syntax.

Installation

$ npm install copy-template-dir

Usage

const copy = require('copy-template-dir')
const path = require('path')

const vars = { foo: 'bar' }
const inDir = path.join(process.cwd(), 'templates')
const outDir = path.join(process.cwd(), 'dist')

copy(inDir, outDir, vars, (err, createdFiles) => {
  if (err) throw err
  createdFiles.forEach(filePath => console.log(`Created ${filePath}`))
  console.log('done!')
})

API

copyTemplateDir(templateDir, targetDir, vars, cb)

Copy a directory of files over to the target directory, and inject the files with variables. Takes the following arguments:

  • templateDir: The directory that holds the templates. Filenames prepended with a _ will have it removed when copying. Dotfiles need to be prepended with a _. Files and filenames are populated with variables using the {{varName}} syntax.
  • targetDir: the output directory
  • vars: An object with variables that are injected into the template files and file names.
  • cb(err, createdFiles): A callback that is called on completion, with paths to created files if there were no errors.

See Also

License

MIT

copy-template-dir's People

Contributors

ahmadawais avatar ggpeti avatar insin avatar loklaan avatar stevesouth avatar yoshuawuyts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

copy-template-dir's Issues

Is there any hook to use custom syntax to achieve variable injection.

background

There is a .github file as follow I want copy it to another dir.

name: github pages
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2.1.0
        with:
          node-version: '12.x'

      - name: Get yarn cache
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-website-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-website-

      - run: yarn install --frozen-lockfile
      - run: yarn build

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: .crd-dist

But when use copy-template-dir, the output file is converted to

name: github pages
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2.1.0
        with:
          node-version: '12.x'

      - name: Get yarn cache
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: $
          key: $-website-$
          restore-keys: |
            $-website-

      - run: yarn install --frozen-lockfile
      - run: yarn build

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: $
          publish_dir: .crd-dist

Is there any hook to use custom syntax to achieve variable injection to avoid such situation.

No errors with a non-existent srcDir

Just something which came up today using [email protected]:

I moved some code up a directory and forgot to change the relative path it was using to find my template directory, but I didn't get an error callback from copy-template-dir due to the non-existent directory.

It seems like copy-template-dir is eating errors from readdirp because it's using the streaming interface and there's no .on('error' ...) handler.

What's your preferred solution to this?

  • Check that srcDir exists in copyTemplateDir and bail out early.
  • Add an error handler for the readdirp stream (it seems it attempts 2 file operations before returning the stream - I saw 2 errors when I tried this quickly in my node_modules/copy-template-dir)

Rename all leading _ files

Would it be possible to extend renaming of _ prefixed files to include all files with the prefix, not just dotfiles?

I just discovered that npm will read nested package.json files in your project, which caused this packaging issue with some templates I was providing: insin/nwb#21

To avoid this, I would need to rename package.json files which will be copied by copy-template-dir to _package.json.

I can create a PR for this later if you're ok with the proposed change.

A suggestion for callback

I think, It's much better to use Promise instead of callback.
like this:

copy(inDir, outDir, vars).then(() => {
  console.log('done');
}).catch(err => { throw err; });

And I'm interested in to do that.

Dotfiles in nested directories aren't renamed

I have a template directory with a tests/_.eslintrc file which isn't getting renamed by reDot.

This tweak seems to do the trick:

function reDot (filepath) {
  const parts = filepath.split(path.sep)
  const filename = parts.pop().replace(/^\_./, '.')
  return parts.concat([filename]).join(path.sep)
}

transform function?

I'd love to be able to write my template files in ejs and just run it through copy-template-dir - what do you think of adding a little transform callback for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.