【问题标题】:How to write script for auto generating files如何编写自动生成文件的脚本
【发布时间】:2019-12-03 16:06:01
【问题描述】:

我正在使用nestjs 使用graphql。当我创建新模块时,我将手动创建一个文件,例如(sample.graphql、sample.interface.ts、sample.module.ts、sample.schema.ts、sample.resolver.ts、sample.service.ts)。 那么我如何自动生成这些文件。有什么脚本可以解决我的问题吗?

【问题讨论】:

  • 您是否希望在每次创建模块时都这样做?您使用的是 Windows、MacOS、Linux 吗?您使用的是哪种外壳?
  • 我必须使用Linux机器
  • 常规shell、bash、zsh,你用什么命令提示符?
  • 我只是用vs code命令提示符
  • 好的,VSCode 使用您的默认命令提示符,除非您自己更改了它。我将继续假设 bash,但您可能正在使用 sh,因此事情可能无法按预期工作,可能需要进行一些调整。

标签: graphql nestjs


【解决方案1】:

一种可能性是创建一个快捷命令并将其放在您的~/bin/ 目录中。该命令将按照您期望的顺序调用所有预期的嵌套生成命令,可能是这样的:

#! /bin/bash

if [ -z $1 ]
  print "A name is necessary for using this command.";
  exit 0;
fi

# Nest has the notion of modules, resolvers, services, and interfaces
nest g module $1
nest g resolver $1
nest g service $1
nest g interface $1
# To create the graphql and schema files you'll need to run touch instead
touch src/$1/$1.schema.ts
touch src/$1/$1.graphql

保存脚本后,请确保通过运行sudo chmod 744 ~/bin/myscript 使其可执行。顺便说一句,您需要一个超级用户密码。

chmod 允许您授予对脚本文件的访问权限并声明应如何处理访问权限。 744 表示您 (7) 拥有完全权限(读、写、执行)。前 4 表示与您具有读取权限的同一组的成员,后 4 表示使用计算机的任何人都具有读取权限。 Checkout chmod for a bit more info.

授予执行权限后,您应该能够运行myscript 并查看输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2014-10-10
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多