Skip to content

Commit 541ffb8

Browse files
committed
git-node: restructure using commandDir of yargs
1 parent b94a608 commit 541ffb8

File tree

10 files changed

+351
-366
lines changed

10 files changed

+351
-366
lines changed

bin/get-metadata

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
const path = require('path');
55
const { runAsync } = require('../lib/run');
66

7-
const script = path.join(
8-
__dirname, '..', 'components', 'git', 'git-node-metadata');
9-
runAsync(script, process.argv.slice(2));
7+
const script = path.join(__dirname, 'git-node');
8+
runAsync(script, ['metadata'].concat(process.argv.slice(2)));

bin/git-node

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
const CMD = process.argv[2];
4+
const yargs = require('yargs');
55
const path = require('path');
6-
const { runAsync } = require('../lib/run');
7-
const fs = require('fs');
6+
const epilogue = require('../components/git/epilogue');
87

9-
if (!CMD) {
10-
console.log('Run `git node help` to see how to use this');
11-
process.exit(1);
12-
}
8+
const commandDir = path.join(__dirname, '..', 'components', 'git');
139

14-
const script = path.join(
15-
__dirname, '..', 'components', 'git', `git-node-${CMD}`);
16-
if (!fs.existsSync(script)) {
17-
console.error(`No such command: git node ${CMD}`);
18-
process.exit(1);
19-
}
20-
21-
runAsync(process.execPath, [script, ...process.argv.slice(3)]);
10+
/* eslint-disable no-unused-expressions */
11+
yargs
12+
.commandDir(commandDir, {
13+
exclude: /epilogue/
14+
})
15+
.command('help', false, () => {}, (yargs) => { yargs.showHelp(); })
16+
.demandCommand(1)
17+
.epilogue(epilogue)
18+
.help('help')
19+
.argv;
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
#!/usr/bin/env node
1+
'use strict';
22

3-
console.log(`
4-
Steps to get metadata for a pull request:
5-
==============================================================================
6-
$ cd path/to/node/project
7-
$ git node metadata $PRID # Retrieves metadata for a PR and validates
8-
# them against nodejs/node PR rules
9-
==============================================================================
10-
11-
Steps to land a pull request:
3+
module.exports = `Steps to land a pull request:
124
==============================================================================
135
$ cd path/to/node/project
146
$ git node land --abort # Abort a landing session, just in case
@@ -21,4 +13,4 @@ $ git rebase --continue # Repeat until the rebase is done
2113
2214
$ git node land --final # Verify all the commit messages
2315
==============================================================================
24-
Watch https://asciinema.org/a/148627 for a complete demo`);
16+
Watch https://asciinema.org/a/148627 for a complete demo`;

components/git/git-node-land

Lines changed: 0 additions & 85 deletions
This file was deleted.

components/git/git-node-metadata

Lines changed: 0 additions & 18 deletions
This file was deleted.

components/git/land.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
'use strict';
2+
3+
const getMetadata = require('../metadata');
4+
const CLI = require('../../lib/cli');
5+
const Request = require('../../lib/request');
6+
const { runPromise } = require('../../lib/run');
7+
const LandingSession = require('../../lib/landing_session');
8+
const epilogue = require('./epilogue');
9+
const yargs = require('yargs');
10+
11+
const landOptions = {
12+
apply: {
13+
describe: 'Apply a patch with the given PR id',
14+
type: 'number'
15+
},
16+
amend: {
17+
describe: 'Amend the current commit',
18+
type: 'boolean'
19+
},
20+
continue: {
21+
alias: 'c',
22+
describe: 'Continue the landing session',
23+
type: 'boolean'
24+
},
25+
final: {
26+
describe: 'Verify the landed PR and clean up',
27+
type: 'boolean'
28+
},
29+
abort: {
30+
describe: 'Abort the current landing session',
31+
type: 'boolean'
32+
}
33+
};
34+
35+
function builder(yargs) {
36+
return yargs
37+
.options(landOptions).positional('prid', {
38+
describe: 'ID of the Pull Request',
39+
type: 'number'
40+
})
41+
.epilogue(epilogue)
42+
.example('git node land 12344',
43+
'Land https://github.com/nodejs/node/pull/12344 in the current directory')
44+
.example('git node land --abort',
45+
'Abort the current session')
46+
.example('git node land --amend',
47+
'Append metadata to the current commit message')
48+
.example('git node land --final',
49+
'Verify the landed PR and clean up')
50+
.example('git node land --continue',
51+
'Continue the current landing session');
52+
}
53+
54+
const START = 'start';
55+
const APPLY = 'apply';
56+
const AMEND = 'amend';
57+
const FINAL = 'final';
58+
const CONTINUE = 'continue';
59+
const ABORT = 'abort';
60+
61+
function handler(argv) {
62+
if (argv.prid && Number.isInteger(argv.prid)) {
63+
return land(START, argv);
64+
}
65+
const provided = [];
66+
for (const type of Object.keys(landOptions)) {
67+
if (argv[type]) {
68+
provided.push(type);
69+
}
70+
}
71+
if (provided.length === 0) {
72+
yargs.showHelp();
73+
return;
74+
}
75+
if (provided.length > 1) {
76+
yargs.showHelp();
77+
return;
78+
}
79+
80+
return land(provided[0], argv);
81+
}
82+
83+
function land(state, argv) {
84+
const cli = new CLI(process.stderr);
85+
const req = new Request();
86+
const dir = process.cwd();
87+
88+
return runPromise(main(state, argv, cli, req, dir)).catch((err) => {
89+
if (cli.spinner.enabled) {
90+
cli.spinner.fail();
91+
}
92+
throw err;
93+
});
94+
}
95+
96+
module.exports = {
97+
command: 'land [prid|options]',
98+
describe:
99+
'Manage the current landing session or start a new one for a pull request',
100+
builder,
101+
handler
102+
};
103+
104+
async function main(state, argv, cli, req, dir) {
105+
let session = new LandingSession(cli, req, dir);
106+
107+
try {
108+
session.restore();
109+
} catch (err) { // JSON error?
110+
if (state === ABORT) {
111+
await session.abort();
112+
return;
113+
}
114+
cli.warn(
115+
'Failed to detect previous session. ' +
116+
'please run `git node land --abort`');
117+
return;
118+
}
119+
120+
if (state === START) {
121+
if (session.hasStarted()) {
122+
cli.warn(
123+
'Previous `git node land` session for ' +
124+
`${session.pullName} in progress.`);
125+
cli.log('run `git node land --abort` before starting a new session');
126+
return;
127+
}
128+
session = new LandingSession(cli, req, dir, argv.prid);
129+
const { repo, owner, prid } = session;
130+
const metadata = await getMetadata({ repo, owner, prid }, cli);
131+
return session.start(metadata);
132+
} else if (state === APPLY) {
133+
return session.apply();
134+
} else if (state === AMEND) {
135+
return session.amend();
136+
} else if (state === FINAL) {
137+
return session.final();
138+
} else if (state === ABORT) {
139+
return session.abort();
140+
} else if (state === CONTINUE) {
141+
return session.continue();
142+
}
143+
}

0 commit comments

Comments
 (0)