命令石灰插件创建终端逐步教程

  • 源码大小:286.24KB
  • 所需积分:1积分
  • 源码编号:19JP-3804
  • 浏览次数:3926次
  • 最后更新:2023年07月25日
  • 所属栏目:其他
本站默认解压密码:19jp.com 或 19jp_com

简介

Command lime是一个jQuery插件,用于创建一个终端界面,在这里您可以展示CLI应用程序在终端中的工作方式

如何使用它:

1.在页面上包括所需的jQuery库和终端仿真器。

  1. <!-- jQuery -->
  2. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  3.  
  4. <!-- Terminal Emulator -->
  5. <link rel="stylesheet" href="/path/to/cdn/jquery.terminal.min.css" />
  6. <script src="/path/to/cdn/jquery.terminal.min.js"></script>

2.在页面中下载并加载Command lime的JavaScript和CSS文件。

  1. <link rel="stylesheet" href="dist/command-lime.min.css" />
  2. <script src="dist/command-lime.min.js"></script>

3.为Command lime创建一个容器,并在数据标题.

  1. <div id="term-demo" class="example" data-title="jQueryScript CLI">
  2. </div>

4.使用以下自定义提示、命令、输出和功能创建自己的分步教程:

  1. function outputMkdir(folder) {
  2. if (folder.indexOf('-h') > -1 || folder.indexOf('--help') > -1) {
  3. return false;
  4. }
  5. if (folder.length !== 1) {
  6. this.echo($.cliLit.txt.red('mkdir expect a single variable'));
  7.  
  8. return false;
  9. }
  10.  
  11. availableFolders.push(folder[0]);
  12.  
  13. return $.cliLit.txt.green('folder created');
  14. }
  15.  
  16. function outputCd(params) {
  17. if (params.length !== 1) {
  18. this.echo($.cliLit.txt.red('cd expect a single variable'));
  19.  
  20. return false;
  21. }
  22.  
  23. if (!_.includes(availableFolders, params[0])) {
  24. this.echo($.cliLit.txt.red(`no folder named "${params[0]}" found`));
  25.  
  26. return false;
  27. }
  28.  
  29. folderName = params[0];
  30. }
  31.  
  32. function outputNpmInit() {
  33. return `This utility will walk you through creating a package.json file.
  34. It only covers the most common items, and tries to guess sensible defaults.
  35.  
  36.  
  37. See \`npm help json\` for definitive documentation on these fields
  38. and exactly what they do.
  39.  
  40.  
  41. Use \`npm install <pkg> --save\` afterwards to install a package and
  42. save it as a dependency in the package.json file.
  43.  
  44.  
  45. Press ^C at any time to quit.`;
  46. }
  47.  
  48. function outputNpmInitName(params) {
  49. params = params.length ? params : [folderName];
  50.  
  51. let joined = params.join(' ');
  52.  
  53. if ((/[A-Z]/gm).test(joined)) {
  54. this.echo($.cliLit.txt.red('Sorry, name can no longer contain capital letters.'));
  55.  
  56. return false;
  57. }
  58.  
  59. if (!(/^[a-zA-Z0-9_-]*$/gm).test(joined)) {
  60. this.echo($.cliLit.txt.red('Sorry, name can only contain URL-friendly characters.'));
  61.  
  62. return false;
  63. }
  64. }
  65.  
  66. function outputPrompt() {
  67. return $.cliLit.txt.red(`~${folderName ? '/' + folderName : ''}`) + $.cliLit.txt.green(' > ');
  68. }
  69.  
  70. $('#term-demo').terminalOnboarding([{
  71. name: 'demo CLI - create project',
  72. greetings: 'Hello there! please run `mkdir` with a folder name to continue',
  73. prompt: outputPrompt,
  74. command: 'mkdir *',
  75. output: outputMkdir
  76. }, {
  77. name: 'demo CLI - go into project',
  78. greetings: () => `now that you created your folder, please go into it with "cd ${availableFolders[0]}"`,
  79. prompt: outputPrompt,
  80. command: 'cd *',
  81. output: outputCd
  82. }, {
  83. name: 'demo CLI - initialize project',
  84. greetings: 'please initialize your project with `npm init`',
  85. prompt: outputPrompt,
  86. command: 'npm init',
  87. output: outputNpmInit
  88. },
  89. {
  90. prompt: () => `name: (${folderName}) `,
  91. command: '*',
  92. output: outputNpmInitName
  93. },
  94. {
  95. prompt: 'version: (1.0.0) ',
  96. command: '*',
  97. output: function(params) {
  98. params = params.length ? params : ['1.0.0'];
  99.  
  100. if (!semverRegex().test(params.join(' '))) {
  101. this.echo($.cliLit.txt.red('version must be in a semver schema'));
  102.  
  103. return false;
  104. }
  105. }
  106. },
  107. {
  108. prompt: 'description: ',
  109. command: ''
  110. },
  111. {
  112. prompt: 'git repository: ',
  113. command: ''
  114. },
  115. {
  116. prompt: 'keywords: ',
  117. command: ''
  118. },
  119. {
  120. prompt: 'author: ',
  121. command: ''
  122. },
  123. {
  124. prompt: 'license: (ISC) ',
  125. command: ''
  126. },
  127. {
  128. prompt: `About to write to /Users/nkalman/Development/aaaaaaa/package.json:
  129.  
  130. {
  131. "name": "my-thing",
  132. "version": "1.0.0",
  133. "main": "index.js",
  134. "scripts": {
  135. "test": "echo \"Error: no test specified\" && exit 1"
  136. },
  137. "author": "",
  138. "license": "ISC",
  139. "dependencies": {},
  140. "devDependencies": {},
  141. "description": ""
  142. }
  143.  
  144.  
  145. Is this ok? (yes) `,
  146. command: ''
  147. },
  148. {
  149. name: 'install project dependencies',
  150. clear: false,
  151. greetings: 'let\'s install our dependencies with `npm install`',
  152. prompt: outputPrompt,
  153. command: 'npm install *',
  154. output: function(params) {
  155. if (params.length > 2) {
  156. this.echo('got these params: ' + params.toString());
  157. this.echo($.cliLit.txt.red('please run npm install without any parameters'));
  158.  
  159. return false;
  160. }
  161.  
  162. this.echo($.cliLit.txt.green(`all dependencies installed`));
  163.  
  164. this.echo($.cliLit.txt.blue(`\n~= Thank you for onboarding! =~`));
  165.  
  166. return true;
  167. }
  168. }
  169. ], {
  170. height: 250,
  171. clearOnEveryStep: false
  172. });

预览截图