Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef2932d

Browse files
authored
Merge pull request#44 from coderoad/fixes
Add "clean" = false option
2 parents36fd3f1 +ea6756f commitef2932d

File tree

5 files changed

+146
-23
lines changed

5 files changed

+146
-23
lines changed

‎src/help.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Usage: coderoad validate [path] [options]
4747
4848
Options:
4949
--help (-h) display these help docs
50+
--clean (-c) set to false to preserve .tmp folder. Helpful for debugging
5051
5152
More docs at https://github.com/coderoad/coderoad-cli`);
5253
}

‎src/utils/args.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,64 @@
1-
typeArgValueParams={name:string;alias?:string;param?:boolean};
1+
typeArgValueParams={
2+
name:string;
3+
alias?:string;
4+
type?:"string"|"bool"|"number";
5+
};
26

3-
constcheckValue=(
7+
functioncheckValue<T>(
48
args:string[],
59
string:string,
6-
options:ArgValueParams
7-
)=>{
10+
isBool:boolean
11+
):string|null{
812
constnameIndex=args.indexOf(string);
9-
if(nameIndex>-1){
10-
if(options.param){
11-
constvalue=args[nameIndex+1];
12-
if(!value){
13-
thrownewError(`Argument${string} is missing a parameter value`);
13+
if(nameIndex>=0){
14+
constnextArg=args[nameIndex+1];
15+
16+
if(nextArg!==undefined){
17+
constnextIsCommand=!!nextArg.match(/^\-/);
18+
if(nextIsCommand){
19+
returnisBool ?"true" :null;
1420
}
15-
returnvalue;
21+
returnnextArg;
22+
}else{
23+
// no secondary set value
24+
returnisBool ?"true" :null;
1625
}
1726
}
1827
returnnull;
19-
};
28+
}
2029

21-
exportfunctiongetArg(args:string[],options:ArgValueParams):string|null{
22-
letvalue:null|string=null;
30+
exportfunctiongetArg<T>(
31+
args:string[],
32+
options:ArgValueParams
33+
):string|boolean|number|null{
34+
letstringValue:null|string=null;
35+
constisBool=options.type==="bool";
2336

24-
constaliasString=`-${options.alias}`;
25-
value=checkValue(args,aliasString,options);
26-
if(!value){
37+
if(options.alias){
38+
constaliasString=`-${options.alias}`;
39+
stringValue=checkValue(args,aliasString,isBool);
40+
}
41+
if(!stringValue){
2742
constnameString=`--${options.name}`;
28-
value=checkValue(args,nameString,options);
43+
stringValue=checkValue(args,nameString,isBool);
44+
}
45+
46+
if(stringValue===null){
47+
returnnull;
2948
}
3049

31-
returnvalue;
50+
if(!options.type){
51+
options.type="string";
52+
}
53+
54+
// coerce type
55+
switch(options.type){
56+
case"bool":
57+
return(stringValue||"").toLowerCase()!=="false";
58+
case"number":
59+
returnNumber(stringValue);
60+
case"string":
61+
default:
62+
returnstringValue;
63+
}
3264
}

‎src/utils/exec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ export function createCherryPick(cwd: string) {
2222
returnasyncfunctioncherryPick(commits:string[]):Promise<void>{
2323
for(constcommitofcommits){
2424
try{
25-
const{ stdout}=awaitcreateExec(cwd)(
25+
const{ stdout, stderr}=awaitcreateExec(cwd)(
2626
`git cherry-pick -X theirs${commit}`
2727
);
28+
if(stderr){
29+
console.warn(stderr);
30+
}
2831
if(!stdout){
2932
console.warn(`No cherry-pick output for${commit}`);
3033
}
3134
}catch(e){
3235
console.warn(`Cherry-pick failed for${commit}`);
36+
console.error(e.message);
3337
}
3438
}
3539
};
@@ -50,6 +54,7 @@ export function createCommandRunner(cwd: string) {
5054
}
5155
const{ stdout, stderr}=awaitcreateExec(cwdDir)(command);
5256

57+
console.log(stdout);
5358
console.warn(stderr);
5459
}catch(e){
5560
console.error(`Command failed: "${command}"`);

‎src/validate.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ import {
1010
}from"./utils/exec";
1111
import{getCommits,CommitLogObject}from"./utils/commits";
1212

13+
interfaceOptions{
14+
yaml:string;
15+
clean:boolean;
16+
}
17+
1318
asyncfunctionvalidate(args:string[]){
1419
// dir - default .
1520
constdir=!args.length||args[0].match(/^-/) ?"." :args[0];
1621
constlocalDir=path.join(process.cwd(),dir);
1722

1823
// -y --yaml - default coderoad-config.yml
19-
constoptions={
20-
yaml:getArg(args,{name:"yaml",alias:"y"})||"coderoad.yaml",
24+
constoptions:Options={
25+
//@ts-ignore
26+
yaml:
27+
getArg(args,{name:"yaml",alias:"y",type:"string"})||
28+
"coderoad.yaml",
29+
//@ts-ignore
30+
clean:getArg(args,{name:"clean",alias:"c",type:"bool"}),
2131
};
2232

23-
const_yaml=awaitfs.readFile(path.join(localDir,options.yaml),"utf8");
33+
const_yaml:string=awaitfs.readFile(
34+
path.join(localDir,options.yaml),
35+
"utf8"
36+
);
2437

2538
// parse yaml config
2639
letskeleton;
@@ -158,7 +171,10 @@ async function validate(args: string[]) {
158171
console.error(e.message);
159172
}finally{
160173
// cleanup
161-
awaitfs.emptyDir(tmpDir);
174+
console.log("options.clean",options.clean);
175+
if(options.clean){
176+
awaitfs.emptyDir(tmpDir);
177+
}
162178
}
163179
}
164180

‎tests/args.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import{getArg}from"../src/utils/args";
2+
3+
describe("args",()=>{
4+
it("should capture an arg name from text",()=>{
5+
constargs=["--name","value"];
6+
constresult=getArg(args,{name:"name"});
7+
expect(result).toBe("value");
8+
});
9+
it("should capture an arg alias from text",()=>{
10+
constargs=["-n","value"];
11+
constresult=getArg(args,{name:"name",alias:"n"});
12+
expect(result).toBe("value");
13+
});
14+
it("should capture an arg name from text when starting values",()=>{
15+
constargs=["dir","--name","value"];
16+
constresult=getArg(args,{name:"name"});
17+
expect(result).toBe("value");
18+
});
19+
it("should capture an arg alias from text",()=>{
20+
constargs=["dir","-n","value"];
21+
constresult=getArg(args,{name:"name",alias:"n"});
22+
expect(result).toBe("value");
23+
});
24+
it("should convert bool string to true",()=>{
25+
constargs=["--someBool","true"];
26+
constresult=getArg(args,{
27+
name:"someBool",
28+
alias:"sb",
29+
type:"bool",
30+
});
31+
expect(result).toBe(true);
32+
});
33+
it("should convert bool string to false",()=>{
34+
constargs=["--someBool","false"];
35+
constresult=getArg(args,{
36+
name:"someBool",
37+
alias:"sb",
38+
type:"bool",
39+
});
40+
expect(result).toBe(false);
41+
});
42+
it("should default value to true if no next value",()=>{
43+
constargs=["--someBool"];
44+
constresult=getArg(args,{
45+
name:"someBool",
46+
alias:"sb",
47+
type:"bool",
48+
});
49+
expect(result).toBe(true);
50+
});
51+
it("should default value to true if next value is --name",()=>{
52+
constargs=["--someBool","--someOtherBool"];
53+
constresult=getArg(args,{
54+
name:"someBool",
55+
alias:"sb",
56+
type:"bool",
57+
});
58+
expect(result).toBe(true);
59+
});
60+
it("should default value to true if next value is -alias",()=>{
61+
constargs=["--someBool","-a"];
62+
constresult=getArg(args,{
63+
name:"someBool",
64+
alias:"sb",
65+
type:"bool",
66+
});
67+
expect(result).toBe(true);
68+
});
69+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp