UNPKG

@google/dscc-gen

Version:

Create component & connector projects with sane defaults.

179 lines 6.67 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 }); exports.rename = exports.mv = exports.cp = exports.mkdir = exports.remove = exports.createAndCopyFiles = exports.parseJsonFile = exports.createDirectoryContents = exports.fixTemplates = exports.recursiveReaddirSync = exports.listFiles = void 0; const fs = require("fs"); const path = require("path"); const rimraf = require("rimraf"); const shelljs = require("shelljs"); const util = require("./util"); const ENCODING = 'utf8'; const CURR_DIR = process.cwd(); const fixFile = (templates) => async (file) => { const contents = util.readFile(file, ENCODING); const newContents = templates.reduce((acc, { match, replace }) => acc.replace(match, replace), contents); return util.writeFile(file, newContents, ENCODING); }; exports.listFiles = (baseDirectory, toIgnore) => { const dirContents = fs.readdirSync(baseDirectory, { withFileTypes: true }); const files = dirContents.map((item) => { const itemPath = path.resolve(baseDirectory, item.name); return item.isDirectory() ? exports.listFiles(itemPath, toIgnore) : [itemPath]; }); return files.reduce((acc, fileNames) => { if (toIgnore != null) { return acc.concat(fileNames).filter((el) => !el.includes(toIgnore)); } else { return acc.concat(fileNames); } }, []); }; exports.recursiveReaddirSync = async (baseDirectory) => { const dirContents = fs.readdirSync(baseDirectory, { withFileTypes: true }); const files = await Promise.all(dirContents.map(async (item) => { const itemPath = path.resolve(baseDirectory, item.name); return item.isDirectory() ? await exports.recursiveReaddirSync(itemPath) : [itemPath]; })); return files.reduce((acc, fileNames) => { return acc.concat(fileNames); }, []); }; exports.fixTemplates = async (baseDirectory, templates) => { const filesToUpdate = exports.listFiles(baseDirectory, 'node_modules'); await Promise.all(filesToUpdate.map(fixFile(templates))); return true; }; exports.createDirectoryContents = async (templatePath, newProjectPath) => { const filesToCreate = util.readDir(templatePath); return Promise.all(filesToCreate.map(async (file) => { const originalFilePath = path.join(templatePath, file); const stats = fs.statSync(originalFilePath); if (stats.isFile()) { const contents = util.readFile(originalFilePath, ENCODING); // npm renames .gitignore to .npmignore so rename it back to .gitignore. if (file === '.npmignore') { file = '.gitignore'; } const writePath = path.join(CURR_DIR, newProjectPath, file); util.writeFile(writePath, contents, ENCODING); } else if (stats.isDirectory()) { exports.mkdir(CURR_DIR, newProjectPath, file); const newTemplatePath = path.join(templatePath, file); const newNewProjectPath = path.join(newProjectPath, file); exports.createDirectoryContents(newTemplatePath, newNewProjectPath); } else { throw new Error(`${originalFilePath} is not a file or directory.`); } })); }; exports.parseJsonFile = (filePath) => { try { const fileContents = util.readFile(filePath, ENCODING); return JSON.parse(fileContents); } catch (e) { throw new Error(`Could not read: ${filePath}`); } }; const createAndCopyFilesImpl = async (projectPath, templatePath, projectName) => { try { exports.mkdir(projectName); } catch (e) { throw new Error(`Couldn't create directory: ${projectPath}`); } try { await exports.createDirectoryContents(templatePath, projectName); } catch (e) { throw new Error(`Couldn't copy over the template files to: ${projectPath}`); } }; exports.createAndCopyFiles = async (projectPath, templatePath, projectName) => await util.spinnify('Creating directories and copying template files...', () => createAndCopyFilesImpl(projectPath, templatePath, projectName)); exports.remove = (...directoryParts) => { if (directoryParts.length === 0) { throw new Error('You must pass directoryParts to this function'); } const directory = path.join(...directoryParts); try { const stats = fs.statSync(directory); if (stats.isDirectory()) { rimraf.sync(directory); } else { fs.unlinkSync(directory); } } catch (err) { throw new Error(`Unable to remove ${directory}`); } return true; }; exports.mkdir = (...directoryParts) => { if (directoryParts.length === 0) { throw new Error('You must pass directoryParts to this function'); } const directory = path.join(...directoryParts); try { fs.mkdirSync(directory, { recursive: true }); } catch (err) { throw Error(err); } return true; }; exports.cp = (fromParts, toParts) => { const fromPath = path.join(...fromParts); const toPath = path.join(...toParts); const result = shelljs.cp('-r', fromPath, toPath); if (result.stderr !== null) { throw new Error(result.stderr); } return true; }; exports.mv = (fromParts, toDirParts) => { const fromPath = path.join(...fromParts); const toPath = path.join(...toDirParts); const result = shelljs.mv('-f', fromPath, toPath); if (!fs.existsSync(toPath)) { if (result.stderr !== null) { throw new Error(result.stderr); } else { throw new Error(`Moving ${fromPath} to ${toPath} failed`); } } return true; }; exports.rename = (fromParts, toParts) => { const fromPath = path.join(...fromParts); const toPath = path.join(...toParts); try { fs.renameSync(fromPath, toPath); } catch (err) { throw new Error(err); } return true; }; //# sourceMappingURL=files.js.map