File tree Expand file tree Collapse file tree 8 files changed +144
-6
lines changed Expand file tree Collapse file tree 8 files changed +144
-6
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " github-run-script" ,
3
- "version" : " 1.0.4 " ,
3
+ "version" : " 1.1.0 " ,
4
4
"description" : " Run a script on multiple repositories, cloning them if needed." ,
5
5
"main" : " dist/index.js" ,
6
6
"type" : " commonjs" ,
Original file line number Diff line number Diff line change 1
1
import * as sade from "sade" ;
2
2
import handler from "./handler" ;
3
+ import addTemplates from "./template" ;
3
4
4
5
// eslint-disable-next-line @typescript-eslint/no-var-requires -- Needed in order to not copy over package.json and make new src directory inside of dist
5
6
const { version, description } = require ( "../package.json" ) ;
6
7
7
- const cli = sade ( "github-run-script <script>" , true )
8
+ let cli = sade ( "github-run-script" )
8
9
. version ( version )
9
10
. describe ( description )
11
+ . command ( "execute <script>" , "Execute a script on the given repositories" , {
12
+ default : true ,
13
+ } )
10
14
. option (
11
15
"-o, --owner" ,
12
16
"The owner for repositories without an explicit owner."
@@ -16,4 +20,6 @@ const cli = sade("github-run-script <script>", true)
16
20
. option ( "--signal" , "The signal to terminate a process with." )
17
21
. action ( handler ) ;
18
22
23
+ cli = addTemplates ( cli ) ;
24
+
19
25
export default cli ;
Original file line number Diff line number Diff line change
1
+ import { Template } from "./types" ;
2
+ import { Sade } from "sade" ;
3
+ import templates from "./templates" ;
4
+
5
+ function generateCLIFromTemplate ( sade : Sade , template : Template ) : Sade {
6
+ const argString : string = template . arguments
7
+ ? " " +
8
+ template . arguments
9
+ . map ( ( arg ) => ( typeof arg === "string" ? { name : arg } : arg ) )
10
+ . map ( ( arg ) => `<${ arg . name } >` )
11
+ . join ( " " )
12
+ : "" ;
13
+ sade = sade . command (
14
+ `template ${ template . name } ${ argString } ` ,
15
+ template . description ,
16
+ {
17
+ alias : template . aliases ?. map ( ( alias ) => `template ${ alias } ` ) ,
18
+ }
19
+ ) ;
20
+ template . flags
21
+ ?. map ( ( flag ) => ( typeof flag === "string" ? { name : flag } : flag ) )
22
+ . forEach ( ( flag ) => {
23
+ sade = sade . option ( flag . name , flag . description , flag . value ) ;
24
+ } ) ;
25
+ sade = sade
26
+ . option ( "--output-dir" , "The directory to output the template to." )
27
+ . option (
28
+ "--output-file" ,
29
+ "The filename to output the template to." ,
30
+ template . defaultFileName
31
+ )
32
+ . action ( template . handler ) ;
33
+ return sade ;
34
+ }
35
+
36
+ export default function main ( sade : Sade ) : Sade {
37
+ for ( const template of templates ) {
38
+ sade = generateCLIFromTemplate ( sade , template ) ;
39
+ }
40
+ return sade ;
41
+ }
Original file line number Diff line number Diff line change
1
+ import { GenerateTemplateCliFlags , Template } from "../types" ;
2
+ import { getOutputPath } from "../utils" ;
3
+ import { writeFile } from "fs/promises" ;
4
+
5
+ export default class CpTemplate implements Template {
6
+ readonly name = "copy" ;
7
+ readonly description =
8
+ "A template to copy a signle source file to multiple repositories and commit the changes." ;
9
+ readonly aliases = [ "cp" ] ;
10
+
11
+ readonly defaultFileName = "cp.sh" ;
12
+
13
+ readonly arguments = [
14
+ {
15
+ name : "source_path" ,
16
+ description : "The path that will be copied to the destination." ,
17
+ } ,
18
+ {
19
+ name : "dest_path" ,
20
+ description :
21
+ "Relative path to copy to. May omit the filename in order to use the filename of the source file." ,
22
+ } ,
23
+ {
24
+ name : "commit_message" ,
25
+ description : "The commit message to use when committing the changes." ,
26
+ } ,
27
+ ] ;
28
+
29
+ async handler (
30
+ source_file : string ,
31
+ dest_path : string ,
32
+ commit_message : string ,
33
+ flags : GenerateTemplateCliFlags
34
+ ) {
35
+ const templateString = `
36
+ #!/bin/bash
37
+ $FILE="${ source_file } "
38
+ $RELATIVE_DEST="${ dest_path } "
39
+ git pull
40
+ cp -r $FILE $RELATIVE_DEST
41
+ git add $RELATIVE_DEST
42
+ git commit -m "${ commit_message } "
43
+ git push
44
+ ` ;
45
+ const outputPath = await getOutputPath ( this , flags ) ;
46
+ await writeFile ( outputPath , templateString ) ;
47
+ console . log ( `Output log written to: ${ outputPath } ` ) ;
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ import { Template } from "../types" ;
2
+ import CpTemplate from "./cp" ;
3
+
4
+ const Templates : Template [ ] = [ new CpTemplate ( ) ] ;
5
+
6
+ export default Templates ;
Original file line number Diff line number Diff line change 1
- export interface CliFlags {
1
+ export interface BaseCliFlags {
2
+ _ : string [ ] ;
3
+ }
4
+
5
+ export interface CliFlags extends BaseCliFlags {
2
6
owner ?: string ;
3
7
search ?: string | string [ ] ;
4
8
terminate ?: string | boolean ;
5
9
signal ?: string ;
6
- _ : string [ ] ;
7
10
}
8
11
9
12
/**
10
13
* A two-element tuple that contains [owner, repository].
11
14
*/
12
15
export type RepoOwner = [ string , string ] ;
16
+
17
+ export interface GenerateTemplateCliFlags extends BaseCliFlags {
18
+ outputDir ?: string ;
19
+ outputFile ?: string ;
20
+ }
21
+
22
+ export interface Template {
23
+ name : string ;
24
+ description ?: string ;
25
+ aliases ?: string [ ] ;
26
+
27
+ defaultFileName : string ;
28
+
29
+ arguments ?: ( string | { name : string ; description ?: string } ) [ ] ;
30
+ flags ?: ( string | { name : string ; description ?: string ; value ?: any } ) [ ] ;
31
+ handler : ( ...args : any [ ] ) => any ;
32
+ }
33
+
34
+ export type TemplateJSON = { [ name : string ] : Template } ;
Original file line number Diff line number Diff line change 1
1
import { ChildProcess } from "child_process" ;
2
- import { RepoOwner } from "./types" ;
2
+ import { GenerateTemplateCliFlags , RepoOwner , Template } from "./types" ;
3
+ import { mkdtemp } from "fs/promises" ;
4
+ import { tmpdir } from "os" ;
3
5
4
6
export function getRepoAndOwner (
5
7
input : string ,
@@ -28,3 +30,14 @@ export async function waitOnChildProcessToExit(process: ChildProcess) {
28
30
process . on ( "error" , reject ) ;
29
31
} ) ;
30
32
}
33
+
34
+ export async function getOutputPath (
35
+ template : Template ,
36
+ flags : GenerateTemplateCliFlags
37
+ ) {
38
+ let { outputDir, outputFile } = flags ;
39
+ outputDir =
40
+ outputDir ?? ( await mkdtemp ( `${ tmpdir ( ) } /github-run-script-template-` ) ) ;
41
+ outputFile = outputFile ?? template . defaultFileName ;
42
+ return `${ outputDir } /${ outputFile } ` ;
43
+ }
Original file line number Diff line number Diff line change 4
4
"module" : " commonjs" ,
5
5
"outDir" : " dist" ,
6
6
"strict" : true ,
7
- "target" : " es2019"
7
+ "target" : " es2019" ,
8
+ "resolveJsonModule" : true
8
9
},
9
10
"include" : [" src/**/*" ]
10
11
}
You can’t perform that action at this time.
0 commit comments