UNPKG

multi-clasp2

Version:

Google Clasp wrapper to push changes to multiple Apps Script projects at once.

157 lines 5.88 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runClasp = runClasp; exports.readMultiClaspConfig = readMultiClaspConfig; exports.readClaspConfig = readClaspConfig; exports.writeClaspConfig = writeClaspConfig; exports.writeMultiClaspConfig = writeMultiClaspConfig; exports.foreachClasp = foreachClasp; exports.genericAction = genericAction; const fs = __importStar(require("fs")); const config_1 = require("./config"); const utils_1 = require("./utils"); /** * Push a Clasp configuration * * @param claspConfig the single clasp config * @param command the clasp command to execute * @param options the command options * @return true if ok, false otherwise */ function runClasp(claspConfig_1, command_1) { return __awaiter(this, arguments, void 0, function* (claspConfig, command, options = "") { if (!claspConfig || !claspConfig.scriptId || !command) { return false; } yield writeClaspConfig(claspConfig); console.log('Elaborating scriptId:', claspConfig.scriptId); try { // const { stdout } = await exec(`npx clasp ${command} ${options}`); const { error, stdout, stderr } = yield (0, utils_1.execShellCommand)(`npx clasp ${command} ${options}`); console.log(stdout); if (error) { console.log(stderr); return false; } } catch (e) { console.error(e.stderr); return false; } return true; }); } /** * Read a Multi Clasp Config. * * @returns array of SingleClasp */ function readMultiClaspConfig() { return JSON.parse(fs.readFileSync(config_1.Config.MULTICLASP_FILENAME, config_1.Config.UTF_8).toString() || "[]"); } /** * Read a Clasp Config. * @param basePath the base path to read * @returns the object with the configuration */ function readClaspConfig(basePath = '') { return JSON.parse(fs.readFileSync(basePath + config_1.Config.CLASP_FILENAME, config_1.Config.UTF_8).toString()); } /** * Write a Clasp Config. * @param config the clasp configuration * @param basePath the base path to write * @returns the object with the configuration */ function writeClaspConfig(config_2) { return __awaiter(this, arguments, void 0, function* (config, basePath = '') { return fs.writeFile(basePath + config_1.Config.CLASP_FILENAME, JSON.stringify(config, null, 2), config_1.Config.UTF_8, (err) => __awaiter(this, void 0, void 0, function* () { if (err) throw err; })); }); } /** * Write a Multi clasp Config. * @param config the multi-clasp configuration * @param basePath the base path to write * @returns the object with the configuration */ function writeMultiClaspConfig(config, basePath = '') { return fs.writeFileSync(basePath + config_1.Config.MULTICLASP_FILENAME, JSON.stringify(config, null, 2), config_1.Config.UTF_8); } /** * commander action to run clasp. * * @returns */ function foreachClasp(fn) { return __awaiter(this, void 0, void 0, function* () { const clasps = readMultiClaspConfig(); for (let i = 0, len = clasps.length; i < len; i++) { yield fn(clasps[i]); } fs.unlink(config_1.Config.CLASP_FILENAME, (err) => { if (err) throw err; }); }); } /** * commander action to run clasp. * * @returns */ function genericAction() { return __awaiter(this, void 0, void 0, function* () { foreachClasp((claspConfig) => __awaiter(this, void 0, void 0, function* () { const retVal = yield runClasp(claspConfig, process.argv[2], (0, utils_1.getOptions)()); if (!retVal) { process.exit(1); } })); }); } //# sourceMappingURL=common.js.map