template.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * grunt-init-amber
  3. * https://amber-lang.net/
  4. *
  5. * Copyright (c) 2013 Manfred Kroehnert, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. // Basic template description.
  10. exports.description = 'Create a web application based on Amber Smalltalk.';
  11. // Template-specific notes to be displayed before question prompts.
  12. exports.notes = ' _Project title_ should be a human-readable title.';
  13. // Template-specific notes to be displayed after question prompts.
  14. exports.after = 'You need to have these installed globally via npm:' +
  15. ' _amber-cli_; _grunt-cli_; _bower_.' +
  16. ' Now, install project dependencies with _bower install_,' +
  17. ' tool dependencies with _npm install_ and optionally, recompile with _grunt_.' +
  18. ' If you are running _amber init_, these three tasks are going to be performed for you now.' +
  19. ' Afterwards, start the development server with _amber serve_.' +
  20. ' Your application is then accessible via _http://localhost:4000/_';
  21. // Any existing file or directory matching this wildcard will cause a warning.
  22. exports.warnOn = '*';
  23. // The actual init template.
  24. exports.template = function(grunt, init, done) {
  25. var remembered = {};
  26. function rememberViaValidator(name) {
  27. var oldValidator = init.prompts[name].validator || function (line) {
  28. return true;
  29. };
  30. var newValidator;
  31. switch (oldValidator.length) { //apply would not work, .length is used to call it differently
  32. case 1:
  33. newValidator = function (line) {
  34. remembered[name] = line;
  35. return oldValidator.call(this, line);
  36. };
  37. break;
  38. case 2:
  39. newValidator = function (line, next) {
  40. remembered[name] = line;
  41. return oldValidator.call(this, line, next);
  42. };
  43. break;
  44. default:
  45. throw new Error("Panic: " + oldValidator.length + "-argument validator for " + name + ".");
  46. }
  47. init.prompts[name].validator = newValidator;
  48. }
  49. function capitalize(string) {
  50. return string[0].toUpperCase() + string.slice(1).toLowerCase();
  51. }
  52. init.prompts.name.message= 'Main class and package of Amber application.\nProject name is derived by lowercasing this.';
  53. init.prompts.name.validator= function (line) { return /^[A-Z][A-Za-z0-9]*$/.test(line) };
  54. init.prompts.name.warning= 'Must be a valid class name: only alphanumeric and starting with an uppercase letter!';
  55. rememberViaValidator('name');
  56. rememberViaValidator('title');
  57. init.process({type: 'amber'}, [
  58. // Prompt for these values.
  59. init.prompt('title', 'Application or Library Title'),
  60. init.prompt('name', function (value, data, done) {
  61. var words = remembered.title.split(/\W/);
  62. words = words.filter(function (x) {
  63. return x && x !== "none";
  64. }).map(capitalize);
  65. value = words.length ? words.join('') : 'MysteriousApp';
  66. done(null, value);
  67. }),
  68. init.prompt('description', 'The Application or The Library doing The Thing.'),
  69. init.prompt('author_name'),
  70. init.prompt('author_email'),
  71. {
  72. name: 'namespace',
  73. message: 'Namespace of the new Amber package.',
  74. altDefault: function(value, data, done) {
  75. value = 'amber-' + remembered.name.toLowerCase();
  76. done(null, value);
  77. },
  78. validator: /^[a-z][a-z0-9/\-]*$/,
  79. warning: 'Only lowercase letters, numbers, and - are allowed in namespaces!'
  80. },
  81. init.prompt('version'),
  82. init.prompt('repository'),
  83. init.prompt('homepage'),
  84. init.prompt('bugs'),
  85. init.prompt('author_url'),
  86. init.prompt('licenses', 'MIT')
  87. ], function(err, props) {
  88. // Files to copy (and process).
  89. var files = init.filesToCopy(props);
  90. // Add properly-named license files.
  91. init.addLicenseFiles(files, props.licenses);
  92. // Actually copy (and process) files.
  93. init.copyAndProcess(files, props, {noProcess: 'libs/**'});
  94. // Clean up non-npm props.
  95. delete props.namespace;
  96. props.name = props.name.toLowerCase();
  97. // A few additional properties.
  98. props.keywords = ['Amber', 'Smalltalk'];
  99. props.devDependencies = {
  100. "grunt": "~0.4.0",
  101. "grunt-execute": "~0.2.1",
  102. "grunt-contrib-clean": "~0.5.0",
  103. "amber-dev": "~0.1.1"
  104. };
  105. props.node_version = '>= 0.8.0';
  106. props.scripts = {
  107. "test": "grunt test"
  108. };
  109. // Generate package.json file, used by npm and grunt.
  110. init.writePackageJSON('package.json', props);
  111. // generate bower.json file
  112. grunt.file.write('bower.json', JSON.stringify({
  113. "name": props.name,
  114. "description": props.description,
  115. "version": props.version,
  116. "ignore": [
  117. "**/.*",
  118. "node_modules",
  119. "bower_components",
  120. "/test_runner.js",
  121. "test",
  122. "tests"
  123. ],
  124. "authors": [
  125. {
  126. "name": props.author_name,
  127. "email": props.author_email
  128. }
  129. ],
  130. "homepage": props.homepage,
  131. "main": props.main,
  132. "keywords": props.keywords,
  133. "license": props.licenses,
  134. "private": false,
  135. "dependencies": {
  136. "amber": "~0.12.4"
  137. }
  138. }, null, 4));
  139. // All done!
  140. done();
  141. });
  142. };