-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (131 loc) · 4.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict'
const exec = require('child_process').execSync
const path = require('path')
const fs = require('fs')
const glob = require('glob')
module.exports = function (user, name, upstream) {
console.log('Starting to transform %s/%s', user, name)
const cwd = path.resolve(__dirname, 'modules', name)
console.log('cwd: %s', cwd)
// git clone [email protected]:<user>/<repo>.git
if (!fs.existsSync(cwd)) {
console.log('Cloning..')
exec(`git clone [email protected]:${user}/${name}.git`, {
cwd: path.resolve(__dirname, 'modules')
})
}
if (upstream) {
console.log('Adding upstream')
exec(`git remote add upstream [email protected]${upstream}/${name}`)
}
try {
exec('git checkout dignified', {cwd})
} catch (err) {
console.log(err.message)
}
// git checkout -b aegir
const branch = exec('git name-rev --name-only HEAD', {cwd}).toString().trim()
if (branch === 'master' || branch === 'dignified') {
console.log('git checkout -b aegir')
exec('git checkout -b aegir', {cwd})
}
// Add .npmignore
if (!fs.existsSync(path.join(cwd, '.npmignore'))) {
console.log('Adding .npmignore')
exec('cp .gitignore .npmignore', {cwd})
const npmignore = fs.readFileSync(path.join(cwd, '.npmignore')).toString()
fs.writeFileSync(path.join(cwd, '.npmignore'), npmignore + '\ntest\n')
}
// 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 aegir 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['aegir']) {
console.log('Updating package.json')
pkg.main = 'lib/index.js'
pkg['jsnext:main'] = 'src/index.js'
pkg.scripts.lint = 'aegir-lint'
pkg.scripts.test = 'aegir-test'
pkg.scripts['test:node'] = 'aegir-test node'
pkg.scripts['test:browser'] = 'aegir-test browser'
pkg.scripts.build = 'aegir-build'
pkg.scripts.release = 'aegir-release'
pkg.scripts.coverage = 'aegir-coverage'
pkg.scripts['coverage-publish'] = 'aegir-coverage publish'
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 === 'istanbul' ||
dep === 'webpack' ||
dep === 'dignified.js' ||
dep === 'standard') {
delete pkg.devDependencies[dep]
}
})
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(pkg, null, 2))
// New dependencies
// - chai
// - aegir
// - pre-commit
console.log('Adding chai and aegir')
exec('npm install --save-dev aegir', {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)
// Add circle.yml
console.log('Copying circle.yml')
exec('cp ../../circle.example.yml circle.yml', {cwd})
// Add .travis.yml
console.log('Copying .travis.yml')
exec('cp ../../travis.example.yml .travis.yml', {cwd})
console.log('Updating README.md')
let readme = fs.readFileSync(path.join(cwd, 'README.md')).toString()
readme = readme.replace('[![dignified.js](https://img.shields.io/badge/uses-dignified.js-blue.svg?style=flat-square)](https://github.com/dignifiedquire/dignified.js)\n', '')
fs.writeFileSync(path.join(cwd, 'README.md'), readme)
}