-
Notifications
You must be signed in to change notification settings - Fork 92
/
split.js
executable file
·155 lines (136 loc) · 4.25 KB
/
split.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
#!/usr/bin/env node
const execa = require('execa');
const gitUrlParse = require('git-url-parse');
const ora = require('ora');
const path = require('path');
const program = require('commander');
const find = require('lodash/find');
const kebabCase = require('lodash/kebabCase');
const getBranchUrl = async (branch) => {
const { stdout: remoteUrl } = await execa('git', [
'config',
'--get',
'remote.origin.url',
]);
const gitHttpsUrl = gitUrlParse(remoteUrl).toString('https');
return `git+${gitHttpsUrl}#${branch}`;
};
const getCurrentBranch = () =>
execa('git', ['rev-parse', '--abbrev-ref', 'HEAD']).then(
({ stdout: branchName }) => branchName,
);
const getPackageInfos = (name) =>
execa('lerna', ['list', '-la', '--json']).then(({ stdout: packageList }) => {
const searchPackage = find(JSON.parse(packageList), { name });
if (!searchPackage) {
return Promise.reject(new Error(`"${name}" package not found`));
}
if (searchPackage.private) {
return Promise.reject(
new Error(`"${searchPackage.name}" is a private package`),
);
}
return searchPackage;
});
const getSubtreeBranch = (packageName, currentBranch) => {
const [scope, name] = packageName.split('/');
const leftPart = name || scope;
return `split-${leftPart}/${kebabCase(currentBranch)}`;
};
const subtreeSplit = (prefix, branch) =>
execa('git', [
'subtree',
'split',
`--prefix=${prefix}`,
`--branch=${branch}`,
]);
const pushBranch = (branch) => execa('git', ['push', 'origin', branch]);
const subtreePush = (prefix, remote, branch) =>
execa('git', ['subtree', 'push', `--prefix=${prefix}`, remote, branch]);
const split = async (packageName, { branch, remote, push }) => {
let spinner = ora('Search Package infos').start();
try {
const packageInfos = await getPackageInfos(packageName);
const currentBranch = await getCurrentBranch();
spinner.succeed();
const branchName =
branch ||
(remote
? currentBranch
: getSubtreeBranch(packageInfos.name, currentBranch));
const prefix = path.relative(process.cwd(), packageInfos.location);
if (!remote) {
spinner = ora(
`Create subtree branch "${branchName}" from "${packageName}" package (${prefix})`,
).start();
await subtreeSplit(prefix, branchName);
spinner.succeed();
if (push) {
spinner = ora(`Pushing "${branchName}" to origin`).start();
await pushBranch(branchName);
spinner.succeed();
}
console.log('🎉 And now, you should:\n');
if (!push) {
console.log(`📤 push your branch:
git push origin ${branchName}
`);
}
const url = await getBranchUrl(branchName);
console.log(`📦 use the splitted branch in your repository:
yarn add ${url}
`);
} else {
spinner = ora(
`Create remote subtree branch "${branchName}" from "${packageName}" package (${prefix}) on ${remote}`,
).start();
await subtreePush(prefix, remote, branchName);
spinner.succeed();
}
} catch (error) {
spinner.fail();
console.error(error.message);
}
};
program
.description(
'Generate a subtree branch (from the current one) for the specified package',
)
.usage('[options] <packageName>')
.option('-b, --branch [branch]', 'set a custom branch name')
.option(
'-p, --push',
'push the splitted branch to origin (useless with "remote" option)',
)
.option(
'-r, --remote [remote]',
'remote url (like git@github.com:ovh-ux/manager.git)',
)
.addHelpText(
'after',
`
Examples:
* Split a module:
$ split @ovh-ux/manager-core
* Split a module and push the subtree branch:
$ split @ovh-ux/manager-core --push
* Split a module and specify a branch name:
$ split @ovh-ux/manager-core --branch manager-core/split
* Split a module and push it to another repository:
$ split @ovh-ux/manager-core --remote git@github.com:ovh-ux/manager.git
`,
)
.action(() => {
const [packageName] = program.args;
const { branch, push, remote } = program.opts();
if (packageName) {
split(packageName, {
branch: branch || false,
push,
remote: remote || false,
});
} else {
program.outputHelp();
}
})
.parse(process.argv);