UNPKG

@ehduardu/bat-framework

Version:

A simple framework for Google Apps Script

98 lines (97 loc) 5.77 kB
"use strict"; 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()); }); }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _CopyingProjectService_canWrite; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = __importDefault(require("chalk")); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const cpy_1 = __importDefault(require("cpy")); const os_1 = __importDefault(require("os")); const git_1 = require("../tools/git"); const install_1 = require("../tools/install"); const package_json_1 = __importDefault(require("../../templates/api/package.json")); const package_json_2 = __importDefault(require("../../templates/project/package.json")); class CopyingProjectService { constructor() { _CopyingProjectService_canWrite.set(this, (path) => __awaiter(this, void 0, void 0, function* () { try { yield fs_1.default.promises.access(path, (fs_1.default.constants || fs_1.default).W_OK); return true; } catch (_a) { return false; } })); } execute({ appPath, projectName, templateType }) { return __awaiter(this, void 0, void 0, function* () { const canWrite = yield __classPrivateFieldGet(this, _CopyingProjectService_canWrite, "f").call(this, path_1.default.dirname(appPath)); if (!canWrite) { console.error('The application path is not writable, please check folder permissions and try again.'); console.error('It is likely you do not have write permissions for this folder.'); process.exit(1); } yield fs_1.default.promises.mkdir(appPath, { recursive: true }); console.log(`Creating a new apps script project in ${chalk_1.default.green(appPath)}`); process.chdir(appPath); console.log(`Copying ${chalk_1.default.cyan('template')} and ${chalk_1.default.cyan('base files')}`); const rootDirName = path_1.default.resolve(__dirname, '..', '..'); yield (0, cpy_1.default)(['.*', '.**', '**', '*.**'], appPath, { parents: true, dot: true, cwd: path_1.default.join(rootDirName, 'templates', templateType), }); let packageJSON; if (templateType === 'api') { packageJSON = package_json_1.default; } else if (templateType === 'project') { packageJSON = package_json_2.default; } fs_1.default.writeFileSync(path_1.default.join(appPath, 'package.json'), JSON.stringify(packageJSON, null, 2) + os_1.default.EOL); console.log('Installing development dependencies...'); yield (0, install_1.install)(appPath, [ "@google/clasp@2.3.0", ], true); console.log('Initializing git...'); const gitInitialized = (0, git_1.gitInit)(appPath); if (gitInitialized) { console.log('Git repository initialized\n'); } else { console.log(`${chalk_1.default.yellow('Attention!')} ${chalk_1.default.redBright('git repository it not initialized!')}`); console.log('Inside that directory, you need to run this commands:\n'); console.log(chalk_1.default.cyan(' git init')); } console.log(`${chalk_1.default.green('Success!')} Created ${projectName} at ${chalk_1.default.cyan(appPath)}\n`); console.log(`${chalk_1.default.yellow('Attention!')} Before start, make sure to change the scriptId in the ${chalk_1.default.cyan('.clasp.json')}`); console.log('Inside that directory, you can run several commands:\n'); console.log(chalk_1.default.cyan(' yarn login')); console.log(' To do login in google clasp.\n'); console.log(chalk_1.default.cyan(' yarn build')); console.log(' Builds the app to dist folder.\n'); console.log(chalk_1.default.cyan(' yarn deploy')); console.log(' Builds the app and push to gas server.\n'); console.log('We suggest that you begin by typing:\n'); console.log(` ${chalk_1.default.cyan('yarn login\n')}`); }); } } exports.default = CopyingProjectService; _CopyingProjectService_canWrite = new WeakMap();