Skip to content

Commit 375916b

Browse files
committed
feat: support scoped package name, fix #236
1 parent b28421d commit 375916b

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

__tests__/app.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,38 @@ describe('generator:app', () => {
6969
assert.fileContent('.eslintignore', '**/templates\n');
7070
});
7171
});
72+
73+
describe('scoped name', () => {
74+
beforeEach(() => {
75+
return helpers.run(path.join(__dirname, '../app')).withPrompts({
76+
name: '@yeoman/generator-temp',
77+
description: 'A node generator',
78+
homepage: 'http://yeoman.io',
79+
githubAccount: 'yeoman',
80+
authorName: 'The Yeoman Team',
81+
authorEmail: '[email protected]',
82+
authorUrl: 'http://yeoman.io',
83+
keywords: [],
84+
license: 'MIT'
85+
});
86+
});
87+
88+
it('created and CD into a folder named like the generator', () => {
89+
assert.equal(path.basename(process.cwd()), 'generator-temp');
90+
});
91+
92+
it('fills package.json with correct information', () => {
93+
// eslint-disable-next-line new-cap
94+
assert.JSONFileContent('package.json', {
95+
name: '@yeoman/generator-temp'
96+
});
97+
});
98+
99+
it('fills the README with project data', () => {
100+
assert.fileContent('README.md', '# @yeoman/generator-temp');
101+
assert.fileContent('README.md', 'npm install -g yo');
102+
assert.fileContent('README.md', 'npm install -g @yeoman/generator-temp');
103+
assert.fileContent('README.md', 'yo @yeoman/temp');
104+
});
105+
});
72106
});

app/index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@ const _ = require('lodash');
66
const extend = require('deep-extend');
77
const mkdirp = require('mkdirp');
88

9+
function parseScopedName(name) {
10+
const nameFragments = name.split('/');
11+
const parseResult = {
12+
scopeName: '',
13+
localName: name
14+
};
15+
16+
if (nameFragments.length > 1) {
17+
parseResult.scopeName = nameFragments[0];
18+
parseResult.localName = nameFragments[1];
19+
}
20+
21+
return parseResult;
22+
}
23+
924
function makeGeneratorName(name) {
25+
const parsedName = parseScopedName(name);
26+
name = parsedName.localName;
1027
name = _.kebabCase(name);
1128
name = name.indexOf('generator-') === 0 ? name : 'generator-' + name;
12-
return name;
29+
return parsedName.scopeName ? `${parsedName.scopeName}/${name}` : name;
1330
}
1431

1532
module.exports = class extends Generator {
@@ -31,16 +48,17 @@ module.exports = class extends Generator {
3148
this
3249
).then(props => {
3350
this.props.name = props.name;
51+
Object.assign(this.props, parseScopedName(props.name));
3452
});
3553
}
3654

3755
default() {
38-
if (path.basename(this.destinationPath()) !== this.props.name) {
56+
if (path.basename(this.destinationPath()) !== this.props.localName) {
3957
this.log(
40-
`Your generator must be inside a folder named ${this.props.name}\nI'll automatically create this folder.`
58+
`Your generator must be inside a folder named ${this.props.localName}\nI'll automatically create this folder.`
4159
);
42-
mkdirp.sync(this.props.name);
43-
this.destinationRoot(this.destinationPath(this.props.name));
60+
mkdirp.sync(this.props.localName);
61+
this.destinationRoot(this.destinationPath(this.props.localName));
4462
}
4563

4664
const readmeTpl = _.template(this.fs.read(this.templatePath('README.md')));

0 commit comments

Comments
 (0)