This repository was archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.js
executable file
·121 lines (102 loc) · 3.14 KB
/
bin.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
#!/usr/bin/env node
const path = require('path')
const args = require('args')
const execa = require('execa')
const confirm = require('positive')
const fsp = require('fs-promise')
args
.option('save', 'Run transform for real')
.option('npm', 'Force use npm instead of detected')
.option('yarn', 'Force use yarn instead of detected')
const flags = args.parse(process.argv)
const hasYarnLock = () => fsp.exists(path.join(process.cwd(), 'yarn.lock'))
const getPackageManager = async () => {
if (flags.npm) {
console.log('Forcing npm')
return 'npm'
}
if (flags.yarn) {
console.log('Forcing yarn')
return 'yarn'
}
if (await hasYarnLock()) {
console.log('detected yarn.lock', 'using yarn')
return 'yarn'
}
console.log(`Didn't find yarn.lock, using npm`)
return 'npm'
}
const jscodeshift = (args = ['--help']) => {
const child = execa(path.join(__dirname, 'node_modules', '.bin', 'jscodeshift'), args)
const stream = child.stdout
stream.pipe(process.stdout)
return child
}
const transform = async (pwd, _opts) => {
const hasGitignore = await fsp.exists(path.join(process.cwd(), '.gitignore'))
if (!hasGitignore) {
throw new Error('You need to have a .gitignore, ignore node_modules etc')
}
const opts = Object.assign({ dry: true }, _opts)
const gitignore = path.join(pwd, '.gitignore')
const args = [
pwd + '/',
'--transform', path.join(__dirname, 'codemod.js'),
'--ignore-config', gitignore
]
if (opts.dry) {
args.push('--dry')
args.push('--print')
console.log('🐪 Running in dry mode (no changes will be made)')
console.log('use --save to save the changes')
} else {
const sure = confirm('🚨 WARNING! This will edit your files, be sure to save any edited files [y/N]: ', false)
if (!sure) {
console.log('Exiting')
process.exit(0)
}
console.log('Running 4 real 🎉')
}
console.log('') // pad
console.log('') // pad
return jscodeshift(args)
}
async function cli () {
const folder = path.join(process.cwd())
console.log('Running in', folder)
const opts = {
dry: !flags.save
}
await transform(folder, opts)
console.log('') // pad
console.log('') // pad
console.log('Transform done! 🚡')
if (flags.save) {
console.log('Removing co from package.json...')
const packageManager = await getPackageManager()
console.log('Removing co dependencies using', packageManager)
if (packageManager === 'yarn') {
await execa(packageManager, ['remove', 'co'])
.catch(error => {
if (error.stderr === 'error This module isn\'t specified in a manifest.\n') {
console.log('looks like co is already removed, moving on..')
return
}
throw error
})
.then(() => console.log('package.json and yarn.lock updated!'))
} else if (packageManager === 'npm') {
await execa(packageManager, ['uninstall', 'co'])
console.log('package.json updated!')
} else {
throw new Error(`Unknown packageManager ${packageManager}`)
}
}
}
cli()
.then(() => {
console.log('Done 🎉')
})
.catch((error) => {
console.error('Error 💀', error)
})