-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6f5fd8
commit dc15c1a
Showing
7 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.clasp.json | ||
.multi-clasp.json | ||
.tags | ||
.DS_Store | ||
node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {foreachClasp, runClasp} from "./common"; | ||
import {parseJsonOrDie} from "./utils"; | ||
|
||
interface CommandOption { | ||
readonly nondev: boolean; | ||
readonly params: string; | ||
} | ||
|
||
/** | ||
* Get Clasp Arguments. | ||
* | ||
* @param functionName {string} The function name within the Apps Script project. | ||
* @param options.nondev {boolean} If we want to run the last deployed version vs the latest code. | ||
* @param options.params {string} JSON string of parameters to be input to function. | ||
* @returns {string} the command arguments | ||
*/ | ||
export function getRunClaspArgs(functionName="", options: CommandOption): string{ | ||
let claspArgs = functionName || ""; | ||
const {params: jsonString = '[]'} = options; | ||
const parameters = parseJsonOrDie<string[]>(jsonString); | ||
|
||
if (!options) { | ||
return claspArgs; | ||
} | ||
|
||
claspArgs += options.nondev?" --nondev":""; | ||
|
||
claspArgs += " --params '"+JSON.stringify(parameters)+"'"; | ||
|
||
return claspArgs; | ||
} | ||
|
||
/** | ||
* Executes an Apps Script function. Requires clasp login --creds. | ||
* @param functionName {string} The function name within the Apps Script project. | ||
* @param options.nondev {boolean} If we want to run the last deployed version vs the latest code. | ||
* @param options.params {string} JSON string of parameters to be input to function. | ||
* @see https://developers.google.com/apps-script/api/how-tos/execute | ||
* @requires `clasp login --creds` to be run beforehand. | ||
*/ | ||
export default async (functionName: string, options: CommandOption): Promise<void> => { | ||
const claspArgs = getRunClaspArgs(functionName, options); | ||
|
||
foreachClasp(async (claspConfig)=>{ | ||
await runClasp(claspConfig, process.argv[2], claspArgs); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Parses input string into a valid JSON object or throws a `ClaspError` error. | ||
* @param value JSON string. | ||
*/ | ||
export const parseJsonOrDie = <T>(value: string): T => { | ||
try { | ||
return JSON.parse(value) as T; | ||
} catch { | ||
throw "Invalid json"; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {getRunClaspArgs} from "../src/run"; | ||
|
||
|
||
describe('run.ts tests', ()=>{ | ||
test('wrong inputs', () => { | ||
expect(()=>(getRunClaspArgs("", {nondev:false, params:''}))).toThrow("Invalid json"); | ||
}); | ||
test('good inputs', () => { | ||
expect(getRunClaspArgs("testFunc", {nondev:false, params:'[]'})).toBe("testFunc --params '[]'"); | ||
expect(getRunClaspArgs("testFunc", {nondev:false, params:'["dino"]'})).toBe("testFunc --params '[\"dino\"]'"); | ||
expect(getRunClaspArgs("testFunc", {nondev:true, params:'["dino", "pepe"]'})).toBe("testFunc --nondev --params '[\"dino\",\"pepe\"]'"); | ||
}); | ||
}) |