├── .gitignore ├── README.md ├── example.gif ├── lerna.json ├── package.json └── packages ├── create-npm-package-scaffold ├── README.md ├── package.json └── src │ ├── README.md │ ├── __tests__ │ └── index.spec.js │ ├── config │ ├── base.config.js │ └── scripts │ │ ├── build.js │ │ └── dev.js │ ├── package.json │ └── src │ └── index.js └── create-npm-package ├── create-npm-package.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | .DS_Store 4 | *.tgz 5 | npm-debug.log 6 | .tern-project 7 | etc/ 8 | yarn-error.log 9 | coverage/ 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![npm version](https://badge.fury.io/js/create-npm-package.svg)](https://badge.fury.io/js/create-npm-package) 2 | [![npm](https://img.shields.io/npm/dm/create-npm-package.svg)](https://www.npmjs.com/package/create-npm-package) 3 | 4 | # create-npm-package 5 | 6 | Creates an npm package boilerplate that you don't have to write again. 7 | 8 | ![alt tag](https://raw.githubusercontent.com/MatteoGabriele/create-npm-package/master/example.gif) 9 | 10 | 11 | ## Install 12 | 13 | ```bash 14 | $ npm install -g create-npm-package 15 | ``` 16 | 17 | ## Usage 18 | 19 | ### Create new package 20 | 21 | ```bash 22 | $ create-npm-package my-package 23 | ``` 24 | 25 | ### Skip the npm name check 26 | 27 | The existance of the package name is checked automatically, but you can skip it! 28 | 29 | ```bash 30 | $ create-npm-package my-package --skip-check 31 | ``` 32 | 33 | ### Skip git initialization 34 | 35 | Git is initialized on package creation, but you can skip it! 36 | 37 | ```bash 38 | $ create-npm-package my-package --skip-git 39 | ``` 40 | 41 | ### Use Yarn to install dependencies 42 | 43 | ```bash 44 | $ create-npm-package my-package --yarn 45 | ``` 46 | 47 | ## Git configuration 48 | 49 | To be able to grab your git global data and add them to the package.json automatically, you need to 50 | 51 | ```bash 52 | $ git config --global user.name 'your name' 53 | ``` 54 | 55 | ```bash 56 | $ git config --global user.email 'your@email' 57 | ``` 58 | 59 | and if you want to add also the repository url, just run 60 | 61 | ```bash 62 | $ git config --global user.url 'https://github.com/your_account_name' 63 | ``` 64 | 65 | # Issues and features requests 66 | 67 | Please drop an issue, if you find something that doesn't work, or a feature request at [https://github.com/MatteoGabriele/create-npm-package/issues](https://github.com/MatteoGabriele/create-npm-package/issues) 68 | 69 | Follow me on twitter [@matteo\_gabriele](https://twitter.com/matteo_gabriele) 70 | -------------------------------------------------------------------------------- /example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MatteoGabriele/create-npm-package/2a47f9298c84feeec70f838e7cf4539ceb16442e/example.gif -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "lerna": "2.0.0-beta.38", 3 | "packages": [ 4 | "packages/*" 5 | ], 6 | "version": "1.3.4" 7 | } 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "lerna": "2.0.0-beta.38" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/README.md: -------------------------------------------------------------------------------- 1 | # create-npm-package-scaffold 2 | 3 | Scaffold used by create-npm-package 4 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "create-npm-package-scaffold", 3 | "version": "1.3.4", 4 | "description": "Scaffold used by create-npm-package", 5 | "author": "Matteo Gabriele ", 6 | "license": "MIT", 7 | "bugs": { 8 | "url": "https://github.com/MatteoGabriele/create-npm-package/issues" 9 | }, 10 | "files": [ 11 | "src" 12 | ], 13 | "repository": "MatteoGabriele/create-npm-package" 14 | } 15 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/src/README.md: -------------------------------------------------------------------------------- 1 | # <%= name %> 2 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/src/__tests__/index.spec.js: -------------------------------------------------------------------------------- 1 | describe('my node module', function () { 2 | it('should be an amazing module', function () { 3 | expect(true).toBeTruthy() 4 | }) 5 | }) 6 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/src/config/base.config.js: -------------------------------------------------------------------------------- 1 | const webpack = require('webpack') 2 | const pkg = require('../package.json') 3 | const path = require('path') 4 | 5 | module.exports = { 6 | context: path.resolve(__dirname, '../'), 7 | entry: './src/index.js', 8 | output: { 9 | path: path.resolve(__dirname, '../dist'), 10 | filename: `${pkg.name}.js`, 11 | libraryTarget: 'umd' 12 | }, 13 | resolve: { 14 | extensions: ['.js'] 15 | }, 16 | module: { 17 | rules: [ 18 | { 19 | test: /\.js$/, 20 | exclude: /node_modules/, 21 | loader: 'babel-loader', 22 | options: { 23 | presets: ['blue'], 24 | babelrc: false 25 | } 26 | } 27 | ] 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/src/config/scripts/build.js: -------------------------------------------------------------------------------- 1 | const base = require('../base.config') 2 | const merge = require('webpack-merge') 3 | const webpack = require('webpack') 4 | const pkg = require('../../package.json') 5 | const ora = require('ora') 6 | const CompressionPlugin = require('compression-webpack-plugin') 7 | 8 | const spinner = ora() 9 | 10 | const webpackConfig = merge.smart({}, base, { 11 | plugins: [ 12 | new webpack.DefinePlugin({ 13 | 'process.env': { 14 | NODE_ENV: '"production"' 15 | } 16 | }), 17 | new webpack.optimize.UglifyJsPlugin({ 18 | compress: { 19 | screw_ie8: true, 20 | warnings: false, 21 | }, 22 | mangle: { 23 | screw_ie8: true, 24 | }, 25 | output: { 26 | comments: false, 27 | screw_ie8: true, 28 | }, 29 | sourceMap: true 30 | }), 31 | new CompressionPlugin({ 32 | asset: '[path].gz[query]', 33 | algorithm: 'gzip', 34 | test: /\.js$/, 35 | threshold: 10240, 36 | minRatio: 0.8 37 | }) 38 | ] 39 | }) 40 | 41 | const compiler = webpack(webpackConfig) 42 | 43 | console.log('') 44 | spinner.text = 'Build package' 45 | spinner.start() 46 | 47 | compiler.run(function (error, stats) { 48 | if (error) { 49 | spinner.fail() 50 | console.log('') 51 | console.log(error) 52 | process.exit(1) 53 | } 54 | 55 | spinner.succeed() 56 | console.log('') 57 | 58 | process.stdout.write(stats.toString({ 59 | colors: true, 60 | hash: false, 61 | version: false, 62 | timings: false, 63 | modules: false, 64 | children: false, 65 | chunks: false, 66 | chunkModules: false 67 | }) + '\n') 68 | }) 69 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/src/config/scripts/dev.js: -------------------------------------------------------------------------------- 1 | const base = require('../base.config') 2 | const merge = require('webpack-merge') 3 | const webpack = require('webpack') 4 | const FriendyError = require('friendly-errors-webpack-plugin') 5 | const pkg = require('../../package.json') 6 | 7 | const webpackConfig = merge.smart({}, base, { 8 | plugins: [ 9 | new webpack.DefinePlugin({ 10 | 'process.env': { 11 | NODE_ENV: '"development"' 12 | } 13 | }), 14 | new FriendyError() 15 | ] 16 | }) 17 | 18 | const compiler = webpack(webpackConfig) 19 | 20 | compiler.watch({ 21 | aggregateTimeout: 300, 22 | poll: true 23 | }, function (error, stats) { 24 | if (error) { 25 | console.log(error) 26 | process.exit(1) 27 | } 28 | }) 29 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/src/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "<%= name %>", 3 | "author": "<%= author.name %> <<%= author.email %>>", 4 | "version": "0.0.0", 5 | "description": "", 6 | "bugs": { 7 | "url": "<%= issuesUrl %>" 8 | }, 9 | "repository": "<%= repositoryUrl %>", 10 | "scripts": { 11 | "dev": "node ./config/scripts/dev.js", 12 | "build": "node ./config/scripts/build.js", 13 | "test": "jest", 14 | "test:watch": "jest --watch" 15 | }, 16 | "main": "./dist/<%= name %>.js", 17 | "license": "MIT", 18 | "devDependencies": { 19 | "babel-core": "^6.24.1", 20 | "babel-loader": "^7.0.0", 21 | "babel-preset-blue": "^1.0.0-beta.29", 22 | "compression-webpack-plugin": "^0.4.0", 23 | "friendly-errors-webpack-plugin": "^1.6.1", 24 | "ora": "^1.2.0", 25 | "webpack": "^2.5.0", 26 | "webpack-merge": "^4.1.0", 27 | "jest": "^20.0.0" 28 | }, 29 | "dependencies": {} 30 | } 31 | -------------------------------------------------------------------------------- /packages/create-npm-package-scaffold/src/src/index.js: -------------------------------------------------------------------------------- 1 | // this is your main entry 2 | -------------------------------------------------------------------------------- /packages/create-npm-package/create-npm-package.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const kopy = require('kopy') 4 | const fs = require('fs') 5 | const path = require('path') 6 | const Listr = require('listr') 7 | const execa = require('execa') 8 | const fetch = require('node-fetch') 9 | const co = require('co') 10 | const extractPackage = require('extract-package') 11 | const argv = require('minimist')(process.argv.slice(2)) 12 | 13 | const name = argv._[0] 14 | const useYarn = argv.yarn 15 | const skipPackageCheck = argv['skip-check'] 16 | const skipGitInit = argv['skip-git'] 17 | const version = argv['version'] 18 | 19 | if (version) { 20 | console.log('v' + require('./package.json').version) 21 | process.exit(0) 22 | } 23 | 24 | if (!name) { 25 | console.log('[create-npm-package] Please specify name of the package') 26 | process.exit(1) 27 | } 28 | 29 | const gitignore = 'dist/\n' + 30 | 'node_modules/\n' + 31 | '.DS_Store\n' + 32 | 'npm-debug.log\n' + 33 | 'yarn-error.log\n' 34 | 35 | const dest = path.resolve(process.cwd(), name) 36 | 37 | const tasks = new Listr([ 38 | { 39 | title: 'Check for package existance', 40 | skip: () => skipPackageCheck, 41 | task: () => fetch(`http://registry.npmjs.org/${name}`) 42 | .then(response => { 43 | return response.json() 44 | }) 45 | .then(packageInfo => { 46 | if (!packageInfo.name) { 47 | return 48 | } 49 | 50 | throw new Error(`${name} already exists https://www.npmjs.com/package/${name}`) 51 | }) 52 | }, 53 | { 54 | title: 'Get git global user information', 55 | task: ctx => { 56 | const getGloablUserGit = co.wrap(function * (name = '', email = '', url = '') { 57 | try { 58 | const configName = yield execa.shell('git config user.name') 59 | const configEmail = yield execa.shell('git config user.email') 60 | const configUrl = yield execa.shell('git config user.url') 61 | 62 | name = configName.stdout 63 | email = configEmail.stdout 64 | url = configUrl.stdout 65 | } catch (error) {} 66 | 67 | return { name, email, url } 68 | }) 69 | 70 | return getGloablUserGit().then(user => ctx.gitUser = user) 71 | } 72 | }, 73 | { 74 | title: 'Download scaffold', 75 | task: ctx => extractPackage({ 76 | name: 'create-npm-package-scaffold' 77 | }).then(templatePath => ctx.templatePath = templatePath) 78 | }, 79 | { 80 | title: 'Drop package scaffold', 81 | task: ctx => kopy(`${ctx.templatePath}/src`, dest, { 82 | data: { 83 | name: name, 84 | author: ctx.gitUser, 85 | issuesUrl: ctx.gitUser.url !== '' ? `${ctx.gitUser.url}/${name}/issues` : '', 86 | repositoryUrl: ctx.gitUser.url !== '' ? `${ctx.gitUser.url}/${name}` : '' 87 | } 88 | }).then(() => { 89 | const filePath = `${dest}/package.json` 90 | const file = fs.readFileSync(filePath, 'utf8') 91 | const scaffoldPackageJSON = JSON.parse(file) 92 | 93 | // files property needs to be added programmatically or all scaffold folder 94 | // will be ignored during create-npm-package publish 95 | scaffoldPackageJSON.files = ['dist'] 96 | 97 | fs.writeFileSync(filePath, JSON.stringify(scaffoldPackageJSON, null, 2)) 98 | }) 99 | }, 100 | { 101 | title: 'Install dependencies with Yarn', 102 | enabled: () => useYarn, 103 | task: () => { 104 | process.chdir(dest) 105 | return execa('yarn') 106 | } 107 | }, 108 | { 109 | title: 'Install dependencies', 110 | enabled: () => !useYarn, 111 | task: () => { 112 | process.chdir(dest) 113 | return execa('npm', ['install']) 114 | } 115 | }, 116 | { 117 | title: 'Initialize git', 118 | skip: () => skipGitInit, 119 | task: () => { 120 | fs.writeFileSync(`${dest}/.gitignore`, gitignore) 121 | return execa('git', ['init']) 122 | } 123 | } 124 | ]) 125 | 126 | tasks.run() 127 | .then(response => { 128 | const cmdStart = useYarn ? 'yarn dev' : 'npm run dev' 129 | const cmdBuild = useYarn ? 'yarn build' : 'npm run build' 130 | 131 | console.log(` 132 | Npm package as been created successfully! 133 | 134 | Start coding: 135 | cd ${name} && ${cmdStart} 136 | 137 | Build your package 138 | cd ${name} && ${cmdBuild} 139 | `) 140 | }) 141 | .catch(() => {}) 142 | -------------------------------------------------------------------------------- /packages/create-npm-package/index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var currentNodeVersion = process.versions.node; 4 | if (currentNodeVersion.split('.')[0] < 6) { 5 | console.error( 6 | 'You are running Node ' + 7 | currentNodeVersion + 8 | '.\n' + 9 | 'Create NPM package requires Node 6 or higher. \n' + 10 | 'Please update your version of Node.' 11 | ); 12 | process.exit(1) 13 | } 14 | 15 | require('./create-npm-package') 16 | -------------------------------------------------------------------------------- /packages/create-npm-package/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "create-npm-package", 3 | "version": "1.3.4", 4 | "description": "Creates an npm package boilerplate that you don't have to write again.", 5 | "author": "Matteo Gabriele ", 6 | "license": "MIT", 7 | "bugs": { 8 | "url": "https://github.com/MatteoGabriele/create-npm-package/issues" 9 | }, 10 | "files": [ 11 | "src", 12 | "index.js", 13 | "create-npm-package.js" 14 | ], 15 | "repository": "MatteoGabriele/create-npm-package", 16 | "bin": { 17 | "create-npm-package": "./index.js" 18 | }, 19 | "dependencies": { 20 | "co": "^4.6.0", 21 | "execa": "^0.6.3", 22 | "extract-package": "^1.1.0", 23 | "kopy": "^7.0.0", 24 | "listr": "^0.12.0", 25 | "minimist": "^1.2.0", 26 | "node-fetch": "^1.6.3" 27 | } 28 | } 29 | --------------------------------------------------------------------------------