UNPKG

@nestjsplus/lib-starter

Version:

Boilerplate for building installable NestJS libraries

86 lines (85 loc) 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const dependencies_1 = require("@schematics/angular/utility/dependencies"); const schematics_utils_1 = require("@nestjsplus/schematics-utils"); const string_utils_1 = require("./string-utils"); const constants_1 = require("../constants"); function inProjectRoot(host) { const currDir = host.getDir('.'); return constants_1.PROJECT_ROOT_FILES.every(file => currDir.subfiles.includes(file)); } exports.inProjectRoot = inProjectRoot; function readWorkspace(host) { const content = host.read('./nest-cli.json').toString(); if (!content) { return; } return JSON.parse(content); } exports.readWorkspace = readWorkspace; function readPackageJson(host) { const content = host.read('./package.json').toString(); if (!content) { return; } return JSON.parse(content); } exports.readPackageJson = readPackageJson; function buildPathRelativeToWorkspaceRoot(sourceRoot) { const pathDots = sourceRoot.replace(/^\//, '').split('/').length - 1; return '../'.repeat(pathDots); } exports.buildPathRelativeToWorkspaceRoot = buildPathRelativeToWorkspaceRoot; function getWorkspaceRoot(host) { const workspace = readWorkspace(host); } exports.getWorkspaceRoot = getWorkspaceRoot; function generateFilesFromTemplates(options, templateUrl, location) { return (context) => schematics_1.apply(schematics_1.url(templateUrl), [ schematics_1.template(Object.assign({}, core_1.strings, options, { lowerCase: string_utils_1.lowerCase, upperCase: string_utils_1.upperCase, dashToUnderscore: string_utils_1.dashToUnderscore })), schematics_1.move(location), ])(context); } exports.generateFilesFromTemplates = generateFilesFromTemplates; function addRootModuleImportAndMetadata(options) { const moduleOptions = Object.assign({}, options); moduleOptions.metadata = 'imports'; moduleOptions.type = 'module'; return (tree) => { if (moduleOptions.skipImport !== undefined && moduleOptions.skipImport) { return tree; } moduleOptions.module = new schematics_utils_1.ModuleFinder(tree).find(moduleOptions.sourceRoot); if (!moduleOptions.module) { return tree; } const content = tree.read(moduleOptions.module).toString(); const declarator = new schematics_utils_1.ModuleDeclarator(); tree.overwrite(moduleOptions.module, declarator.declare(content, moduleOptions)); return tree; }; } exports.addRootModuleImportAndMetadata = addRootModuleImportAndMetadata; function addNpmScripts(script) { return (host) => { const pkgPath = '/package.json'; const buffer = host.read(pkgPath); if (buffer === null) { throw new schematics_1.SchematicsException('Could not find package.json'); } const pkg = JSON.parse(buffer.toString()); pkg.scripts[script.name] = script.action; host.overwrite(pkgPath, JSON.stringify(pkg, null, 2)); return host; }; } exports.addNpmScripts = addNpmScripts; function addPackageDependencies(dep) { return (host) => { dependencies_1.addPackageJsonDependency(host, dep); }; }