UNPKG

@google/clasp

Version:

Develop Apps Script Projects locally

83 lines (82 loc) 4.63 kB
import path from 'node:path'; import { Command } from 'commander'; import inflection from 'inflection'; import { intl } from '../intl.js'; import { withSpinner } from './utils.js'; // https://developers.google.com/drive/api/v3/mime-types const DRIVE_FILE_MIMETYPES = { docs: 'application/vnd.google-apps.document', forms: 'application/vnd.google-apps.form', sheets: 'application/vnd.google-apps.spreadsheet', slides: 'application/vnd.google-apps.presentation', }; export const command = new Command('create-script') .alias('create') .description('Create a script') .option('--type <type>', 'Creates a new Apps Script project attached to a new Document, Spreadsheet, Presentation, Form, or as a standalone script, web app, or API.', 'standalone') .option('--title <title>', 'The project title.') .option('--parentId <id>', 'A project parent Id.') .option('--rootDir <rootDir>', 'Local root directory in which clasp will store your project files.') .action(async function (options) { var _a; const 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); } // Create defaults. const parentId = options.parentId; const name = options.title ? options.title : getDefaultProjectName(process.cwd()); const type = options.type ? options.type.toLowerCase() : 'standalone'; const rootDir = (_a = options.rootDir) !== null && _a !== void 0 ? _a : '.'; clasp.withContentDir(rootDir); if (type && type !== 'standalone') { const mimeType = DRIVE_FILE_MIMETYPES[type]; if (!mimeType) { const msg = intl.formatMessage({ id: "d2MBtN", defaultMessage: [{ type: 0, value: "Invalid container file type" }] }); this.error(msg); } const spinnerMsg = intl.formatMessage({ id: "TMfpGK", defaultMessage: [{ type: 0, value: "Creating script..." }] }); const { parentId, scriptId } = await withSpinner(spinnerMsg, async () => await clasp.project.createWithContainer(name, mimeType)); const parentUrl = `https://drive.google.com/open?id=${parentId}`; const scriptUrl = `https://script.google.com/d/${scriptId}/edit`; const successMessage = intl.formatMessage({ id: "yf9wXJ", defaultMessage: [{ type: 0, value: "Created new document: " }, { type: 1, value: "parentUrl" }, { type: 1, value: "br" }, { type: 0, value: "Created new script: " }, { type: 1, value: "scriptUrl" }] }, { parentUrl, scriptUrl, br: '\n', }); console.log(successMessage); } else { const spinnerMsg = intl.formatMessage({ id: "TMfpGK", defaultMessage: [{ type: 0, value: "Creating script..." }] }); const scriptId = await withSpinner(spinnerMsg, async () => await clasp.project.createScript(name, parentId)); const parentUrl = `https://drive.google.com/open?id=${parentId}`; const scriptUrl = `https://script.google.com/d/${scriptId}/edit`; const successMessage = intl.formatMessage({ id: "0a429S", defaultMessage: [{ type: 0, value: "Created new script: " }, { type: 1, value: "scriptUrl" }, { type: 5, value: "parentId", options: { undefined: { value: [] }, other: { value: [{ type: 1, value: "br" }, { type: 0, value: "Bound to document: " }, { type: 1, value: "parentUrl" }] } } }] }, { parentId, parentUrl, scriptUrl, br: '\n', }); console.log(successMessage); } const spinnerMsg = intl.formatMessage({ id: "UTMHnH", defaultMessage: [{ type: 0, value: "Cloning script..." }] }); const files = await withSpinner(spinnerMsg, async () => { const files = await clasp.files.pull(); 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); }); /** * Gets default project name. * @return {string} default project name. */ export function getDefaultProjectName(dir) { const dirName = path.basename(dir); return inflection.humanize(dirName); }