-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
174 lines (147 loc) · 4.57 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const chalk = require('chalk');
const yaml = require('js-yaml');
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const unlink = promisify(fs.unlink);
let config;
let unpublishedPackages = {};
const {argv} = yargs
.option('config', {
alias: 'c',
describe: 'The path to the config.yaml file',
default: path.join(__dirname, 'config.yaml'),
type: 'string'
})
.option('package-name-prefix', {
alias: 'p',
describe: 'String to inject as a prefix to the package name in package.json',
type: 'string'
})
.option('bower-file', {
alias: 'i',
describe: 'The path to the bower.json file',
default: './bower.json',
type: 'string'
})
.option('package-file', {
alias: 'o',
describe: 'The path to the package.json file',
default: './package.json',
type: 'string'
})
.option('overwrite-duplicates', {
alias: 'd',
describe: 'If set to `true`, duplicate dependecies will resolve to the dependency defined in bower.json',
default: false,
type: 'boolean'
})
.normalize('config')
.normalize('bower-file')
.normalize('package-file')
.help();
try {
config = yaml.safeLoad(fs.readFileSync(argv.config, 'utf8'));
} catch (error) {
displayError(error.message);
}
function displayMessage(category = '', message = '') {
console.log(`${chalk.dim('[%s]')} ${chalk.green('➟')} %s`, category, message);
}
function displayWarning(message = '') {
console.error(`${chalk.yellow('⚠ Warning ➟')} ${message}`);
}
function displayError(message = '') {
console.error(`${chalk.red('⚠ Error ➟')} ${message}`);
}
readFile(argv.bowerFile, 'utf8')
.then((data) => {
try {
data = JSON.parse(data);
} catch (error) {
throw new Error(error);
}
if (!data.hasOwnProperty('dependencies')) {
return {};
}
for (const key in config.packageResolves) {
if (data.dependencies.hasOwnProperty(key)) {
data.dependencies[config.packageResolves[key]] = data.dependencies[key];
delete data.dependencies[key];
}
}
// Clean up git@ urls (yarn doesn't like those)
for (const key in data.dependencies) {
if (data.dependencies[key].indexOf('git@') === 0) {
data.dependencies[key] = 'git+ssh://' + data.dependencies[key];
}
if (data.dependencies[key].indexOf('git') === 0) {
unpublishedPackages[key] = data.dependencies[key];
}
}
return data.dependencies;
})
.then((bowerDependencies) => {
return readFile(argv.packageFile, 'utf8')
.then((data) => {
try {
data = JSON.parse(data);
} catch (error) {
throw new Error(error);
}
if (!data.hasOwnProperty('dependencies')) {
data.dependencies = bowerDependencies;
for (const key in bowerDependencies) {
displayMessage('move-dep', `Moving dependency ${chalk.cyan(key)}`);
}
} else {
for (const key in bowerDependencies) {
if (!data.dependencies.hasOwnProperty(key) || (data.dependencies.hasOwnProperty(key) && config.overwriteDuplicates)) {
displayMessage('move-dep', `Moving dependency ${chalk.cyan(key)}`);
data.dependencies[key] = bowerDependencies[key];
} else {
displayMessage('move-dep', `Skipping duplicate dependency ${chalk.cyan(key)}`);
}
}
}
// Prepend name prefix if defined
if (argv.packageNamePrefix && data.name.indexOf(argv.packageNamePrefix) !== 0) {
data.name = argv.packageNamePrefix + data.name;
}
// Clean up git@ urls (yarn doesn't like those)
if (data.hasOwnProperty('devDependencies')) {
for (const key in data.devDependencies) {
if (data.devDependencies[key].indexOf('git@') === 0) {
data.devDependencies[key] = 'git+ssh://' + data.devDependencies[key];
}
if (data.devDependencies[key].indexOf('git') === 0) {
unpublishedPackages[key] = data.devDependencies[key];
}
}
}
// Remove bower dependency (if presxent)
if (data.hasOwnProperty('devDependencies')) {
delete data.devDependencies.bower;
}
return writeFile(argv.packageFile, JSON.stringify(data, null, 2));
});
})
.then(() => {
displayMessage('clean-up', `Removing ${argv.bowerFile}`);
return unlink(argv.bowerFile);
})
.then(() => {
console.log('');
for (const key in unpublishedPackages) {
displayWarning(`The ${chalk.cyan(key)} dependency is loaded from git. It should perhaps be published to a registry?`);
}
const words = ['Marvelous!', 'Splendid!', 'All done!', 'Superb!', 'Brilliant!', 'Magnificent!', 'Superlative!'];
console.log('👌 %s', chalk.bold.green(words[Math.floor(Math.random() * words.length)]));
})
.catch((error) => {
displayError(error.message);
});