|
| 1 | +/* eslint-env node*/ |
| 2 | +'use strict'; |
| 3 | +const path = require('path'); |
| 4 | +const Generator = require('yeoman-generator'); |
| 5 | +const chalk = require('chalk'); |
| 6 | +const yosay = require('yosay'); |
| 7 | +const inquirer = require('inquirer'); |
| 8 | +const packageJson = require('package-json'); |
| 9 | +const globby = require('globby'); |
| 10 | + |
| 11 | +module.exports = class extends Generator { |
| 12 | + prompting() { |
| 13 | + // Have Yeoman greet the user. |
| 14 | + this.log( |
| 15 | + yosay( |
| 16 | + chalk`Welcome to the superb {red DNN JavaScript Library} generator!` |
| 17 | + ) |
| 18 | + ); |
| 19 | + |
| 20 | + const prompts = [ |
| 21 | + { |
| 22 | + type: 'input', |
| 23 | + name: 'libraryName', |
| 24 | + message: "What is the npm module's name?", |
| 25 | + }, |
| 26 | + { |
| 27 | + type: 'input', |
| 28 | + name: 'friendlyName', |
| 29 | + message: "What is the library's friendly name?", |
| 30 | + default: answers => |
| 31 | + packageJson(answers.libraryName, { |
| 32 | + fullMetadata: true, |
| 33 | + }).then(pkg => { |
| 34 | + answers.pkg = pkg; |
| 35 | + if (pkg.repository && pkg.repository.url) { |
| 36 | + answers.githubUrl = pkg.repository.url |
| 37 | + .replace(/^git(?:\+https?)?:/, 'https:') |
| 38 | + .replace(/\.git$/, ''); |
| 39 | + } |
| 40 | + |
| 41 | + return pkg.name; |
| 42 | + }), |
| 43 | + }, |
| 44 | + { |
| 45 | + type: 'input', |
| 46 | + name: 'licenseUrl', |
| 47 | + message: "What is the URL to the library's license?", |
| 48 | + default: answers => |
| 49 | + answers.githubUrl |
| 50 | + ? `${answers.githubUrl}/blob/LICENSE` |
| 51 | + : answers.homepage, |
| 52 | + }, |
| 53 | + { |
| 54 | + type: 'input', |
| 55 | + name: 'licenseName', |
| 56 | + message: 'What is the name of the license?', |
| 57 | + default: answers => answers.pkg.license, |
| 58 | + }, |
| 59 | + { |
| 60 | + type: 'input', |
| 61 | + name: 'changelogUrl', |
| 62 | + message: "What is the URL to the library's changelog?", |
| 63 | + default: answers => |
| 64 | + answers.githubUrl |
| 65 | + ? `${answers.githubUrl}/releases` |
| 66 | + : answers.homepage, |
| 67 | + }, |
| 68 | + { |
| 69 | + type: 'input', |
| 70 | + name: 'description', |
| 71 | + message: "What is the library's description?", |
| 72 | + default: answers => answers.pkg.description, |
| 73 | + }, |
| 74 | + { |
| 75 | + type: 'list', |
| 76 | + name: 'relativePath', |
| 77 | + message: 'What is the main JavaScript file?', |
| 78 | + choices: answers => { |
| 79 | + console.log( |
| 80 | + 'choices', |
| 81 | + `node_modules/${answers.libraryName}/**/*.js` |
| 82 | + ); |
| 83 | + try { |
| 84 | + return globby |
| 85 | + .sync(`node_modules/${answers.libraryName}/**/*.js`) |
| 86 | + .map(file => |
| 87 | + file.replace( |
| 88 | + `node_modules/${answers.libraryName}/`, |
| 89 | + '' |
| 90 | + ) |
| 91 | + ) |
| 92 | + .map(path.normalize) |
| 93 | + .concat([new inquirer.Separator(), 'Other']); |
| 94 | + } catch (e) { |
| 95 | + console.error(e); |
| 96 | + } |
| 97 | + }, |
| 98 | + default: answers => |
| 99 | + path.normalize(answers.pkg.browser || answers.pkg.main), |
| 100 | + }, |
| 101 | + { |
| 102 | + type: 'input', |
| 103 | + name: 'relativePath', |
| 104 | + message: 'What is the main JavaScript file?', |
| 105 | + when: answers => answers.relativePath === 'Other', |
| 106 | + }, |
| 107 | + { |
| 108 | + type: 'list', |
| 109 | + name: 'preferredScriptLocation', |
| 110 | + message: |
| 111 | + 'What is the preferred script location for the library?', |
| 112 | + choices: ['PageHead', 'BodyTop', 'BodyBottom'], |
| 113 | + default: 'BodyBottom', |
| 114 | + }, |
| 115 | + { |
| 116 | + type: 'input', |
| 117 | + name: 'objectName', |
| 118 | + message: |
| 119 | + 'What JavaScript object will be defined if the script loaded correctly?', |
| 120 | + }, |
| 121 | + ]; |
| 122 | + |
| 123 | + return this.prompt(prompts).then(props => { |
| 124 | + // To access props later use this.props.someAnswer; |
| 125 | + this.props = props; |
| 126 | + this.props.fileName = path.basename(props.relativePath); |
| 127 | + this.props.version = props.pkg.version; |
| 128 | + this.props.versionFolder = props.pkg.version |
| 129 | + .split('.') |
| 130 | + .map(n => n.padStart(2, '0')) |
| 131 | + .join('_'); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + writing() { |
| 136 | + const folder = `${this.props.libraryName}_${this.props.version}`; |
| 137 | + this.fs.copyTpl( |
| 138 | + this.templatePath('{libraryName}.dnn'), |
| 139 | + this.destinationPath(`${folder}/${this.props.libraryName}.dnn`), |
| 140 | + this.props |
| 141 | + ); |
| 142 | + this.fs.copyTpl( |
| 143 | + this.templatePath('CHANGES.htm'), |
| 144 | + this.destinationPath(`${folder}/CHANGES.htm`), |
| 145 | + this.props |
| 146 | + ); |
| 147 | + this.fs.copyTpl( |
| 148 | + this.templatePath('dnn-library.json'), |
| 149 | + this.destinationPath(`${folder}/dnn-library.json`), |
| 150 | + this.props |
| 151 | + ); |
| 152 | + this.fs.copyTpl( |
| 153 | + this.templatePath('LICENSE.htm'), |
| 154 | + this.destinationPath(`${folder}/LICENSE.htm`), |
| 155 | + this.props |
| 156 | + ); |
| 157 | + } |
| 158 | +}; |
0 commit comments