UNPKG

@google/clasp

Version:

Develop Apps Script Projects locally

72 lines (71 loc) 3.21 kB
import { Command } from 'commander'; import inquirer from 'inquirer'; import { intl } from '../intl.js'; import { isInteractive, withSpinner } from './utils.js'; export const command = new Command('clone-script') .alias('clone') .description('Clone an existing script') .arguments('[scriptId] [versionNumber]') .option('--rootDir <rootDir>', 'Local root directory in which clasp will store your project files.') .action(async function (scriptId, versionNumber) { var _a; let clasp = this.opts().clasp; if (clasp.project.exists()) { const msg = intl.formatMessage({ id: "kk5+4G", defaultMessage: [{ type: 0, value: "Project file already exists." }] }); this.error(msg); } const rootDir = this.opts().rootDir; clasp.withContentDir(rootDir !== null && rootDir !== void 0 ? rootDir : '.'); if (scriptId) { const match = scriptId.match(/https:\/\/script\.google\.com\/d\/([^/]+)\/.*/); if (match) { scriptId = match[1]; } else { scriptId = scriptId.trim(); } } else if (isInteractive()) { const projects = await clasp.project.listScripts(); const choices = projects.results.map(file => ({ name: `${file.name.padEnd(20)} - https://script.google.com/d/${file.id}/edit`, value: file.id, })); const prompt = intl.formatMessage({ id: "3aQTl3", defaultMessage: [{ type: 0, value: "Clone which script?" }] }); const answer = await inquirer.prompt([ { choices: choices, message: prompt, name: 'scriptId', pageSize: 30, type: 'list', }, ]); scriptId = answer.scriptId; } if (!scriptId) { const msg = intl.formatMessage({ id: "DJWCmP", defaultMessage: [{ type: 0, value: "No script ID." }] }); this.error(msg); } try { const cloningScriptMsg = intl.formatMessage({ id: "UTMHnH", defaultMessage: [{ type: 0, value: "Cloning script..." }] }); const files = await withSpinner(cloningScriptMsg, async () => { clasp = clasp.withScriptId(scriptId); const files = await clasp.files.pull(versionNumber); clasp.project.updateSettings(); return files; }); files.forEach(f => console.log(`└─ ${f.localPath}`)); const successMessage = intl.formatMessage({ id: "XABSyD", defaultMessage: [{ type: 0, value: "Cloned " }, { type: 6, value: "count", options: { "=0": { value: [{ type: 0, value: "no files." }] }, one: { value: [{ type: 0, value: "one file." }] }, other: { value: [{ type: 7 }, { type: 0, value: " files" }] } }, offset: 0, pluralType: "cardinal" }, { type: 0, value: "." }] }, { count: files.length, }); console.log(successMessage); } catch (error) { if (((_a = error.cause) === null || _a === void 0 ? void 0 : _a.code) === 'INVALID_ARGUMENT') { const msg = intl.formatMessage({ id: "jRe7cT", defaultMessage: [{ type: 0, value: "Invalid script ID." }] }); this.error(msg); } throw error; } });