1
0

template.js 4.8 KB

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