Last active
March 24, 2016 22:05
-
-
Save dignifiedquire/1e907226b2eea6242cc2 to your computer and use it in GitHub Desktop.
Auto upgrade to dignified.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const exec = require('child_process').execSync | |
const path = require('path') | |
const fs = require('fs') | |
const glob = require('glob') | |
const name = process.argv[2] | |
console.log('Starting to transform %s', name) | |
if (fs.existsSync(`./${name}`)) { | |
exec(`rm -rf ./${name}`) | |
} | |
const cwd = path.resolve(__dirname, name) | |
// git clone [email protected]:dignifiedquire/<repo>.git | |
if (!fs.existsSync(cwd)) { | |
console.log('Cloning..') | |
exec(`git clone [email protected]:dignifiedquire/${name}.git`) | |
} | |
// git checkout -b dignified | |
if (exec('git name-rev --name-only HEAD', {cwd}).toString().trim() === 'master') { | |
console.log('checkout') | |
exec('git checkout -b dignified', {cwd}) | |
} | |
// Update .gitignore | |
// - append `dist` | |
// - append `lib` | |
const gitignore = fs.readFileSync(path.join(cwd, '.gitignore')).toString() | |
if (!gitignore.match(/\n(lib|dist)\n/)) { | |
console.log('Updating .gitignore...') | |
fs.writeFileSync(path.join(cwd, '.gitignore'), gitignore + '\nlib\ndist\n') | |
} | |
// Update package.json | |
// - Replace all scripts, except `coverage` with the dignified versions | |
// - Remove devDependencies | |
// - karma-* | |
// - json-loader | |
// - babel-loader | |
// - babel* | |
// - eslint* | |
// - standard | |
// - webpack | |
// - Set main = 'lib/index.js' | |
// - Set jsnext:main = 'src/index.js' | |
let pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'))) | |
if (!pkg.devDependencies['dignified.js']) { | |
console.log('Updating package.json') | |
pkg.main = 'lib/index.js' | |
pkg['jsnext:main'] = 'src/index.js' | |
Object.keys(pkg.scripts).forEach((s) => { | |
if (s === 'coverage') return | |
delete pkg.scripts[s] | |
}) | |
pkg.scripts.lint = 'dignified-lint' | |
pkg.scripts.test = 'dignified-test' | |
pkg.scripts['test:node'] = 'dignified-test node' | |
pkg.scripts['test:browser'] = 'dignified-test browser' | |
pkg.scripts.build = 'dignified-build' | |
pkg.scripts.release = 'dignified-release' | |
pkg['pre-commit'] = ['lint', 'test'] | |
Object.keys(pkg.devDependencies).forEach((dep) => { | |
if (dep.match(/^karma/) || | |
dep.match(/^babel/) || | |
dep.match(/^eslint/) || | |
dep === 'json-loader' || | |
dep === 'webpack' || | |
dep === 'standard') { | |
delete pkg.devDependencies[dep] | |
} | |
}) | |
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(pkg, null, 2)) | |
// New dependencies | |
// - chai | |
// - dignified.js | |
// - pre-commit | |
console.log('Adding chai and dignified.js') | |
exec('npm install --save-dev dignifiedquire/dignified.js' , {cwd}) | |
exec('npm install --save-dev chai' , {cwd}) | |
exec('npm install --save-dev pre-commit' , {cwd}) | |
} | |
// Installing dependencies | |
console.log('Installing dependencies') | |
exec('npm install', {cwd}) | |
// git rm -rf dist | |
if (fs.existsSync(path.join(cwd, 'dist'))) { | |
console.log('Removing dist') | |
exec('git rm -rf dist', {cwd}) | |
} | |
// Ensure all tests are in `test` | |
if (fs.existsSync(path.join(cwd, 'tests'))) { | |
console.log('Renaming test folder') | |
exec('git mv tests test', {cwd}) | |
} | |
// Add 'use strict' to all js files | |
console.log('Adding strict mode') | |
const addStrict = (file) => { | |
let content = fs.readFileSync(path.join(cwd, file)).toString() | |
if (content.match(/'use strict'\n/)) { | |
return | |
} | |
content = `'use strict'\n\n${content}` | |
fs.writeFileSync(path.join(cwd, file), content) | |
} | |
glob.sync('test/**/*.js', {cwd}).forEach(addStrict) | |
glob.sync('src/**/*.js', {cwd}).forEach(addStrict) | |
glob.sync('examples/**/*.js', {cwd}).forEach(addStrict) | |
// Linting first pass | |
exec('npm run lint', {cwd}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment