-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrename.js
More file actions
96 lines (92 loc) · 2.95 KB
/
rename.js
File metadata and controls
96 lines (92 loc) · 2.95 KB
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
import getBowerRegistryConfig from '../getBowerRegistryConfig.js'
import authenticate from './autenticate.js'
import bower from 'bower'
import chalk from 'chalk'
import inquirer from 'inquirer'
import fetch from 'node-fetch'
import path from 'path'
import Plugin from '../Plugin.js'
export default async function rename ({
logger,
oldName,
newName,
cwd = process.cwd()
} = {}) {
cwd = path.resolve(process.cwd(), cwd)
const BOWER_REGISTRY_CONFIG = getBowerRegistryConfig({ cwd })
if (!oldName || !newName) {
logger?.error('You must call rename with the following arguments: <plugin name> <new plugin name>')
return
}
// use Plugin to standardise name
newName = new Plugin({ name: newName, logger }).packageName
oldName = new Plugin({ name: oldName, logger }).packageName
logger?.warn('Using registry at', BOWER_REGISTRY_CONFIG.register)
logger?.warn(`Plugin will be renamed from ${oldName} to ${newName}`)
try {
const oldExists = await exists(BOWER_REGISTRY_CONFIG, oldName)
if (!oldExists) throw new Error(`Plugin "${oldName}" does not exist`)
const newExists = await exists(BOWER_REGISTRY_CONFIG, newName)
if (newExists) throw new Error(`Name "${newName}" already exists`)
const { username, token, type } = await authenticate({ pluginName: oldName })
logger?.log(`${username} authenticated as ${type}`)
await confirm()
await renameInBowerRepo({
username,
token,
oldName,
newName,
BOWER_REGISTRY_CONFIG
})
logger?.log(chalk.green('The plugin was successfully renamed.'))
} catch (err) {
logger?.error(err)
logger?.error('The plugin was not renamed.')
}
}
async function confirm () {
const schema = [
{
name: 'ready',
message: chalk.cyan('Confirm rename now?'),
type: 'confirm',
default: true
}
]
const confirmation = await inquirer.prompt(schema)
if (!confirmation.ready) throw new Error('Aborted. Nothing has been renamed.')
}
async function renameInBowerRepo ({
username,
token,
oldName,
newName,
BOWER_REGISTRY_CONFIG
}) {
const path = 'packages/rename/' + username + '/' + oldName + '/' + newName
const query = '?access_token=' + token
const response = await fetch(BOWER_REGISTRY_CONFIG.register + path + query, {
method: 'GET',
headers: { 'User-Agent': 'adapt-cli' },
followRedirect: false
})
if (response.status !== 201) throw new Error(`The server responded with ${response.status}`)
}
/**
* @param {Plugin} plugin
* @returns {boolean}
*/
async function exists (BOWER_REGISTRY_CONFIG, plugin) {
const pluginName = plugin.toString().toLowerCase()
return new Promise((resolve, reject) => {
// TODO: npm implementation
bower.commands.search(pluginName, {
registry: BOWER_REGISTRY_CONFIG.register
})
.on('end', result => {
const matches = result.filter(({ name }) => name.toLowerCase() === pluginName)
resolve(Boolean(matches.length))
})
.on('error', reject)
})
}