Skip to content

Commit

Permalink
Add support for writing individual files and package install
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Feb 2, 2018
1 parent 87dc7f7 commit 61c52a7
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 18 deletions.
20 changes: 17 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# node-skel

This is a simple CLI tool to initialize a new node project with some common
files. Currently, an [EditorConfig](http://editorconfig.org/) file and a
Git ignore file are created. In the future, the tool may allow for more files;
possibly via switches or a prompt system.
files, `npm` scripts, and dependencies.

By default the following will be added:

+ A `.editorconfig` file
+ A `.gitignore` file
+ A `.travis.yml` file
+ Development dependences: `pre-commit`, `snazzy`, `standard` and `tap`
+ Scripts for linting and testing
+ Pre-commit hooks for linting and testing

## Install and Usage

Expand All @@ -15,6 +22,13 @@ $ npm init
$ nskel
```

To learn about the available options, run `nskel run --help`.

> ### Note
> This utility is meant to be run *directly after* `npm init`.
> It will *overwrite* any "scripts" or "precommit" properties in the
> present `package.json` file, and any present dot files.
## License

[MIT License](http://jsumners.mit-license.org/)
20 changes: 20 additions & 0 deletions files/travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: node_js

node_js:
- "9"
- "8"
- "6"

# before_install:
# - curl -L https://unpkg.com/@pnpm/self-installer | node
# install:
# - pnpm install

script:
- npm run lint-ci
- npm run test-ci

notifications:
email:
on_success: never
on_failure: always
143 changes: 132 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,138 @@
#!/usr/bin/env node
'use strict'

const {exec} = require('child_process')
const fs = require('fs')
const path = require('path')
const pwd = process.env['PWD']
const files = [
path.join(__dirname, 'files', 'editorconfig'),
path.join(__dirname, 'files', 'gitignore')
]

files.forEach((f) => {
const fname = path.basename(f)
const outs = fs.createWriteStream(path.join(pwd, '.' + fname))
const ins = fs.createReadStream(f)
const {which} = require('@cedx/which')
const files = {
editorconfig: { src: path.join(__dirname, 'files', 'editorconfig'), name: '.editorconfig' },
gitignore: { src: path.join(__dirname, 'files', 'gitignore'), name: '.gitignore' },
travis: { src: path.join(__dirname, 'files', 'travis.yml'), name: '.travis.yml' }
}

const pkg = require('./package.json')
const prog = require('sade')('nskel')

// TODO: allow specifying package manager binary
prog
.version(pkg.version)
.describe('A utility to help bootstrap a Node.js project.')

prog
.command('run', 'Generate files and/or create scripts and install packages', {default: true})
.example('-A')
.option('--all, -a', 'Generate all dot files, add scripts, and install packages', true)
.option('--all-files, -A', 'Generate all dot files', false)
.option('--editorconfig, -e', 'Generate .editorconfig file', false)
.option('--gitignore, -g', 'Generate .gitignore file', false)
.option('--scripts, -s', 'Add standard scripts to package.json and install packages', false)
.option('--travis, -t', 'Generate .travis.yml file', false)
.action(run)

prog.parse(process.argv)

async function getPkgManager () {
console.log('Looking up package manager')
let manager
try {
manager = await which('pnpm')
console.log('Using pnpm via %s', manager)
} catch (e) {}
if (manager) return manager

try {
manager = await which('npm')
console.log('Using npm via %s', manager)
} catch (e) {
console.error('Cannot find compatible package manager in PATH.')
process.exit(1)
}
return manager
}

async function run (options) {
const cwd = process.cwd()
try {
require(path.join(cwd, 'package.json'))
} catch (err) {
if (options.all || options.scripts) {
console.error('Must have existing package.json to continue. Use `npm init` first.')
process.exit(1)
}
}

if (options.all || options.scripts) {
const manager = await getPkgManager()
// Using fork doesn't work because the arguments don't get recognized correctly.
console.log('Installing packages')
const child = exec(
manager + ' install --save-dev standard snazzy tap pre-commit'
)
child.on('exit', (code, signal) => {
if (code !== 0) {
console.error('Error installing packages. Got code: %s', code)
process.exit(code)
}

writeScripts((err) => {
if (err) {
console.error('Could not write package.json: %s', err.message)
process.exit(1)
}
deployFiles()
})
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
} else {
deployFiles()
}

function deployFiles () {
if (options.all || options.A) {
return deployAllFiles()
}
if (options.editorconfig) deployFile(files.editorconfig)
if (options.gitignore) deployFile(files.gitignore)
if (options.travis) deployFile(files.travis)
}
}

function writeScripts (cb) {
console.log('Adding scripts')
const cwd = process.cwd()
const packageFile = path.join(cwd, 'package.json')
const pkg = require(packageFile)
pkg.scripts = {
'lint': 'standard | snazzy',
'lint-ci': 'standard',
'test': `tap --no-cov 'test/**/*.test.js'`,
'test-ci': `tap --cov --coverage-report=text 'test/**/*.test.js'`
}
pkg.precommit = ['lint', 'test']
const deps = pkg.dependencies
const devDeps = pkg.devDependencies
delete pkg.dependencies
delete pkg.devDependencies
pkg.devDependencies = devDeps
pkg.dependencies = deps
var data = JSON.stringify(pkg, null, 2) + '\n'
fs.writeFile(packageFile, data, 'utf8', function (err) {
return cb(err)
})
}

function deployAllFiles () {
Object.keys(files).forEach((key) => {
deployFile(files[key])
})
}

function deployFile (file) {
console.log('Generating %s', file.name)
const cwd = process.cwd()
const outs = fs.createWriteStream(path.join(cwd, file.name))
const ins = fs.createReadStream(file.src)
ins.pipe(outs)
})
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
"url": "https://github.com/jsumners/node-skel/issues"
},
"homepage": "https://github.com/jsumners/node-skel#readme",
"files": [
"files/editorconfig",
"files/gitignore"
],
"engines": {
"node": ">=8.9.0"
},
"devDependencies": {
"pre-commit": "^1.2.2",
"standard": "^10.0.2"
},
"dependencies": {
"@cedx/which": "^3.1.0",
"sade": "^1.3.1"
}
}

0 comments on commit 61c52a7

Please sign in to comment.