-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
156 lines (134 loc) · 3.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
const Generator = require('yeoman-generator');
const path = require('path');
const _ = require('lodash');
function stripBabelPlugin(str) {
return str.replace(/^babel-plugin-/, '');
}
module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);
this.pkg = this.fs.readJSON(this.destinationPath('package.json'), {});
this.props = {};
}
prompting() {
const prompts = [{
name: 'name',
message: 'Plugin Name',
default: stripBabelPlugin(path.basename(process.cwd())),
filter: function(name) {
return _.kebabCase(stripBabelPlugin(name));
},
validate: function (input) {
return !!input.length;
},
when: !this.pkg.name
}, {
name: 'description',
message: 'Description',
when: !this.pkg.description
}, {
name: 'githubUsername',
message: 'GitHub username or organization',
when: !this.pkg.repository
}, {
name: 'authorName',
message: 'Author\'s Name',
when: !this.pkg.author,
store: true
}, {
name: 'authorEmail',
message: 'Author\'s Email',
when: !this.pkg.author,
store: true
}, {
name: 'keywords',
message: 'Key your keywords (comma to split)',
when: !this.pkg.keywords
}];
return this.prompt(prompts).then((props) => {
this.props = _.extend(this.props, props);
this.props.githubRepoName = 'babel-plugin-' + this.props.name;
if (props.githubUsername) {
this.props.repository = props.githubUsername + '/' + this.props.githubRepoName;
}
this.props.keywords = _.uniq(_.words(props.keywords).concat(['babel-plugin']));
});
}
writing() {
const pkgJsonFields = {
name: this.props.githubRepoName,
version: '0.0.0',
description: this.props.description,
repository: this.props.repository,
license: this.props.license,
author: this.getAuthor(),
main: 'lib/index.js',
dependencies: {
'@babel/runtime': '^7.10.5'
},
devDependencies: {
'@babel/cli': '^7.10.5',
'@babel/core': '^7.10.5',
'@babel/plugin-transform-runtime': '^7.10.5',
'@babel/preset-env': '^7.10.4',
'jest': '^26.1.0'
},
scripts: {
'clean': 'rm -rf lib',
'build': 'babel src -d lib',
'test': 'jest __tests__/index.js',
'test:watch': 'npm run test -- --watch',
'prepublish': 'npm run clean && npm run build'
},
keywords: this.props.keywords
};
this.fs.writeJSON('package.json', _.merge(pkgJsonFields, this.pkg));
this.fs.copy(
this.templatePath('gitignore'),
this.destinationPath('.gitignore')
);
this.fs.copy(
this.templatePath('npmignore'),
this.destinationPath('.npmignore')
);
this.fs.copy(
this.templatePath('babelrc'),
this.destinationPath('.babelrc')
);
this.fs.copy(
this.templatePath('travis.yml'),
this.destinationPath('.travis.yml')
);
this.fs.copyTpl(
this.templatePath('README.md'),
this.destinationPath('README.md'),
this.props
);
this.fs.copyTpl(
this.templatePath('src/index.js'),
this.destinationPath('src/index.js'),
this.props
);
// The file
let testIndex = this.fs.read(this.templatePath('__tests__/index.js'));
testIndex = testIndex.replace('<%= description %>', this.props.description);
this.fs.write(this.destinationPath('__tests__/index.js'), testIndex);
}
default() {
this.composeWith(require.resolve('../fixture/'), { arguments: 'example' });
}
install() {
this.npmInstall();
}
getAuthor() {
if (this.props.authorName && this.props.authorEmail) {
return `${this.props.authorName} <${this.props.authorEmail}>`;
}
if (this.props.authorName) {
return this.props.authorName;
}
// author requires at least a name
return undefined;
}
};