Skip to content

Commit b9d3e75

Browse files
committed
fix: export classes for commands
1 parent a894e5e commit b9d3e75

File tree

14 files changed

+307
-379
lines changed

14 files changed

+307
-379
lines changed

__tests__/core.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('testing core functionalities', () => {
6262
test('config value from file is wrong, revert to defaults', () => {
6363
sh.ShellString(
6464
`{
65-
"development": "asldk /.. ,./3"
65+
"development": "asldk /.. ,./3"
6666
}`
6767
).to(tempConfigFile)
6868

bin/cli.js

Lines changed: 156 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ function _interopDefault(e) {
1313
return e && "object" == typeof e && "default" in e ? e.default : e;
1414
}
1515

16-
var yargs = _interopDefault(require("yargs")), shelljs = require("shelljs"), inquirer = _interopDefault(require("inquirer")), findUp = _interopDefault(require("find-up")), path = require("path"), chalk = _interopDefault(require("chalk")), child_process = require("child_process");
17-
18-
function getDefaultConfigValues() {
19-
return {
20-
...defaultConfigValues,
21-
...loadConfigValues()
22-
};
23-
}
16+
var yargs = _interopDefault(require("yargs")), shelljs = require("shelljs"), findUp = _interopDefault(require("find-up")), path = require("path"), inquirer = _interopDefault(require("inquirer")), child_process = require("child_process"), chalk = _interopDefault(require("chalk"));
2417

2518
function loadConfigFile(e) {
2619
if (!e || !shelljs.test("-f", e)) return defaultConfigValues;
@@ -165,26 +158,120 @@ function generateCommentedValues(e) {
165158
return a;
166159
}
167160

168-
const success = e => chalk.black.bgGreen(e), error = e => chalk.black.bgRed(e), info = e => chalk.black.bgCyan(e);
169-
170-
var init = {
171-
command: "init [options]",
172-
desc: "Generate a config file",
173-
builder: e => e.option("y", {
174-
alias: "default-values",
175-
describe: "Auto-generate config file using default values. These values WILL NOT overwrite existing values!"
176-
}),
177-
handler: async function(e) {
178-
try {
179-
const a = e.defaultValues ? getDefaultConfigValues() : await inquirer.prompt(generateQuestions(e));
180-
console.log(JSON.stringify(a, null, 2)), (e.defaultValues || await askConfirmationBeforeWrite()) && (writeConfigFile({
181-
data: a
182-
}) ? console.log(success("Initialisation done!")) : console.error(error("Cannot write config file!")));
183-
} catch (e) {
184-
console.error(error(e));
185-
}
161+
var name = "git-oneflow";
162+
163+
function _defineProperty(e, a, t) {
164+
return a in e ? Object.defineProperty(e, a, {
165+
value: t,
166+
enumerable: !0,
167+
configurable: !0,
168+
writable: !0
169+
}) : e[a] = t, e;
170+
}
171+
172+
class StartFeature {
173+
constructor() {
174+
_defineProperty(this, "command", "start <featureBranch>"), _defineProperty(this, "describe", "Start a new feature"),
175+
_defineProperty(this, "handler", async e => {
176+
const a = e.usedev ? e.development : e.main;
177+
isValidBranchName(e.featureBranch) && shelljs.exec(`git checkout -b ${e.feature}/${e.featureBranch} ${a}`);
178+
});
179+
}
180+
}
181+
182+
class FinishFeature {
183+
constructor() {
184+
_defineProperty(this, "command", "finish <featureBranch> [options]"), _defineProperty(this, "describe", "Finish a feature"),
185+
_defineProperty(this, "builder", e => e.option("i", {
186+
alias: "interactive",
187+
describe: "Rebase using `rebase -i`. It applies only if `integration` option is set to 1 or 3"
188+
})), _defineProperty(this, "handler", e => {
189+
const a = e.usedev ? e.development : e.main;
190+
if (isValidBranchName(a)) return handleFinish(e, a);
191+
});
192+
}
193+
}
194+
195+
async function handleFinish(e, a) {
196+
2 !== e.integration && await rebaseStep(e, a), shelljs.exec(`git checkout ${a}`);
197+
let t = "--no-ff";
198+
switch (2 === e.integration && (t = "--ff-only"), shelljs.exec(`git merge ${t} ${e.feature}/${e.featureBranch}`),
199+
e.push) {
200+
case "always":
201+
shelljs.exec(`git push origin ${a}`);
202+
break;
203+
204+
case "never":
205+
break;
206+
207+
case "ask":
208+
await ask(`Do you want to push to ${a}?`) && shelljs.exec(`git push origin ${a}`);
209+
}
210+
switch (e.deleteBranch) {
211+
case "always":
212+
shelljs.exec(`git branch -d ${e.feature}/${e.featureBranch}`);
213+
break;
214+
215+
case "never":
216+
break;
217+
218+
case "ask":
219+
await ask(`Do you want to delete branch ${e.feature}/${e.featureBranch}?`) && shelljs.exec(`git branch -d ${e.feature}/${e.featureBranch}`);
220+
}
221+
}
222+
223+
async function rebaseStep(e, a) {
224+
switch (shelljs.exec(`git checkout ${e.feature}/${e.featureBranch}`), e.interactive) {
225+
case "always":
226+
child_process.spawnSync("git", [ "rebase", "-i", `${a}` ], {
227+
stdio: "inherit"
228+
});
229+
break;
230+
231+
case "never":
232+
shelljs.exec(`git rebase ${a}`);
233+
break;
234+
235+
case "ask":
236+
await ask("Do you want to use rebase interactively?") ? child_process.spawnSync("git", [ "rebase", "-i", `${a}` ], {
237+
stdio: "inherit"
238+
}) : shelljs.exec(`git rebase ${a}`);
239+
}
240+
}
241+
242+
async function ask(e) {
243+
return (await inquirer.prompt([ {
244+
type: "confirm",
245+
name: "accept",
246+
message: e
247+
} ])).accept;
248+
}
249+
250+
class Feature {
251+
constructor() {
252+
_defineProperty(this, "command", "feature <command>"), _defineProperty(this, "describe", "Manage starting and finishing features"),
253+
_defineProperty(this, "builder", e => e.command(new StartFeature()).command(new FinishFeature())),
254+
_defineProperty(this, "handler", () => {});
255+
}
256+
}
257+
258+
const success = e => chalk.black.bgGreen(e), warning = e => chalk.black.bgYellow(e), error = e => chalk.black.bgRed(e), info = e => chalk.black.bgCyan(e);
259+
260+
class Init {
261+
constructor() {
262+
_defineProperty(this, "command", "init [options]"), _defineProperty(this, "describe", "Generate a config file"),
263+
_defineProperty(this, "handler", async e => {
264+
try {
265+
const a = await inquirer.prompt(generateQuestions(e));
266+
console.log(JSON.stringify(a, null, 2)), await askConfirmationBeforeWrite() && (writeConfigFile({
267+
data: a
268+
}) ? console.log(success("Initialisation done!")) : console.error(error("Cannot write config file!")));
269+
} catch (e) {
270+
console.error(error(e));
271+
}
272+
});
186273
}
187-
};
274+
}
188275

189276
function generateQuestions(e) {
190277
return [ {
@@ -316,112 +403,21 @@ async function askConfirmationBeforeWrite() {
316403
} ])).write;
317404
}
318405

319-
var start = {
320-
command: "start <featureBranch>",
321-
desc: "Start a new feature",
322-
builder: e => {},
323-
handler: async e => {
324-
const a = e.usedev ? e.development : e.main;
325-
isValidBranchName(e.featureBranch) && shelljs.exec(`git checkout -b ${e.feature}/${e.featureBranch} ${a}`);
326-
}
327-
}, finish = {
328-
command: "finish <featureBranch> [options]",
329-
desc: "Finish a feature",
330-
builder: e => e.option("i", {
331-
alias: "interactive",
332-
describe: "Rebase using `rebase -i`. It applies only if `integration` option is set to 1 or 3"
333-
}),
334-
handler: e => {
335-
const a = e.usedev ? e.development : e.main;
336-
try {
337-
return handleFinish(e, a);
338-
} catch (e) {
339-
throw e;
340-
}
341-
}
342-
};
343-
344-
async function handleFinish(e, a) {
345-
2 !== e.integration && await rebaseStep(e, a), shelljs.exec(`git checkout ${a}`);
346-
let t = "--no-ff";
347-
switch (2 === e.integration && (t = "--ff-only"), shelljs.exec(`git merge ${t} ${e.feature}/${e.featureBranch}`),
348-
e.push) {
349-
case "always":
350-
shelljs.exec(`git push origin ${a}`);
351-
break;
352-
353-
case "never":
354-
break;
355-
356-
case "ask":
357-
await ask(`Do you want to push to ${a}?`) && shelljs.exec(`git push origin ${a}`);
358-
}
359-
switch (e.deleteBranch) {
360-
case "always":
361-
shelljs.exec(`git branch -d ${e.feature}/${e.featureBranch}`);
362-
break;
363-
364-
case "never":
365-
break;
366-
367-
case "ask":
368-
await ask(`Do you want to delete branch ${e.feature}/${e.featureBranch}?`) && shelljs.exec(`git branch -d ${e.feature}/${e.featureBranch}`);
369-
}
370-
}
371-
372-
async function rebaseStep(e, a) {
373-
switch (shelljs.exec(`git checkout ${e.feature}/${e.featureBranch}`), e.interactive) {
374-
case "always":
375-
child_process.spawnSync("git", [ "rebase", "-i", `${a}` ], {
376-
stdio: "inherit"
406+
class StartRelease {
407+
constructor() {
408+
_defineProperty(this, "command", "start <releaseName> <from>"), _defineProperty(this, "describe", "Start a new release.\n <releaseName> should be something like `2.3.0`.\n <from> should be a branch (e.g. develop) or a commit (e.g. 9af345)"),
409+
_defineProperty(this, "handler", e => {
410+
!isValidBranchName(e.releaseName) || e.from && !isValidBranchName(e.from) || shelljs.exec(`git checkout -b ${e.release}/${e.releaseName} ${e.from}`);
377411
});
378-
break;
379-
380-
case "never":
381-
shelljs.exec(`git rebase ${a}`);
382-
break;
383-
384-
case "ask":
385-
await ask("Do you want to use rebase interactively?") ? child_process.spawnSync("git", [ "rebase", "-i", `${a}` ], {
386-
stdio: "inherit"
387-
}) : shelljs.exec(`git rebase ${a}`);
388412
}
389413
}
390414

391-
async function ask(e) {
392-
return (await inquirer.prompt([ {
393-
type: "confirm",
394-
name: "accept",
395-
message: e
396-
} ])).accept;
397-
}
398-
399-
var feature = {
400-
command: "feature <command>",
401-
desc: "Manage starting and finishing features",
402-
builder: function(e) {
403-
return e.command(start).command(finish);
404-
},
405-
handler: function(e) {}
406-
}, start$1 = {
407-
command: "start <releaseName> <from>",
408-
desc: "Start a new release.\n<releaseName> should be something like `2.3.0`.\n<from> should be a branch (e.g. develop) or a commit (e.g. 9af345)",
409-
builder: e => {},
410-
handler: e => {
411-
isValidBranchName(e.releaseName) && shelljs.exec(`git checkout -b ${e.release}/${e.releaseName} ${e.from}`);
415+
class FinishRelease {
416+
constructor() {
417+
_defineProperty(this, "command", "finish <releaseName>"), _defineProperty(this, "desc", "Finishes a release."),
418+
_defineProperty(this, "handler", async e => handleFinish$1(e));
412419
}
413-
}, finish$1 = {
414-
command: "finish <releaseName>",
415-
desc: "Finishes a release.",
416-
builder: e => {},
417-
handler: async e => {
418-
try {
419-
return handleFinish$1(e);
420-
} catch (e) {
421-
throw e;
422-
}
423-
}
424-
};
420+
}
425421

426422
async function handleFinish$1(e) {
427423
const a = e.usedev ? e.development : e.main;
@@ -468,32 +464,30 @@ async function ask$1(e) {
468464
} ])).accept;
469465
}
470466

471-
var release = {
472-
command: "release <command>",
473-
desc: "Manage starting and finishing releases.",
474-
builder: function(e) {
475-
return e.command(start$1).command(finish$1);
476-
},
477-
handler: function(e) {}
478-
}, start$2 = {
479-
command: "start <hotfixName> <from>",
480-
desc: "Start a new hotfix.\n<hotfixName> should be something like `2.3.1`.\n<from> should be a branch (e.g. develop), a tag (e.g. 2.3.0) or a commit (e.g. 9af345)",
481-
builder: e => {},
482-
handler: e => {
483-
isValidBranchName(e.hotfixName) && shelljs.exec(`git checkout -b ${e.hotfix}/${e.hotfixName} ${e.from}`);
467+
class Release {
468+
constructor() {
469+
_defineProperty(this, "command", "release <command>"), _defineProperty(this, "desc", "Manage starting and finishing releases."),
470+
_defineProperty(this, "builder", function(e) {
471+
return e.command(new StartRelease()).command(new FinishRelease());
472+
}), _defineProperty(this, "handler", () => {});
484473
}
485-
}, finish$2 = {
486-
command: "finish <hotfixName>",
487-
desc: "Finishes a hotfix.",
488-
builder: e => {},
489-
handler: async e => {
490-
try {
491-
return handleFinish$2(e);
492-
} catch (e) {
493-
throw e;
494-
}
474+
}
475+
476+
class StartHotfix {
477+
constructor() {
478+
_defineProperty(this, "command", "start <hotfixName> <from>"), _defineProperty(this, "describe", "Start a new hotfix.\n <hotfixName> should be something like `2.3.1`.\n <from> should be a branch (e.g. develop), a tag (e.g. 2.3.0) or a commit (e.g. 9af345)"),
479+
_defineProperty(this, "handler", e => {
480+
!isValidBranchName(e.hotfixName) || e.from && !isValidBranchName(e.from) || shelljs.exec(`git checkout -b ${e.hotfix}/${e.hotfixName} ${e.from}`);
481+
});
495482
}
496-
};
483+
}
484+
485+
class FinishHotfix {
486+
constructor() {
487+
_defineProperty(this, "command", "finish <hotfixName>"), _defineProperty(this, "describe", "Finishes a hotfix."),
488+
_defineProperty(this, "handler", async e => handleFinish$2(e));
489+
}
490+
}
497491

498492
async function handleFinish$2(e) {
499493
const a = e.usedev ? e.development : e.main;
@@ -540,18 +534,17 @@ async function ask$2(e) {
540534
} ])).accept;
541535
}
542536

543-
var hotfix = {
544-
command: "hotfix <command>",
545-
desc: "Manage starting and finishing hotfixes.",
546-
builder: function(e) {
547-
return e.command(start$2).command(finish$2);
548-
},
549-
handler: function(e) {}
550-
}, name = "git-oneflow";
537+
class Hotfix {
538+
constructor() {
539+
_defineProperty(this, "command", "hotfix <command>"), _defineProperty(this, "describe", "Manage starting and finishing hotfixes."),
540+
_defineProperty(this, "builder", e => e.command(new StartHotfix()).command(new FinishHotfix())),
541+
_defineProperty(this, "handler", () => {});
542+
}
543+
}
551544

552-
shelljs.which("git") || (console.error("Sorry, git-OneFlow requires git... it's in the name"),
545+
shelljs.which("git") && (console.error("Sorry, git-OneFlow requires git... it's in the name"),
553546
process.exit(1));
554547

555-
var argv = yargs.scriptName(name).version().alias("v", "version").config(loadConfigValues()).pkgConf("git-oneflow").command(init).command(feature).command(release).command(hotfix).help().alias("h", "help").argv;
548+
const argv = yargs.scriptName(name).version().alias("v", "version").config(loadConfigValues()).pkgConf("git-oneflow").command(new Init()).command(new Feature()).command(new Release()).command(new Hotfix()).help().alias("h", "help").argv;
556549

557-
argv._.length <= 0 && console.log(`Try ${path.basename(process.argv[1])} --help`);
550+
argv._.length <= 0 && console.log(warning(`Try ${path.basename(process.argv[1])} --help`));

0 commit comments

Comments
 (0)