Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 31, 2020
0 parents commit c5b6e0b
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
7 changes: 7 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["config:base"],
"automerge": true,
"major": {
"automerge": false
}
}
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js

node_js:
- node
- lts/*
- 12
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Exuanbo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "gulp-plugin-boilerplate",
"version": "0.0.0",
"description": "",
"main": "dist/index.js",
"module": "dist/index.es.js",
"files": ["dist"],
"scripts": {
"test": "mocha",
"pretest": "npm run build",
"build": "rm -rf dist && rollup -c"
},
"repository": {
"type": "git",
"url": "git+https://github.com/exuanbo/gulp-plugin-boilerplate.git"
},
"keywords": [],
"author": "exuanbo",
"license": "MIT",
"bugs": {
"url": "https://github.com/exuanbo/gulp-plugin-boilerplate/issues"
},
"homepage": "https://github.com/exuanbo/gulp-plugin-boilerplate#readme",
"dependencies": {
"plugin-error": "1.0.1",
"through2": "4.0.2"
},
"devDependencies": {
"chai": "4.2.0",
"mocha": "8.2.0",
"rollup": "2.32.1",
"vinyl": "2.2.1"
}
}
17 changes: 17 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const pkg = require('./package.json')

export default {
input: 'src/index.js',
external: [...Object.keys(pkg.dependencies)],
output: [
{
file: pkg.main,
format: 'cjs',
exports: 'auto'
},
{
file: pkg.module,
format: 'es'
}
]
}
36 changes: 36 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import through from 'through2'
import PluginError from 'plugin-error'
import { salute } from './salute'

const { name } = require('../package.json')
const PLUGIN_NAME = name

const myPlugin = () =>
through.obj(async function (file, _, callback) {
if (file.isNull()) {
callback(null, file)
return
}

if (file.isStream()) {
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'))
return
}

if (file.contents.toString().length > 10) {
callback(new PluginError(PLUGIN_NAME, 'Name too long'))
return
}

try {
const newContents = await salute(file.contents)
file.contents = Buffer.from(newContents)
this.push(file)
} catch (err) {
this.emit('error', new PluginError(PLUGIN_NAME, err))
}

callback()
})

export default myPlugin
1 change: 1 addition & 0 deletions src/salute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const salute = async name => `hello ${name}`
1 change: 1 addition & 0 deletions test/hello_world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
51 changes: 51 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'

const fs = require('fs')
const path = require('path')
const expect = require('chai').expect
const Vinyl = require('vinyl')

const myPlugin = require('..')
const { name } = require('../package.json')
const PLUGIN_NAME = name

const getFile = file => {
const filePath = path.join(__dirname, file)

return new Vinyl({
base: path.dirname(filePath),
path: filePath,
contents: fs.readFileSync(filePath)
})
}

const compare = (stream, fixture, expected, done, expectedErr) => {
stream.on('error', err => {
if (expectedErr) {
expect(err.message).to.equal(expectedErr)
done()
return
}
done(err)
})

stream.on('data', file => {
expect(file.contents.toString()).to.equal(
getFile(expected).contents.toString()
)
done()
})

stream.write(getFile(fixture))
stream.end()
}

describe(PLUGIN_NAME, () => {
it('works for me', done => {
compare(myPlugin(), 'world', 'hello_world', done)
})

it('throws an error', done => {
compare(myPlugin(), 'long_world', 'no_file', done, 'Name too long')
})
})
1 change: 1 addition & 0 deletions test/long_world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wooooooorld
1 change: 1 addition & 0 deletions test/world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
world

0 comments on commit c5b6e0b

Please sign in to comment.