UNPKG

@google/dscc-gen

Version:

Create component & connector projects with sane defaults.

122 lines (121 loc) 3.42 kB
"use strict"; /** * @license * Copyright 2018 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const bb = require("bluebird"); const cp = require("child_process"); const fs = require("fs"); const cli_spinner_1 = require("cli-spinner"); exports.readDir = (path) => { return new Promise((resolve, reject) => { fs.readdir(path, (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); }; exports.readFile = (filePath, encoding) => { return new Promise((resolve, reject) => { fs.readFile(filePath, encoding, (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); }; exports.makeDir = (filePath) => { return new Promise((resolve, reject) => { fs.mkdir(filePath, (err) => { if (err) { reject(err); } else { resolve(filePath); } }); }); }; exports.writeFile = (filePath, data, encoding) => { return new Promise((resolve, reject) => { fs.writeFile(filePath, data, encoding, (err) => { if (err) { reject(err); } else { resolve(); } }); }); }; exports.statSync = bb.promisify(fs.stat); exports.fileExists = (filePath) => { return new Promise((resolve, _) => { // The callback version (fs.exists) is deprecated so using synchronous // version (that isn't). const exists = fs.existsSync(filePath); resolve(exists); }); }; exports.exec = (command, options, pipeStd) => { return new Promise((resolve, reject) => { const child = cp.exec(command, options, (err, stdout, stderr) => { if (err) { reject(err); } else { resolve({ out: stdout, err: stderr }); } }); if (pipeStd) { child.stdout.pipe(process.stdout); child.stderr.pipe(process.stderr); } }); }; exports.npmInstall = async (projectPath, answers) => { const execOptions = { cwd: projectPath }; if (answers.yarn) { return exports.exec('yarn install', execOptions, false); } else { return exports.exec('npm install', execOptions, false); } }; exports.pause = async (millis) => { return new Promise((resolve, _) => { setInterval(() => { resolve(); }, millis); }); }; exports.spinnify = async (spinnerText, fn) => { const spinner = new cli_spinner_1.Spinner(spinnerText); spinner.start(); try { return await fn(); } finally { spinner.stop(true); } };